AVLTree-inl.h 7.4 KB

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