Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cmath>
  4. #define NOT_IMPLEMENTED -70
  5. using namespace std;
  6.  
  7. const int Max = 1000; // Maksymalna wartosc klucza drzewa
  8.  
  9. class BST
  10. {
  11.  
  12. // Struktura w�z�a drzewa.
  13. struct node
  14. {
  15. int less;
  16. int number;
  17. int key; // klucz wezla
  18. node *left; // adres lewego potomka
  19. node *right; // adres prawego potomka
  20. node(int elem, node *l, node *r)
  21. {
  22. less = 0;
  23. number = 1;
  24. key = elem;
  25. left = l;
  26. right = r;
  27. }
  28. };
  29.  
  30. //typedef node* tree;
  31.  
  32. private:
  33. void DestroyTree(node *&t) // Usuwanie poddrzewa o korzeniu t
  34. {
  35. if (t == NULL)
  36. return;
  37. DestroyTree(t->left);
  38. DestroyTree(t->right);
  39. delete t;
  40. }
  41.  
  42. void Print(node *t) // Drukowanie poddrzewa o korzeniu t
  43. {
  44. if (t == NULL)
  45. return;
  46. Print(t->left);
  47. cout << t->key << " ";
  48. Print(t->right);
  49. }
  50. void PrintDetails(node *t)
  51. {
  52. if (t == NULL)
  53. return;
  54. cout << "Klucz: " << t->key << " " << endl;
  55. cout << " LEWY SYN: ";
  56. if (t->left == NULL)
  57. cout << "PUSTE\n";
  58. else
  59. cout << t->left->key << " " << endl;
  60. cout << " PRAWY SYN: ";
  61. if (t->right == NULL)
  62. cout << "PUSTE\n";
  63. else
  64. cout << t->right->key << " " << endl;
  65. PrintDetails(t->left);
  66. PrintDetails(t->right);
  67. }
  68.  
  69. public:
  70. node * root; // Korzen drzewa
  71. BST() { root = NULL; } // Konstruktor drzewa
  72. ~BST() { DestroyTree(root); } // Destruktor drzewa
  73.  
  74. // Funkcja logiczna, zwraca TRUE, jesli klucz elem jest w drzewie, wpp zwraca FALSE
  75. //
  76. bool Member(int elem)
  77. {
  78. node *t = root;
  79. while (t != NULL)
  80. {
  81. if (t->key == elem)
  82. return true; // Znaleziono wezel o kluczu elem
  83. if (elem > t->key)
  84. t = t->right;
  85. else
  86. t = t->left; // Schodzimy do odpowiedniego poddrzewa
  87. }
  88. return false; // Brak klucza elem w drzewie
  89. }
  90.  
  91. // Procedura wstawiania elementu elem do drzewa
  92. //
  93. void Insert(int elem)
  94. {
  95. int q = 0;
  96. node *t = root; // Aktualnie rozpatrywany wezel
  97. node *p = NULL; // Ojciec aktualnie rozpatrywanego wezla
  98. while (t != NULL) // Dopoki istnieje wezel
  99. {
  100. if (elem == t->key) {
  101. t->number++;
  102. return;
  103. }
  104. // Klucz elem jest juz w drzewie
  105. p = t; // Aktualizuj ojca aktualnego w�z�a
  106. if (elem < t->key) {
  107. t->less++;
  108. t = t->left; // Schodzimy do lewego poddrzewa
  109. }
  110. else {
  111. q++;
  112. t = t->right; // Schodzimy do prawego poddrzewa
  113. }
  114.  
  115. }
  116. t = new node(elem, NULL, NULL); // Utworzenie nowego w�z�a
  117. if (p == NULL)
  118. root = t; // Dotychczasowe drzewo bylo puste
  119. else // Dotychczasowe drzewo bylo niepuste
  120. if (elem < p->key)
  121. p->left = t;
  122. else
  123. p->right = t; // Dowiazanie nowego wezla do odpowiedniego poprzednika
  124. t->less == q;
  125. }
  126.  
  127. // BUDOWA DRZEWA
  128. // O losowej liczbie wezlow
  129. //
  130. void BuildRandomTree()
  131. {
  132. int n = rand() % Max + 1;
  133. for (int i = 0; i < n; i++)
  134. {
  135. do
  136. {
  137. int elem = rand() % Max + 1;
  138. if (!Member(elem))
  139. {
  140. Insert(elem);
  141. break;
  142. }
  143. } while (true);
  144. }
  145. }
  146.  
  147. // O n wezlach
  148. //
  149. void BuildTree(int n)
  150. {
  151. int m = 0;
  152. do
  153. {
  154. int elem = rand() % Max + 1;
  155. if (!Member(elem))
  156. {
  157. m++;
  158. Insert(elem);
  159. }
  160. } while (m < n);
  161. }
  162.  
  163. // DRUKOWANIE DRZEWA
  164. //
  165. // W porzadku Inorder
  166. void PrintTree()
  167. {
  168. Print(root);
  169. cout << endl;
  170. }
  171.  
  172. // Szczegolowe
  173. void PrintDetailedTree()
  174. {
  175. PrintDetails(root);
  176. }
  177.  
  178. //________________________________________________________________________
  179. // Uzupelnienie klasy zgodnie z zadaniem
  180. //________________________________________________________________________
  181.  
  182. /// Funkcja zwraca różnicę pomiędzy przychodem uzyskanym z najbardziej dochodowej aukcji a najmniej dochodowej aukcji.
  183. /// Jeśli różnica nie jest możliwa do wyznaczenia - funkcja zwraca -1
  184. int AuctionDiff()
  185. {
  186. if (root == NULL) {
  187. return -1;
  188. }
  189. node* min = root;
  190. node* max = root;
  191. while (min->left != NULL) {
  192. min = min->left;
  193. }
  194. while (max->right != NULL) {
  195. max = max->right;
  196. }
  197.  
  198. return max->key-min->key;
  199. }
  200.  
  201. /// Funkcja zwraca liczbę aukcji, które uzyskały przychód równy p
  202. /// Jeśli aukcja o podanym przychodzie p nie istnieje - funkcja powinna zwrócić -1
  203. int HowManyAuctions(int p)
  204. {
  205. node* t = root;
  206. while (t != NULL) {
  207. if (t->key == p) {
  208. return t->number;
  209. }
  210. else if (t->key < p) {
  211. t = t->right;
  212. }
  213. else t = t->left;
  214. }
  215.  
  216.  
  217. return -1;
  218. }
  219.  
  220. /// Funkcja zwraca liczbę aukcji, które uzyskały mniej niż a przychodu
  221. /// UWAGA: Zakładamy, że a nie jest większe niż najwyższy przychód z aukcji.
  222. /// Jeśli nie jest możliwe określenie ilości aukcji - funkcja powinna zwrócić -1
  223. int LowerThan(int a) {
  224. node* t = root;
  225. while (t != NULL) {
  226. if (t->key < a && t->right->key >= a) {
  227. return t->right->less;
  228. }
  229. else if (t->key < a && t->right->key < a) {
  230. t = t->right;
  231. }
  232. else if (t->key > a && t->left->key > a) {
  233. t = t->left;
  234. }
  235. else if (t->key >= a && t->left->key < a) {
  236. return t->less;
  237. }
  238. else if (t -> key == a)
  239. return t->less;
  240.  
  241. }
  242. return -1;
  243. }
  244.  
  245. //________________________________________________________________________
  246. // I to juz koniec definicji klasy BST :-)
  247. //________________________________________________________________________
  248. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement