Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- struct ed //drzewo
- {
- int liczba;
- int licznik; //pokazuje ile cos wystapilo
- ed* prawy;
- ed* lewy;
- };
- void dodaj(ed*& r, int liczba)
- {
- if (r == NULL) //pierwszy element drzewa
- {
- r = new ed;
- r->liczba = liczba;
- r->licznik = 1; //incijalizacja
- r->lewy = NULL;
- r->prawy = NULL;
- return;
- }
- if (r->liczba > liczba) //dodanie na lewo czy prawo
- dodaj(r->lewy, liczba);
- else
- if (r->liczba == liczba)
- r->licznik++;
- else
- dodaj(r->prawy, liczba);
- }
- void wypiszliczby(int liczba)
- {
- if (liczba > 200) return;
- cout << liczba << " ";
- wypiszliczby(liczba + 1); //rekurencja
- }
- int silnia1(int liczba)
- {
- if (liczba <2) return 1;
- return liczba*silnia1(liczba - 1);
- }
- void wypisz(ed*r)
- {
- if (r == NULL) return;
- wypisz(r->lewy);
- cout << r->liczba << " (" << r->licznik << ")" << endl;
- wypisz(r->prawy);
- }
- void usuncaledrzewo(ed*&root)
- {
- if (root == NULL) return;
- usuncaledrzewo(root->prawy);
- usuncaledrzewo(root->lewy);
- delete root;
- root = NULL; //usunecie korzenia czyli calej gory
- }
- int silnia(int liczba);
- int main()
- {
- // for (int i = 0; i < 7; i++)
- // cout << i << "!=" << silnia(i) << endl;
- // int a = 18;
- // wypiszliczby(a); //wypiszliczby(18);
- // silnia1(5);
- srand(time(NULL));
- ed* root = NULL; //drzewo jest puste na poczatku
- for (int i = 0; i < 15; i++)
- {
- dodaj(root,rand()%10);
- }
- wypisz(root);
- usuncaledrzewo(root);
- root = NULL;
- wypisz(root);
- system("pause");
- return 0;
- }
- int silnia(int liczba)
- {if (liczba < 2)return 1;
- int wynik = 1;
- for (int i = 2; i <= liczba; i++)
- wynik = i*wynik;
- return wynik;
- }
Advertisement
Add Comment
Please, Sign In to add comment