AVLTree.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*AVLTree.h
  2. *
  3. *Dylan Jeffers
  4. *Tahmid Rahman
  5. *This file taken from Joshua Brody's CS31 Class
  6. *Taught Fall of 2014 @ Swarthmore College
  7. */
  8. #ifndef AVLTREE_H_
  9. #define AVLTREE_H_
  10. #include "BST.h"
  11. #include "pair.h"
  12. #include "library/queue.h"
  13. // Forward declaration of the AVLTreeNode class
  14. template <typename K, typename V> class AVLTreeNode;
  15. /**
  16. * A AVLTree is a templated binary search tree, implementing the
  17. * BST interface (see BST.h). This implementation is similar
  18. * to LinkedBST except:
  19. * (1) Each AVLTreeNode stores the height of its sub-tree
  20. * (2) An AVLTree is balanced according to the AVL property:
  21. * the difference between the height of two child nodes is at most 1
  22. */
  23. template <typename K, typename V>
  24. class AVLTree : public BST<K,V> {
  25. private:
  26. int size; // Current number of items in the tree.
  27. AVLTreeNode<K,V>* root; // Pointer to the root node (possibly NULL).
  28. public:
  29. AVLTree();
  30. ~AVLTree();
  31. /* All public functions declared/detailed in BST.h*/
  32. /* These methods are defined in AVLTree-inl.h*/
  33. /* sizing operations */
  34. int getSize();
  35. bool isEmpty();
  36. int getHeight();
  37. /* test operations */
  38. bool isBalanced();
  39. /* Key operations */
  40. K getMax();
  41. K getMin();
  42. /* dictionary operations */
  43. void insert (K key, V value);
  44. void update (K key, V value);
  45. bool contains(K key);
  46. void remove (K key);
  47. V find (K key);
  48. /* traversal operations */
  49. Queue< Pair<K,V> >* getPreOrder();
  50. Queue< Pair<K,V> >* getInOrder();
  51. Queue< Pair<K,V> >* getPostOrder();
  52. Queue< Pair<K,V> >* getLevelOrder();
  53. private:
  54. /* Private recursive internal methods that
  55. * correspond to the public methods defined above.
  56. * These methods are defined in AVLTree-private-inl.h
  57. */
  58. /*
  59. * isBalancedInSubtree - Recursive function that tests whether a
  60. * a subtree is balanced
  61. * Note: all AVLTrees _should_ be balanced, so if this tree is not,
  62. * there's something wrong with the implementation.
  63. *
  64. * @param current : a pointer to the root of the subtree
  65. * @return bool: true iff the AVL subtree is indeed balanced.
  66. */
  67. bool isBalancedInSubtree(AVLTreeNode<K,V>* current);
  68. /* insertInSubtree - Recursive function that inserts a new node into
  69. * a sub-tree pointed to by current
  70. * @param current : a pointer to the root of the sub-tree
  71. * @param key : the key for the new node being inserted
  72. * @param value : the value for the new node being inserted
  73. * @error runtime_error if the key already exists
  74. * @return AVLTreeNode<K,V>*: the root of the sub-tree.
  75. */
  76. AVLTreeNode<K,V>* insertInSubtree(AVLTreeNode<K,V>* current, K key, V value);
  77. /* updateInSubtree - Recursive function that updates a key-value pair
  78. * in the tree
  79. * @param current : pointer to root node of the sub-tree
  80. * @param key : the key being searched for
  81. * @param value : the new value to associate with the given key
  82. * @error runtime_error if key not found
  83. */
  84. void updateInSubtree(AVLTreeNode<K,V>* current, K key, V value);
  85. /* removeFromSubtree - Recursive function that searches for and removes
  86. * the element associated with the given key in the
  87. * sub-tree pointed to by current
  88. * @param current : a pointer to the root of the sub-tree
  89. * @param key : the key for the node to be removed
  90. * @error runtime_error if the key does not exist
  91. * @return AVLTreeNode<K,V>*: the root of the sub-tree.
  92. */
  93. AVLTreeNode<K,V>* removeFromSubtree (AVLTreeNode<K,V>* current, K key);
  94. /* containsInSubtree - Recursive function that checks if the sub-tree
  95. * pointed by current contains the given key
  96. * @param current : pointer to root node of the sub-tree
  97. * @param key : the key being searched for
  98. * @return bool : true if the key was found
  99. */
  100. bool containsInSubtree (AVLTreeNode<K,V>* current, K key);
  101. /* findInSubtree - Recursive function that returns the value associated
  102. * with the given key in the sub-tree
  103. * pointed by current
  104. *@param current : pointer to root node of the sub-tree
  105. *@param key : the key being searched for
  106. *@error runtime_error if the key is not in the sub-tree
  107. *@return V : the value associated with the search key
  108. */
  109. V findInSubtree(AVLTreeNode<K,V>* current, K key);
  110. /* getMaxInSubtree - Recursive function that retrieves the maximal key
  111. * in the sub-tree pointed to by current
  112. *
  113. * @param current: pointer to root node of the sub-tree
  114. * @return K : the maximal key in the subtree
  115. */
  116. K getMaxInSubtree(AVLTreeNode<K,V>* current);
  117. /* getMinInSubtree - Recursive function that retrieves the minimal key
  118. * in the sub-tree pointed to by current
  119. *
  120. * @param current: pointer to root node of the sub-tree
  121. * @return K : the minimal key in the subtree
  122. */
  123. K getMinInSubtree(AVLTreeNode<K,V>* current);
  124. /* build{Pre,In,Post} - Recursive helper functions for building
  125. * iterators for the data structure.
  126. * Each enqueues all key-value pairs for the
  127. * sub-tree pointed to by current
  128. *
  129. * @param current : pointer to root node of the sub-tree
  130. * @param it : a pointer to the Queue to fill with the key-value
  131. * pairs based on the traversal order
  132. */
  133. void buildPreOrder (AVLTreeNode<K,V>* current, Queue< Pair<K,V> >* it);
  134. void buildInOrder (AVLTreeNode<K,V>* current, Queue< Pair<K,V> >* it);
  135. void buildPostOrder(AVLTreeNode<K,V>* current, Queue< Pair<K,V> >* it);
  136. /* traverseAndDelete - Recursive helper function for the destructor
  137. * Performs a post-order traversal of the sub-tree
  138. * freeing memory for all nodes in the sub-tree
  139. * pointed to by current
  140. * @param current : pointer to root node of the sub-tree to be deleted
  141. */
  142. void traverseAndDelete (AVLTreeNode<K,V>* current);
  143. /*These methods are unique to the AVLTree relative to LinkedBST. Each
  144. * maintains/ensures a balanced tree according to the AVL property
  145. */
  146. /* computeHeightFromChildren - updates height for a node by checking
  147. * child nodes
  148. * @param current - root of sub-tree whose height needs to be
  149. * updated
  150. */
  151. void computeHeightFromChildren(AVLTreeNode<K,V>* current);
  152. /* balance - updates heights after insert/remove, detects imbalances,
  153. * and invokes rotation to fix an imbalance
  154. * @param current : pointer to the root node of sub-tree to be balanced
  155. * @return AVLTreeNode<K,V>* pointer to root of sub-tree (current if
  156. * no rotations are made)
  157. */
  158. AVLTreeNode<K,V>* balance(AVLTreeNode<K,V>* current);
  159. /* The four rotations needed to fix each of the four possible imbalances
  160. * in an AVLTree
  161. * (1) Right rotation for a left-left imbalance
  162. * (2) Left rotation for a right-right imbalance
  163. * (3) LeftRight rotation for left-right imbalance
  164. * (4) RightLeft rotation for a right-left imbalance
  165. * @param current : pointer to root of sub-tree to be balanced
  166. * @return AVLTreeNode<K,V>* pointer to root of updated sub-tree
  167. */
  168. AVLTreeNode<K,V>* rightRotate(AVLTreeNode<K,V>* current);
  169. AVLTreeNode<K,V>* leftRightRotate(AVLTreeNode<K,V>* current);
  170. AVLTreeNode<K,V>* leftRotate(AVLTreeNode<K,V>* current);
  171. AVLTreeNode<K,V>* rightLeftRotate(AVLTreeNode<K,V>* current);
  172. };
  173. /*****************************************************************************/
  174. /**
  175. * The AVLTreeNode is a templated class that stores data for each node
  176. * in the AVLTree.
  177. */
  178. template <typename K, typename V>
  179. class AVLTreeNode {
  180. private:
  181. K key; // The key stored in this node.
  182. V value; // The value stored in this node.
  183. AVLTreeNode<K,V>* left; // Pointer to this node's left subtree.
  184. AVLTreeNode<K,V>* right; // Pointer to this node's right subtree.
  185. int height;
  186. /* default constructor */
  187. AVLTreeNode();
  188. /* preferred constructor to initialize key and value to given
  189. * parameters; left and right are set to NULL
  190. */
  191. AVLTreeNode(K k, V v);
  192. /* getHeight checks for NULL for you, returning -1*/
  193. int getHeight();
  194. /* indicates that AVLTree is a friend class so that it can directly
  195. * access private aspects of this class.
  196. */
  197. friend class AVLTree<K,V>;
  198. };
  199. //all the public methods are defined here as well as the AVLTreeNode class
  200. #include "AVLTree-inl.h"
  201. //all the private methods are defined here
  202. #include "AVLTree-private-inl.h"
  203. #endif // AVLTREE_H_