Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- struct Drzewo
- {
- int value;
- Drzewo* left;
- Drzewo* right;
- int ile_powtorzen;
- };
- void dodaj(Drzewo* drzewo, int wartosc)
- {
- if(drzewo == nullptr)
- {
- drzewo = new Drzewo;
- drzewo->value = wartosc;
- drzewo->left = nullptr;
- drzewo->right = nullptr;
- drzewo->ile_powtorzen = 1;
- return;
- }
- if(drzewo->value == wartosc)
- {
- drzewo->ile_powtorzen++;
- return;
- }
- if(wartosc < drzewo->value)
- {
- dodaj(drzewo->left, wartosc);
- return;
- }
- if(wartosc > drzewo->value)
- {
- dodaj(drzewo->right, wartosc);
- return;
- }
- }
- bool wyszukaj_wartosc(int wartosc, Drzewo* drzewo)
- {
- if(drzewo == nullptr)
- return false;
- if(drzewo->value == wartosc)
- return true;
- if(wartosc < drzewo->value)
- return wyszukaj_wartosc(wartosc, drzewo->left);
- if(wartosc > drzewo->value)
- return wyszukaj_wartosc(wartosc, drzewo->right);
- }
- int main()
- {
- int tablica[100];
- srand(time(nullptr));
- for(int i = 0; i < 100; ++i)
- tablica[i] = rand();
- // przenoszenie tablicy do drzewa:
- for(int i = 0; i < 100; ++i)
- {
- dodaj(drzewo, tablica[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement