Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. const unsigned char KEY_MAX_LENGTH = 5;
  7.  
  8. struct node {
  9. int key;
  10. unsigned char height;
  11. node* left;
  12. node* right;
  13. node(int k) { key = k; left = right = 0; height = 1; }
  14. };
  15.  
  16. unsigned char height(node* p)
  17. {
  18. return p ? p->height : 0;
  19. }
  20.  
  21. int bfactor(node* p)
  22. {
  23. return height(p->right) - height(p->left);
  24. }
  25.  
  26. void fixheight(node* p)
  27. {
  28. unsigned char hl = height(p->left);
  29. unsigned char hr = height(p->right);
  30. p->height = (hl > hr ? hl : hr) + 1;
  31. }
  32.  
  33. node* rotateright(node* p) // правый поворот вокруг p
  34. {
  35. node* q = p->left;
  36. p->left = q->right;
  37. q->right = p;
  38. fixheight(p);
  39. fixheight(q);
  40. return q;
  41. }
  42.  
  43. node* rotateleft(node* q) // левый поворот вокруг q
  44. {
  45. node* p = q->right;
  46. q->right = p->left;
  47. p->left = q;
  48. fixheight(q);
  49. fixheight(p);
  50. return p;
  51. }
  52.  
  53. node* balance(node* p) // балансировка узла p
  54. {
  55. fixheight(p);
  56. if (bfactor(p) == 2)
  57. {
  58. if (bfactor(p->right) < 0)
  59. p->right = rotateright(p->right);
  60. return rotateleft(p);
  61. }
  62. if (bfactor(p) == -2)
  63. {
  64. if (bfactor(p->left) > 0)
  65. p->left = rotateleft(p->left);
  66. return rotateright(p);
  67. }
  68. return p; // балансировка не нужна
  69. }
  70.  
  71. node* insert(node* p, int k) // вставка ключа k в дерево с корнем p
  72. {
  73. if (!p) return new node(k);
  74. if (k < p->key)
  75. p->left = insert(p->left, k);
  76. else
  77. p->right = insert(p->right, k);
  78. return balance(p);
  79. }
  80.  
  81. node* findmin(node* p) // поиск узла с минимальным ключом в дереве p
  82. {
  83. return p->left ? findmin(p->left) : p;
  84. }
  85.  
  86. node* removemin(node* p) // удаление узла с минимальным ключом из дерева p
  87. {
  88. if (p->left == 0)
  89. return p->right;
  90. p->left = removemin(p->left);
  91. return balance(p);
  92. }
  93.  
  94. node* remove(node* p, int k) // удаление ключа k из дерева p
  95. {
  96. if (!p) return 0;
  97. if (k < p->key)
  98. p->left = remove(p->left, k);
  99. else if (k > p->key)
  100. p->right = remove(p->right, k);
  101. else // k == p->key
  102. {
  103. node* q = p->left;
  104. node* r = p->right;
  105. delete p;
  106. if (!r) return q;
  107. node* min = findmin(r);
  108. min->right = removemin(r);
  109. min->left = q;
  110. return balance(min);
  111. }
  112. return balance(p);
  113. }
  114.  
  115. bool search(node* p, int k) {
  116. bool res = false;
  117. if (p) {
  118. if (k == p->key)
  119. return true;
  120. if (k < p->key) {
  121. res = search(p->left, k);
  122. }
  123. if (k > p->key) {
  124. res = search(p->right, k);
  125. }
  126. }
  127. return res;
  128. }
  129.  
  130. void printGL(node* p) {
  131. if (p) {
  132. if (p->left) {
  133. printGL(p->left);
  134. }
  135. for (int i = 0; i < p->height - 1; i++)
  136. cout << "\t";
  137. cout << p->key;
  138. cout << endl;
  139. if (p->right) {
  140. printGL(p->right);
  141. }
  142. }
  143. }
  144.  
  145. void printSH(node* p) {
  146. if (p) {
  147. cout << p->key;
  148. for (int i = 0; i < p->height; i++) {
  149. cout << "*";
  150. }
  151. if (p->left)
  152.  
  153. if (p->right)
  154. printGL(p->right);
  155. }
  156. }
  157.  
  158. int main()
  159. {
  160. char ch = 0;
  161. do {
  162. node* p = nullptr;
  163. system("cls");
  164. cout << "Choose your destiny!\n";
  165. cout << "[1] - add\n[2] - remove\n[3] - search\n[4] - print\n";
  166. ch = _getch();
  167. switch (ch) {
  168. case '1':
  169. int a;
  170. cout << "Enter a key: ";
  171. cin >> a;
  172. if (!p) {
  173. p = new node(a);
  174. }
  175. else
  176. insert(p, a);
  177. cout << "insertion of " << a << " has been made succesfully";
  178. break;
  179. case '2':
  180. if (p) {
  181. cout << "Enter a key that you wanna remove: ";
  182. int a;
  183. cin >> a;
  184. remove(p, a);
  185. cout << "remove of " << a << " has been made succesfully";
  186. }
  187. else {
  188. cout << "Your tree is empty\n";
  189. }
  190. break;
  191. case '3':
  192. if (p) {
  193.  
  194. cout << "Enter a key that you wanna remove: ";
  195. int a;
  196. cin >> a;
  197. remove(p, a);
  198. cout << "remove of " << a << " has been made succesfully";
  199. }
  200. else {
  201. cout << "Your tree is empty\n";
  202. }
  203. break;
  204. case '4':
  205.  
  206. break;
  207. }
  208. } while (ch != 27);
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement