Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. /**
  2. * @file avltree.cpp
  3. * Definitions of the binary tree functions you'll be writing for this lab.
  4. * You'll need to modify this file.
  5. */
  6.  
  7. template <class K, class V>
  8. V AVLTree<K, V>::find(const K& key) const
  9. {
  10. return find(root, key);
  11. }
  12.  
  13. template <class K, class V>
  14. V AVLTree<K, V>::find(Node* subtree, const K& key) const
  15. {
  16. if (subtree == NULL)
  17. return V();
  18. else if (key == subtree->key)
  19. return subtree->value;
  20. else {
  21. if (key < subtree->key)
  22. return find(subtree->left, key);
  23. else
  24. return find(subtree->right, key);
  25. }
  26. }
  27.  
  28. template <class K, class V>
  29. void AVLTree<K, V>::rotateLeft(Node*& t)
  30. {
  31. functionCalls.push_back("rotateLeft"); // Stores the rotation name (don't remove this)
  32. Node * temp = t;
  33. Node * temp2 = t->right;
  34.  
  35. temp->right = temp2->left;
  36. temp2->left = temp;
  37. t = temp2;
  38. updateHeight(temp);
  39. updateHeight(temp);
  40. }
  41.  
  42. template <class K, class V>
  43. void AVLTree<K, V>::rotateLeftRight(Node*& t)
  44. {
  45. functionCalls.push_back("rotateLeftRight"); // Stores the rotation name (don't remove this)
  46. // Implemented for you:
  47. rotateLeft(t->left);
  48. rotateRight(t);
  49. }
  50.  
  51.  
  52. template <class K, class V>
  53. void AVLTree<K, V>::updateHeight(Node*& node) {
  54. node->height = 1 + max(heightOrNeg1(node->left), heightOrNeg1(node->right));
  55. }
  56.  
  57.  
  58.  
  59. template <class K, class V>
  60. void AVLTree<K, V>::rotateRight(Node*& t)
  61. {
  62. functionCalls.push_back("rotateRight"); // Stores the rotation name (don't remove this)
  63. Node *temp = t;
  64. Node *temp2 = t->left;
  65.  
  66. temp->left = temp2->right;
  67. temp2->right = temp;
  68. t = temp2;
  69. updateHeight(temp);
  70. updateHeight(temp2);
  71. }
  72.  
  73. template <class K, class V>
  74. void AVLTree<K, V>::rotateRightLeft(Node*& t)
  75. {
  76. functionCalls.push_back("rotateRightLeft"); // Stores the rotation name (don't remove this)
  77. rotateRight(t->right);
  78. rotateLeft(t);
  79. }
  80.  
  81. template <class K, class V>
  82. void AVLTree<K, V>::rebalance(Node*& subtree)
  83. {
  84. if (!subtree) {
  85. return;
  86. }
  87. int balance = heightOrNeg1(subtree->right) - heightOrNeg1(subtree->left);
  88.  
  89. if (balance == -2) {
  90. int leftbalance = heightOrNeg1(subtree->left->right) - heightOrNeg1(subtree->left->left);
  91. if (leftbalance == -1 ) {
  92. rotateRight(subtree);
  93. } else {
  94. rotateLeftRight(subtree);
  95. }
  96. } else if (balance == 2) {
  97. int rightbalance = heightOrNeg1(subtree->right->right) - heightOrNeg1(subtree->right->left);
  98. if (rightbalance == 1 ){
  99. rotateLeft(subtree);
  100. } else {
  101. rotateRightLeft(subtree);
  102. }
  103. }
  104. updateHeight(subtree);
  105. }
  106.  
  107. template <class K, class V>
  108. void AVLTree<K, V>::insert(const K & key, const V & value)
  109. {
  110. insert(root, key, value);
  111. }
  112.  
  113. template <class K, class V>
  114. void AVLTree<K, V>::insert(Node*& subtree, const K& key, const V& value)
  115. {
  116. if (subtree == NULL) {
  117. subtree = new Node(key, value);
  118. return;
  119. } else if (key > subtree->key) {
  120. insert(subtree->right, key, value);
  121. } else if (key < subtree->key) {
  122. insert(subtree->left, key, value);
  123. }
  124. rebalance(subtree);
  125.  
  126. }
  127.  
  128. template <class K, class V>
  129. void AVLTree<K, V>::remove(const K& key)
  130. {
  131. remove(root, key);
  132. }
  133.  
  134. template <class K, class V>
  135. void AVLTree<K, V>::remove(Node*& subtree, const K& key)
  136. {
  137. if (subtree == NULL)
  138. return;
  139.  
  140. if (key < subtree->key) {
  141. // your code here
  142. } else if (key > subtree->key) {
  143. // your code here
  144. } else {
  145. if (subtree->left == NULL && subtree->right == NULL) {
  146. /* no-child remove */
  147. // your code here
  148. } else if (subtree->left != NULL && subtree->right != NULL) {
  149. /* two-child remove */
  150. // your code here
  151. } else {
  152. /* one-child remove */
  153. // your code here
  154. }
  155. // your code here
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement