Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.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.     // your code here
  33.     if (t == NULL || t->right == NULL) {
  34.         return;
  35.     }
  36.     //we have t->b->c to the right, we do:
  37.     //b's left child becomes t's right child.
  38.     //t becomes b's left child
  39.     Node * newRoot = t->right;
  40.     Node * temp = newRoot->left;
  41.  
  42.     newRoot->left = t;
  43.     t->right = temp;
  44.     //t = newRoot;
  45.  
  46.     newRoot->height = findHeight(newRoot);
  47.     if (newRoot->left != NULL){
  48.         newRoot->left->height = findHeight(newRoot->left);
  49.     }
  50.     if (newRoot->right != NULL){
  51.         newRoot->right->height = findHeight(newRoot->right);
  52.     }
  53.  
  54. }
  55.  
  56. template <class K, class V>
  57. void AVLTree<K, V>::rotateLeftRight(Node*& t)
  58. {
  59.     functionCalls.push_back("rotateLeftRight"); // Stores the rotation name (don't remove this)
  60.     // Implemented for you:
  61.     rotateLeft(t->left);
  62.     rotateRight(t);
  63. }
  64.  
  65. template <class K, class V>
  66. void AVLTree<K, V>::rotateRight(Node*& t)
  67. {
  68.     functionCalls.push_back("rotateRight"); // Stores the rotation name (don't remove this)
  69.     Node * newRoot = t->left;
  70.     Node * temp = newRoot->right;
  71.  
  72.     newRoot->right = t;
  73.     t->left = temp;
  74.  
  75.     newRoot->height = findHeight(newRoot);
  76.     if (newRoot->left != NULL){
  77.         newRoot->left->height = findHeight(newRoot->left);
  78.     }
  79.     if (newRoot->right != NULL){
  80.         newRoot->right->height = findHeight(newRoot->right);
  81.     }
  82. }
  83.  
  84. template <class K, class V>
  85. void AVLTree<K, V>::rotateRightLeft(Node*& t)
  86. {
  87.     functionCalls.push_back("rotateRightLeft"); // Stores the rotation name (don't remove this)
  88.     // your code here
  89.     rotateRight(t->right);
  90.     rotateLeft(t);
  91. }
  92.  
  93. template <class K, class V>
  94. void AVLTree<K, V>::rebalance(Node*& subtree)
  95. {
  96.     if (subtree == NULL){
  97.         return;
  98.     }
  99.  
  100.     int currBalanceFactor = balanceFactor(subtree);
  101.  
  102.     if (currBalanceFactor < -1){
  103.         if (balanceFactor(subtree->right) <= 0){
  104.             rotateLeft(subtree);
  105.         }
  106.         else {  
  107.             rotateRightLeft(subtree);
  108.         }
  109.     }
  110.     else if (currBalanceFactor > 1){      
  111.         if (balanceFactor(subtree->left) >= 0 ){
  112.             rotateRight(subtree);
  113.         }
  114.         else {  
  115.             rotateRight(subtree);
  116.         }
  117.     }
  118.    
  119.     subtree->height = findHeight(subtree);
  120.  
  121. }
  122.  
  123. template <class K, class V>
  124. int AVLTree<K, V>::balanceFactor(Node*& subtree) {
  125.     if (subtree == NULL){
  126.         return 0;
  127.     }
  128.  
  129.     int leftH = heightOrNeg1(subtree->left);
  130.     int rightH = heightOrNeg1(subtree->right);
  131.     return leftH - rightH;
  132. }
  133.  
  134. template <class K, class V>
  135. int AVLTree<K, V>::findHeight(Node*& subtree) {
  136.     if (subtree == NULL){
  137.         return 0;
  138.     }
  139.     return max(findHeight(subtree->left), findHeight(subtree->right)) + 1;
  140. }
  141.  
  142. template <class K, class V>
  143. void AVLTree<K, V>::insert(const K & key, const V & value)
  144. {
  145.     insert(root, key, value);
  146. }
  147.  
  148. template <class K, class V>
  149. void AVLTree<K, V>::insert(Node*& subtree, const K& key, const V& value)
  150. {
  151.     if (subtree == NULL){
  152.         subtree = new Node(key, value);
  153.     }
  154.     else if (subtree->key == key){
  155.         subtree->value = value;
  156.     }
  157.     else if (key < subtree->key){
  158.         insert(subtree->left, key, value);
  159.         //rebalance(subtree);
  160.     }
  161.     else {
  162.         insert(subtree->right, key, value);
  163.         //rebalance(subtree);
  164.     }
  165. }
  166.  
  167. template <class K, class V>
  168. void AVLTree<K, V>::remove(const K& key)
  169. {
  170.     remove(root, key);
  171. }
  172.  
  173. template <class K, class V>
  174. void AVLTree<K, V>::remove(Node*& subtree, const K& key)
  175. {
  176.     if (subtree == NULL)
  177.         return;
  178.  
  179.     if (key < subtree->key) {
  180.         // your code here
  181.     } else if (key > subtree->key) {
  182.         // your code here
  183.     } else {
  184.         if (subtree->left == NULL && subtree->right == NULL) {
  185.             /* no-child remove */
  186.             // your code here
  187.         } else if (subtree->left != NULL && subtree->right != NULL) {
  188.             /* two-child remove */
  189.             // your code here
  190.         } else {
  191.             /* one-child remove */
  192.             // your code here
  193.         }
  194.         // your code here
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement