Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define rodzaj(type,scan) \
- ( \
- strcmp(#type, "int") == 0 ? "%d" : \
- strcmp(#type, "char") == 0 ? "%c" : \
- strcmp(#type, "double") == 0 && scan == 1 ? "%lf" : \
- strcmp(#type, "double") == 0 || strcmp(#type, "float") == 0 ? "%.2f": \
- "blad" \
- )
- #define porownaj(a,b) \
- ( \
- a > b ? 1 : \
- a < b ? -1 : \
- 0 \
- )
- #define print(type,zm) printf(rodzaj(type,0), zm)
- #define scan(type,zm) scanf(rodzaj(type,1), &zm)
- #define TYP int
- typedef struct element
- {
- TYP wartosc;
- struct element *lewe, *prawe;
- } wezel;
- wezel *wstaw(wezel *drzewo, TYP wart)
- {
- if(drzewo == NULL)
- {
- wezel *nowy;
- nowy = malloc(sizeof(wezel));
- nowy->wartosc = wart;
- nowy->lewe = NULL;
- nowy->prawe = NULL;
- return nowy;
- }
- else
- {
- if(porownaj(wart,drzewo->prawe)) drzewo->prawe = wstaw(drzewo->prawe,wart);
- else if(porownaj(wart,drzewo->lewe)==-1) drzewo->lewe = wstaw(drzewo->lewe,wart);
- }
- return drzewo;
- }
- void wypisz(wezel *drzewo)
- {
- if(drzewo->lewe != NULL) wypisz(drzewo->lewe);
- print(TYP, drzewo->wartosc);
- fputc(' ',stdout);
- if(drzewo->prawe != NULL) wypisz(drzewo->prawe);
- }
- void usun(wezel *drzewo)
- {
- if(drzewo != NULL)
- {
- usun(drzewo->lewe);
- usun(drzewo->prawe);
- free(drzewo);
- }
- }
- int main()
- {
- wezel *drzewo;
- drzewo=malloc(sizeof(wezel));
- drzewo->lewe=drzewo->prawe=NULL;
- wstaw(drzewo,12);
- wstaw(drzewo,2);
- wstaw(drzewo,10);
- wypisz(drzewo);
- usun(drzewo);
- return 0;
- }
Add Comment
Please, Sign In to add comment