Guest User

Untitled

a guest
Jan 11th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4. struct ed   //drzewo
  5. {
  6.     int liczba;
  7.     int licznik;  //pokazuje ile cos wystapilo
  8.     ed* prawy;
  9.     ed* lewy;
  10.    
  11. };
  12. void dodaj(ed*& r, int liczba)
  13. {
  14.     if (r == NULL)  //pierwszy element drzewa
  15.     {
  16.         r = new ed;
  17.         r->liczba = liczba;
  18.         r->licznik = 1; //incijalizacja
  19.         r->lewy = NULL;
  20.         r->prawy = NULL;
  21.         return;
  22.     }
  23.     if (r->liczba > liczba)  //dodanie na lewo czy prawo
  24.         dodaj(r->lewy, liczba);
  25.     else
  26.     if (r->liczba == liczba)
  27.         r->licznik++;
  28.     else
  29.         dodaj(r->prawy, liczba);
  30.  
  31. }
  32.  
  33. void wypiszliczby(int liczba)
  34. {
  35.     if (liczba > 200) return;
  36.     cout << liczba << "   ";
  37.         wypiszliczby(liczba + 1);  //rekurencja
  38.  
  39. }
  40. int silnia1(int liczba)
  41. {
  42.     if (liczba <2)  return 1;
  43.     return liczba*silnia1(liczba - 1);
  44. }
  45. void wypisz(ed*r)
  46. {
  47.     if (r == NULL) return;
  48.     wypisz(r->lewy);
  49.     cout << r->liczba << " (" << r->licznik << ")" << endl;
  50.     wypisz(r->prawy);
  51. }
  52. void usuncaledrzewo(ed*&root)
  53. {
  54.     if (root == NULL) return;
  55.     usuncaledrzewo(root->prawy);
  56.     usuncaledrzewo(root->lewy);
  57.     delete root;
  58.         root = NULL;  //usunecie korzenia czyli calej gory  
  59. }
  60. int silnia(int liczba);
  61. int main()
  62. {
  63. //  for (int i = 0; i < 7; i++)
  64. //      cout << i << "!=" << silnia(i) << endl;
  65. //  int a = 18;
  66. //  wypiszliczby(a);  //wypiszliczby(18);
  67. //  silnia1(5);
  68.     srand(time(NULL));
  69.     ed* root = NULL;  //drzewo jest puste na poczatku
  70.     for (int i = 0; i < 15; i++)
  71.     {
  72.         dodaj(root,rand()%10);
  73.     }
  74.     wypisz(root);
  75.     usuncaledrzewo(root);
  76. root = NULL;
  77.         wypisz(root);
  78.     system("pause");
  79.     return 0;
  80. }
  81. int silnia(int liczba)
  82. {if (liczba < 2)return 1;
  83. int wynik = 1;
  84. for (int i = 2; i <= liczba; i++)
  85. wynik = i*wynik;
  86. return wynik;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment