Advertisement
Guest User

Untitled

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