Facebook
From Paltry Marten, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 225
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Wezel
  6. {
  7.     int wartosc;
  8.     Wezel *lewy, *prawy, *ojciec;
  9. };
  10. Wezel *root = NULL;
  11.  
  12. void dodajBST(int w, Wezel *start);
  13. void searchBST(int w, Wezel *start);
  14. void printBST(Wezel *p);
  15.  
  16. int main()
  17. {
  18.     dodajBST(10,root);
  19.     dodajBST(5, root);
  20.     dodajBST(3, root);
  21.     dodajBST(9, root);
  22.     dodajBST(8, root);
  23.     dodajBST(14, root);
  24.     dodajBST(12, root);
  25.     dodajBST(1, root);
  26.     dodajBST(11, root);
  27.     printBST(root);
  28.  
  29.     cout << endl;
  30.  
  31.     searchBST(10, root);
  32.     searchBST(1, root);
  33.     searchBST(12, root);
  34.     searchBST(3, root);
  35.     searchBST(14, root);
  36.     searchBST(9, root);
  37.  
  38.     return 0;
  39. }
  40.  
  41. void dodajBST(int w, Wezel *start)
  42. {
  43.     if(root == NULL)
  44.     {
  45.         Wezel *n = new Wezel();
  46.         n->wartosc=w;
  47.         n->ojciec=NULL;
  48.         n->lewy=NULL;
  49.         n->prawy=NULL;
  50.         root = n;
  51.     }
  52.     else if(w < start->wartosc)
  53.     {
  54.         if(start->lewy != NULL)
  55.         {
  56.             dodajBST(w, start->lewy);
  57.         }
  58.         else
  59.         {
  60.             Wezel *n1 = new Wezel();
  61.             n1->wartosc = w;
  62.             n1->prawy = NULL;
  63.             n1->lewy = NULL;
  64.             n1->ojciec = start;
  65.             start->lewy = n1;
  66.         }
  67.     }
  68.     else
  69.     {
  70.         if(start->prawy != NULL)
  71.         {
  72.             dodajBST(w, start->prawy);
  73.         }
  74.         else
  75.         {
  76.             Wezel * n2 = new Wezel;
  77.             n2->wartosc = w;
  78.             n2->prawy = NULL;
  79.             n2->lewy = NULL;
  80.             n2->ojciec = start;
  81.             start->prawy = n2;
  82.         }
  83.     }
  84. }
  85. void printBST(Wezel *p)
  86. {
  87.     if(p->lewy != NULL)
  88.     {
  89.         printBST(p->lewy);
  90.     }
  91.  
  92.     cout << p->wartosc << " ";
  93.  
  94.     if(p->prawy != NULL)
  95.     {
  96.         printBST(p->prawy);
  97.     }
  98. }
  99. void searchBST(int w, Wezel *start)
  100. {
  101.     if(w == start->wartosc)
  102.     {
  103.         cout << "Znaleziono lizbe: " << w << endl;
  104.     }
  105.     else if(w < start->wartosc)
  106.     {
  107.         if(start->lewy != NULL)
  108.         {
  109.             searchBST(w, start->lewy);
  110.         }
  111.     }
  112.     else
  113.     {
  114.         if(w > start->wartosc)
  115.         {
  116.  
  117.             if(start->prawy != NULL)
  118.             {
  119.                 searchBST(w, start->prawy);
  120.             }
  121.         }
  122.     }
  123. }
  124.  
  125.