Advertisement
MeehoweCK

Untitled

Feb 8th, 2021
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. struct Drzewo
  6. {
  7.     int value;
  8.     Drzewo* left;
  9.     Drzewo* right;
  10.     int ile_powtorzen;
  11. };
  12.  
  13. void dodaj(Drzewo* drzewo, int wartosc)
  14. {
  15.     if(drzewo == nullptr)
  16.     {
  17.         drzewo = new Drzewo;
  18.         drzewo->value = wartosc;
  19.         drzewo->left = nullptr;
  20.         drzewo->right = nullptr;
  21.         drzewo->ile_powtorzen = 1;
  22.         return;
  23.     }
  24.     if(drzewo->value == wartosc)
  25.     {
  26.         drzewo->ile_powtorzen++;
  27.         return;
  28.     }
  29.     if(wartosc < drzewo->value)
  30.     {
  31.         dodaj(drzewo->left, wartosc);
  32.         return;
  33.     }
  34.     if(wartosc > drzewo->value)
  35.     {
  36.         dodaj(drzewo->right, wartosc);
  37.         return;
  38.     }
  39. }
  40.  
  41. bool wyszukaj_wartosc(int wartosc, Drzewo* drzewo)
  42. {
  43.     if(drzewo == nullptr)
  44.         return false;
  45.     if(drzewo->value == wartosc)
  46.         return true;
  47.     if(wartosc < drzewo->value)
  48.         return wyszukaj_wartosc(wartosc, drzewo->left);
  49.     if(wartosc > drzewo->value)
  50.         return wyszukaj_wartosc(wartosc, drzewo->right);
  51. }
  52.  
  53. int main()
  54. {
  55.     int tablica[100];
  56.     srand(time(nullptr));
  57.     for(int i = 0; i < 100; ++i)
  58.         tablica[i] = rand();
  59.    
  60.     // przenoszenie tablicy do drzewa:    
  61.     for(int i = 0; i < 100; ++i)
  62.     {
  63.         dodaj(drzewo, tablica[i]);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement