#include <iostream>
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);
}
}
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}