#include using namespace std; struct Wezel { int wartosc; Wezel *lewy, *prawy, *ojciec; }; Wezel *root = NULL; void dodajBST(int w, Wezel *start); void searchBST(int w, Wezel *start); void printBST(Wezel *p); int main() { dodajBST(10,root); dodajBST(5, root); dodajBST(3, root); dodajBST(9, root); dodajBST(8, root); dodajBST(14, root); dodajBST(12, root); dodajBST(1, root); dodajBST(11, root); printBST(root); cout << endl; searchBST(10, root); searchBST(1, root); searchBST(12, root); searchBST(3, root); searchBST(14, root); searchBST(9, root); return 0; } void dodajBST(int w, Wezel *start) { if(root == NULL) { Wezel *n = new Wezel(); n->wartosc=w; n->ojciec=NULL; n->lewy=NULL; n->prawy=NULL; root = n; } else if(w < start->wartosc) { if(start->lewy != NULL) { dodajBST(w, start->lewy); } else { Wezel *n1 = new Wezel(); n1->wartosc = w; n1->prawy = NULL; n1->lewy = NULL; n1->ojciec = start; start->lewy = n1; } } else { if(start->prawy != NULL) { dodajBST(w, start->prawy); } else { Wezel * n2 = new Wezel; n2->wartosc = w; n2->prawy = NULL; n2->lewy = NULL; n2->ojciec = start; start->prawy = n2; } } } void printBST(Wezel *p) { if(p->lewy != NULL) { printBST(p->lewy); } cout << p->wartosc << " "; if(p->prawy != NULL) { printBST(p->prawy); } } void searchBST(int w, Wezel *start) { if(w == start->wartosc) { cout << "Znaleziono lizbe: " << w << endl; } else if(w < start->wartosc) { if(start->lewy != NULL) { searchBST(w, start->lewy); } } else { if(w > start->wartosc) { if(start->prawy != NULL) { searchBST(w, start->prawy); } } } }