AVLTree.h 8.8 KB

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