AVLTree-inl.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*Dylan Jeffers
  2. *Tahmid Rahman
  3. *founders of RahmROMJeffers ltd.
  4. */
  5. #include <stdexcept>
  6. #include "library/arrayQueue.h"
  7. ////////////////////////////////////////////////////////////////////////////
  8. /////////////////////////////AVLTREE NODE IMPLEMENTATION////////////////////////
  9. ////////////////////////////////////////////////////////////////////////////
  10. /**
  11. * Default constructor for the AVLTreeNode class.
  12. * Does not set the key-value pair, and initializes the subtrees to NULL.
  13. */
  14. template <typename K, typename V>
  15. AVLTreeNode<K,V>::AVLTreeNode() {
  16. height = -1; //An empty tree has height -1
  17. left = NULL;
  18. right = NULL;
  19. }
  20. /**
  21. * Standard constructor for the AVLTreeNode class.
  22. * Stores the given key-value pair and initializes the subtrees to NULL.
  23. * @param k - key for the element (index in the overall BST)
  24. * @param v - value for the element
  25. */
  26. template <typename K, typename V>
  27. AVLTreeNode<K,V>::AVLTreeNode(K k, V v) {
  28. key = k;
  29. value = v;
  30. height = 0;
  31. left = NULL;
  32. right = NULL;
  33. }
  34. ////////////////////////////////////////////////////////////////////////////
  35. ////////////////////////////AVLTree IMPLEMENTATION////////////////////////
  36. ////////////////////////////////////////////////////////////////////////////
  37. /**
  38. * Standard constructor for the AVLTree class.
  39. * Constructs an empty tree (size 0, with a NULL root).
  40. */
  41. template <typename K, typename V>
  42. AVLTree<K,V>::AVLTree() {
  43. size = 0;
  44. root = NULL;
  45. }
  46. /**
  47. * The AVLTree destructor uses a recursive helper function which
  48. * traverses the tree and frees each node on the heap as it traverses.
  49. */
  50. template <typename K, typename V>
  51. AVLTree<K,V>::~AVLTree() {
  52. traverseAndDelete(root);
  53. }
  54. /* getSize - returns the size of the BST
  55. * @return int: the number of key-value pairs in the data structure
  56. */
  57. template <typename K, typename V>
  58. int AVLTree<K,V>::getSize() {
  59. return size;
  60. }
  61. /* isEmpty - returns true if the tree is empty
  62. * @return bool: true if there are no elements in the BST
  63. */
  64. template <typename K, typename V>
  65. bool AVLTree<K,V>::isEmpty() {
  66. return size == 0;
  67. }
  68. /* isBalanced- returns true if the tree is balanced
  69. * @return bool: true if the tree is balanced.
  70. */
  71. template <typename K, typename V>
  72. bool AVLTree<K,V>::isBalanced() {
  73. return isBalancedInSubtree(root);
  74. }
  75. /* insert - inserts the key-value pair into the tree
  76. * @param key - key for indexing the new element
  77. * @param value - value associated with the given key
  78. * @error runtime_error if they key already exists
  79. */
  80. template <typename K, typename V>
  81. void AVLTree<K,V>::insert(K key, V value) {
  82. root = insertInSubtree(root, key, value);
  83. }
  84. /* update - finds the element indexed by the given key and updates
  85. * its value to the provided value parameter
  86. * @param key - key for finding the existing element
  87. * @param value - the new value to store for the given key
  88. * @error runtime_error if the key is not found in the BST
  89. */
  90. template <typename K, typename V>
  91. void AVLTree<K,V>::update(K key, V value){
  92. updateInSubtree(root, key, value);
  93. }
  94. /* remove - deletes the element with given key from the tree
  95. * @param key - index key to search for and remove
  96. * @error: runtime_error if they key is not found
  97. */
  98. template <typename K, typename V>
  99. void AVLTree<K,V>::remove(K key) {
  100. root = removeFromSubtree(root, key);
  101. }
  102. /* contains - returns true if there exists an element in the BST
  103. * with the given key
  104. * @param key - index key to search for
  105. * @return bool: true if the given key exists in the BST
  106. */
  107. template <typename K, typename V>
  108. bool AVLTree<K,V>::contains(K key) {
  109. return containsInSubtree(root, key);
  110. }
  111. /* find - returns the value associated with the given key
  112. * @param key - index key for element to find
  113. * @error runtime_error if the key is not found in the BST
  114. * @return V: value associated with given key
  115. */
  116. template <typename K, typename V>
  117. V AVLTree<K,V>::find(K key) {
  118. return findInSubtree(root, key);
  119. }
  120. /* getMin - returns the smallest key in the data structure
  121. * @error runtime_error if BST is empty
  122. * @return K: minimum key in the BST
  123. */
  124. template <typename K, typename V>
  125. K AVLTree<K,V>::getMin() {
  126. if (isEmpty()) {
  127. throw std::runtime_error("AVLTree::getMin called on an empty tree.");
  128. }
  129. return getMinInSubtree(root);
  130. }
  131. /* getMax - returns the largest key in the data structure
  132. * @error runtime_error if BST is empty
  133. * @return K: maximum key in the BST
  134. */
  135. template <typename K, typename V>
  136. K AVLTree<K,V>::getMax() {
  137. if (isEmpty()) {
  138. throw std::runtime_error("AVLTree::getMax called on an empty tree.");
  139. }
  140. return getMaxInSubtree(root);
  141. }
  142. /* getHeight - returns a height for the tree (i.e., largest
  143. * depth for any leaf node)
  144. * @return int: height of tree, -1 if tree is empty
  145. */
  146. template <typename K, typename V>
  147. int AVLTree<K,V>::getHeight() {
  148. if(root == NULL){
  149. return -1;
  150. } else{
  151. return root->height;
  152. }
  153. }
  154. /* getPreOrder - returns a pointer to an iterator (a queue here) containing
  155. * all key-value pairs in the data structure. Uses a pre-order
  156. * traversal to obtain all elements
  157. * @return Queue< Pair<K,V>>*: a pointer to a dynamically allocated
  158. * Queue with key-value pairs. The caller is responsible for handling
  159. * the heap memory deallocation
  160. */
  161. template <typename K, typename V>
  162. Queue< Pair<K,V> >* AVLTree<K,V>::getPreOrder() {
  163. Queue< Pair<K,V> >* it = new ArrayQueue< Pair<K,V> >();
  164. buildPreOrder(root, it);
  165. return it;
  166. }
  167. /* getInOrder - returns a pointer to an iterator (a queue here) containing
  168. * all key-value pairs in the data structure. Uses an in-order
  169. * traversal to obtain all elements
  170. * @return Queue< Pair<K,V>>*: a pointer to a dynamically allocated
  171. * Queue with key-value pairs. The caller is responsible for handling
  172. * the heap memory deallocation
  173. */
  174. template <typename K, typename V>
  175. Queue< Pair<K,V> >* AVLTree<K,V>::getInOrder() {
  176. Queue< Pair<K,V> >* it = new ArrayQueue< Pair<K,V> >();
  177. buildInOrder(root, it);
  178. return it;
  179. }
  180. /* getPostOrder - returns a pointer to an iterator (a queue here) containing
  181. * all key-value pairs in the data structure. Uses a post-order
  182. * traversal to obtain all elements
  183. * @return Queue< Pair<K,V>>*: a pointer to a dynamically allocated
  184. * Queue with key-value pairs. The caller is responsible for handling
  185. * the heap memory deallocation
  186. */
  187. template <typename K, typename V>
  188. Queue< Pair<K,V> >* AVLTree<K,V>::getPostOrder() {
  189. Queue< Pair<K,V> >* it = new ArrayQueue< Pair<K,V> >();
  190. buildPostOrder(root, it);
  191. return it;
  192. }
  193. /* getLevelOrder - returns a pointer to an iterator (a queue here) containing
  194. * all key-value pairs in the data structure. Uses a level-order
  195. * traversal to obtain all elements
  196. * @return Queue< Pair<K,V>>*: a pointer to a dynamically allocated
  197. * Queue with key-value pairs. The caller is responsible for handling
  198. * the heap memory deallocation
  199. */
  200. template <typename K, typename V>
  201. Queue< Pair<K,V> >* AVLTree<K,V>::getLevelOrder() {
  202. ArrayQueue< AVLTreeNode<K,V>* > levelQ;
  203. Queue< Pair<K,V> >* it = new ArrayQueue< Pair<K,V> >();
  204. levelQ.enqueue(root);
  205. while (!levelQ.isEmpty()) {
  206. AVLTreeNode<K,V>* current = levelQ.dequeue();
  207. if (current != NULL) {
  208. it->enqueue( Pair<K,V>(current->key, current->value) );
  209. levelQ.enqueue(current->left);
  210. levelQ.enqueue(current->right);
  211. }
  212. }
  213. return it;
  214. }