linkedBST-inl.h 7.0 KB

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