Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct wezel
- {
- int wartosc;
- struct wezel *lewy;
- struct wezel *prawy;
- };
- struct wezel* newwezel(int wartosc)
- {
- struct wezel* wezel = (struct wezel*)malloc(sizeof(struct wezel));
- wezel->wartosc = wartosc;
- wezel->lewy = NULL;
- wezel->prawy = NULL;
- return(wezel);
- }
- void printInorder(struct wezel* wezel)
- {
- if (wezel == NULL)
- return;
- printInorder(wezel->lewy);
- printf("%d ", wezel->wartosc);
- printInorder(wezel->prawy);
- }
- void printPreorder(struct wezel* wezel)
- {
- if (wezel == NULL)
- return;
- printf("%d ", wezel->wartosc);
- printPreorder(wezel->lewy);
- printPreorder(wezel->prawy);
- }
- void printPostorder(struct wezel* wezel)
- {
- if (wezel == NULL)
- return;
- printPostorder(wezel->lewy);
- printPostorder(wezel->prawy);
- printf("%d ", wezel->wartosc);
- }
- struct wezel* wstawBST(struct wezel* wezel, int wartosc)
- {
- if (wezel == NULL)
- return newwezel(wartosc);
- if (wartosc < wezel->wartosc)
- wezel->lewy = wstawBST(wezel->lewy, wartosc);
- else if (wartosc > wezel->wartosc)
- wezel->prawy = wstawBST(wezel->prawy, wartosc);
- return wezel;
- }
- struct wezel* szukaj(struct wezel* korzen, int wartosc)
- {
- if (korzen == NULL || korzen->wartosc == wartosc)
- return korzen;
- if (korzen->wartosc < wartosc)
- return szukaj(korzen->prawy, wartosc);
- return szukaj(korzen->lewy, wartosc);
- }
- int main()
- {
- struct wezel *korzen = newwezel(79);
- wstawBST(korzen,13);
- wstawBST(korzen,23);
- wstawBST(korzen,15);
- wstawBST(korzen,111);
- wstawBST(korzen,64);
- wstawBST(korzen,34);
- printf("Post-order ");
- printPostorder(korzen);
- printf("\nPre-order ");
- printPreorder(korzen);
- printf("\nIn-order ");
- printInorder(korzen);
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement