Advertisement
wurdalak007

AVLTree

Nov 17th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct AVLTree{
  6.  
  7. int position;
  8. int key;
  9. int depth;
  10. AVLTree* left;
  11. AVLTree* right;
  12.  
  13. AVLTree() :
  14. position(0),
  15. key(0),
  16. depth(0),
  17. left( nullptr ),
  18. right( nullptr )
  19. {}
  20.  
  21. explicit AVLTree( int key );
  22.  
  23. };
  24.  
  25. AVLTree::AVLTree( int key ) :
  26. position( 0 ),
  27. key( key ),
  28. depth( 1 ),
  29. left( nullptr ),
  30. right( nullptr )
  31. {}
  32.  
  33. int Number_of_higher ( AVLTree* root ) {
  34. if( root == nullptr )
  35. return 0;
  36. return Number_of_higher( root->left ) + Number_of_higher( root->right ) + 1;
  37.  
  38. }
  39.  
  40. void update_depth( AVLTree*& root ) {
  41.  
  42. if( root == nullptr ) {
  43. return;
  44. } else {
  45. if( root->left != nullptr && root->right != nullptr ){
  46. if( root->left->depth > root->right->depth ) {
  47. root->depth = root->left->depth + 1;
  48. } else {
  49. root->depth = root->right->depth + 1;
  50. }
  51. }
  52. }
  53.  
  54. }
  55.  
  56. int safe_depth( AVLTree* root ) {
  57. if( root == nullptr ) {
  58. return 0;
  59. } else {
  60. return root->depth;
  61. }
  62. }
  63.  
  64. AVLTree* balance( AVLTree*& tree ) {
  65. if( tree == nullptr ) {
  66. return nullptr;
  67. }
  68. int imbalance = 0;
  69. imbalance = safe_depth( tree->left ) - safe_depth( tree->right );
  70.  
  71. if( imbalance < -1 ) {
  72. AVLTree* a = tree;
  73. AVLTree* b = tree->right;
  74. AVLTree* L = tree->left;
  75. AVLTree* c = b->left;
  76. AVLTree* R = b->right;
  77. if( c->depth <= R->depth ) {
  78. b->left = a;
  79. a->left = L;
  80. a->right = c;
  81. update_depth( a );
  82. update_depth( R );
  83. return R;
  84. } else {
  85. AVLTree* c1 = c->left;
  86. AVLTree* c2 = c->right;
  87. a->right = c1;
  88. c->left = a;
  89. b->left = c2;
  90. c->right = b;
  91. update_depth( a );
  92. update_depth( b );
  93. update_depth( c );
  94. return c;
  95. }
  96. } else if( imbalance > 1 ) {
  97. AVLTree* a = tree;
  98. AVLTree* b = tree->right;
  99. AVLTree* L = tree->left;
  100. AVLTree* c = b->left;
  101. AVLTree* R = b->right;
  102. if( c->depth <= L->depth ) {
  103. b->right = a;
  104. a->left = c;
  105. a->right = R;
  106. update_depth( a );
  107. update_depth( R );
  108. } else {
  109. AVLTree* M = c->right;
  110. AVLTree* N = c->right;
  111. a->left = N;
  112. c->right = a;
  113. c->left = b;
  114. b->right = M;
  115. update_depth( a );
  116. update_depth( b );
  117. update_depth( c );
  118. }
  119. return nullptr;
  120. } else {
  121. return tree;
  122. }
  123.  
  124. }
  125.  
  126. void update_position( AVLTree*& root ) {
  127. if( root == nullptr ) {
  128. return;
  129. }
  130. update_position( root->left );
  131. update_position( root->right );
  132. root->position++;
  133. }
  134.  
  135. AVLTree* insert( AVLTree*& root, AVLTree* current ) {
  136.  
  137. if( root == nullptr ){
  138. root = current;
  139. return root;
  140. }
  141.  
  142. if( root->key > current->key ) {
  143. current->position += 1 + Number_of_higher( root->right );
  144. insert( root->left, current );
  145. } else {
  146. root->position++;
  147. update_position( root->left );
  148. insert( root->right, current );
  149. }
  150.  
  151. update_depth( root );
  152. balance( root );
  153. return root;
  154. }
  155.  
  156. void update_remove_position( AVLTree*& root ) {
  157. if( root == nullptr ) {
  158. return;
  159. }
  160. update_remove_position( root->left );
  161. update_remove_position( root->right );
  162. root->position--;
  163. }
  164.  
  165. AVLTree* findmin( AVLTree* root ) {
  166. if( root->left != nullptr ) {
  167. return findmin(root->left);
  168. } else {
  169. return root;
  170. }
  171. }
  172.  
  173. AVLTree* removemin( AVLTree*& root ) {
  174.  
  175. if( root->left == nullptr ) {
  176. return root->right;
  177. }
  178.  
  179. root->left = removemin( root->left );
  180.  
  181. return balance( root );
  182. }
  183.  
  184. AVLTree* remove( AVLTree*& root, int position ) {
  185. if( root == nullptr ) {
  186. return nullptr;
  187. }
  188.  
  189. if( root->position > position ) {
  190. root->position--;
  191. update_remove_position( root-> left );
  192. root->right = remove( root->right, position );
  193. } else if( root->position < position ) {
  194. root->left = remove( root->left, position );
  195. } else {
  196.  
  197. update_remove_position( root->left );
  198.  
  199. AVLTree* left = root->left;
  200. AVLTree* right = root->right;
  201.  
  202. delete root;
  203. root = NULL;
  204.  
  205. if( right == nullptr ) {
  206. return left;
  207. }
  208.  
  209. AVLTree* min = findmin( right );
  210. min->right = removemin( right );
  211. min->left = left;
  212. update_depth( root );
  213. return balance( min );
  214.  
  215. }
  216.  
  217. update_depth( root );
  218. return balance( root );
  219. }
  220.  
  221. void deletetree( AVLTree*& root ) {
  222. if( root == nullptr )
  223. return;
  224.  
  225. deletetree( root->left );
  226. deletetree( root->right );
  227.  
  228. delete root;
  229. root = NULL;
  230. }
  231.  
  232. int main() {
  233. int n = 0;
  234. int height = 0;
  235. int com = 0;
  236.  
  237.  
  238. cin >> n;
  239. cin >> com >> height;
  240. cout<<" ";
  241. AVLTree* root = new AVLTree ( height );
  242.  
  243. cout << root->position << " ";
  244.  
  245. for( int i = 1; i < n; i++ ) {
  246.  
  247. cin >> com;
  248. cin >> height;
  249.  
  250. if( com == 2 ) {
  251. root = remove(root, height);
  252. } else {
  253. AVLTree* current = new AVLTree( height );
  254. insert( root, current );
  255. cout << current->position << " ";
  256. }
  257.  
  258. }
  259.  
  260. deletetree( root );
  261.  
  262. return 0;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement