Advertisement
wurdalak007

Untitled

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