linkedBST-private-inl.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*Dylan Jeffers
  2. *Tahmid Rahman
  3. *founders of RahmROMJeffers ltd.
  4. */
  5. /*
  6. * This recursive helper function inserts a key-value pair into a subtree
  7. * of the tree, or throws a runtime_error if the key is already present.
  8. */
  9. template <typename K, typename V>
  10. BSTNode<K,V>*
  11. LinkedBST<K,V>::insertInSubtree(BSTNode<K,V>* current, K key, V value) {
  12. if (current == NULL){
  13. size++;
  14. return new BSTNode<K, V>(key, value);
  15. }
  16. else if (key == current->key){
  17. throw std::runtime_error("LinkedBST::insertInSubtree" \
  18. "called on key already in tree.");
  19. }
  20. else if (key < current->key){
  21. current->left = insertInSubtree(current->left, key, value);
  22. }
  23. else if (key > current->key){
  24. current->right = insertInSubtree(current->right, key, value);
  25. }
  26. return current;
  27. }
  28. /**
  29. * This recursive helper function updates key-value pair in the subtree
  30. * of the tree, or throws a runtime_error if the key is not present.
  31. */
  32. template <typename K, typename V>
  33. void LinkedBST<K,V>::updateInSubtree(BSTNode<K,V>* current, K key, V value) {
  34. if (current == NULL){
  35. throw std::runtime_error("Key not found in LinkedBST::updateInSubtree.");
  36. }
  37. else if (key == current->key){
  38. current->value = value;
  39. }
  40. else if (key < current->key){
  41. updateInSubtree(current->left, key, value);
  42. }
  43. else if (key > current->key){
  44. updateInSubtree(current->right, key, value);
  45. }
  46. return;
  47. }
  48. /**
  49. * This recursive helper function removes a key-value pair from a subtree
  50. * of the tree, or throws a runtime_error if that key was not present.
  51. *
  52. * It returns a pointer to the root of the subtree. This root is often
  53. * the node that was passed as an argument to the function (current) but
  54. * might be a different node if current contains the key we are removing
  55. * from the tree.
  56. */
  57. template <typename K, typename V>
  58. BSTNode<K,V>*
  59. LinkedBST<K,V>::removeFromSubtree(BSTNode<K,V>* current,
  60. K key) {
  61. if (current == NULL) {
  62. throw std::runtime_error("LinkedBST::remove called on key not in tree.");
  63. }
  64. else if (key == current->key) { // We've found the node to remove
  65. if ((current->left == NULL) && (current->right == NULL)) {
  66. size--;
  67. delete current;
  68. return NULL;
  69. }
  70. else if (current->left == NULL) {
  71. BSTNode<K,V>* tempNode = current->right;
  72. delete current;
  73. size--;
  74. return tempNode;
  75. }
  76. else if (current->right == NULL) {
  77. BSTNode<K,V>* tempNode = current->left;
  78. delete current;
  79. size--;
  80. return tempNode;
  81. }
  82. else {
  83. BSTNode<K,V>* minimum = current->right;
  84. while (minimum->left != NULL) {
  85. minimum = minimum->left;
  86. }
  87. current->key = minimum->key;
  88. current->value = minimum->value;
  89. current->right = removeFromSubtree(current->right, current->key);
  90. }
  91. }
  92. else if (key < current->key) {
  93. current->left = removeFromSubtree(current->left, key);
  94. }
  95. else {
  96. current->right = removeFromSubtree(current->right, key);
  97. }
  98. return current;
  99. }
  100. /**
  101. * Returns true if a key is contained in a subtree of the tree, and
  102. * false otherwise.
  103. */
  104. template <typename K, typename V>
  105. bool LinkedBST<K,V>::containsInSubtree(BSTNode<K,V>* current, K key) {
  106. if (current == NULL){
  107. return false;
  108. }
  109. else if (key == current->key){
  110. return true;
  111. }
  112. else if (key < current->key){
  113. return containsInSubtree(current->left, key);
  114. }
  115. else {
  116. return containsInSubtree(current->right, key);
  117. }
  118. }
  119. /**
  120. * Given a key, returns the value for that key from a subtree of the tree.
  121. * Throws a runtime_error if the key is not in the subtree.
  122. */
  123. template <typename K, typename V>
  124. V LinkedBST<K,V>::findInSubtree(BSTNode<K,V>* current, K key) {
  125. if (current == NULL) {
  126. throw std::runtime_error("LinkedBS::findInSubtree called on an empty tree");
  127. }
  128. else if (key == current->key) {
  129. return current->value;
  130. }
  131. else if (key < current->key) {
  132. return findInSubtree(current->left, key);
  133. }
  134. else {
  135. return findInSubtree(current->right, key);
  136. }
  137. }
  138. /**
  139. * Returns the largest key in a subtree of the tree.
  140. */
  141. template <typename K, typename V>
  142. K LinkedBST<K,V>::getMaxInSubtree(BSTNode<K,V>* current) {
  143. if (current->right == NULL) {
  144. return current->key;
  145. }
  146. return getMaxInSubtree(current->right);
  147. }
  148. /**
  149. * Returns the smallest key in a subtree of the tree.
  150. */
  151. template <typename K, typename V>
  152. K LinkedBST<K,V>::getMinInSubtree(BSTNode<K,V>* current) {
  153. if (current->left == NULL) {
  154. return current->key;
  155. }
  156. return getMinInSubtree(current->left);
  157. }
  158. /**
  159. * Returns the height of a subtree of the tree, or -1 if the subtree
  160. * is empty.
  161. */
  162. template <typename K, typename V>
  163. int LinkedBST<K,V>::getHeightOfSubtree(BSTNode<K,V>* current) {
  164. if (current == NULL) {
  165. return -1;
  166. }
  167. int l = getHeightOfSubtree(current->left);
  168. int r = getHeightOfSubtree(current->right);
  169. if (l >= r) {
  170. return ++l;
  171. }
  172. else
  173. return ++r;
  174. }
  175. /**
  176. * Recursively builds a post-order iterator for a subtree of the tree.
  177. */
  178. template <typename K, typename V>
  179. void LinkedBST<K,V>::buildPostOrder(BSTNode<K,V>* current,
  180. Queue< Pair<K,V> >* it) {
  181. if (current == NULL) {
  182. return;
  183. }
  184. buildPostOrder(current->left, it);
  185. buildPostOrder(current->right, it);
  186. it->enqueue( Pair<K,V>(current->key, current->value) );
  187. }
  188. /**
  189. * Recursively builds a pre-order iterator for a subtree of the tree.
  190. */
  191. template <typename K, typename V>
  192. void LinkedBST<K,V>::buildPreOrder(BSTNode<K,V>* current,
  193. Queue< Pair<K,V> >* it) {
  194. if (current == NULL){
  195. return;
  196. }
  197. it->enqueue( Pair<K,V>(current->key, current->value) );
  198. buildPreOrder(current->left, it);
  199. buildPreOrder(current->right, it);
  200. }
  201. /**
  202. * Recursively builds an in-order iterator for a subtree of the tree.
  203. */
  204. template <typename K, typename V>
  205. void LinkedBST<K,V>::buildInOrder(BSTNode<K,V>* current,
  206. Queue< Pair<K,V> >* it) {
  207. if (current == NULL){
  208. return;
  209. }
  210. buildInOrder(current->left, it);
  211. it->enqueue( Pair<K,V>(current->key, current->value) );
  212. buildInOrder(current->right, it);
  213. }
  214. /**
  215. * Performs a post-order traversal of the tree, deleting each node from the
  216. * heap after we have already traversed its children.
  217. */
  218. template <typename K, typename V>
  219. void LinkedBST<K,V>::traverseAndDelete(BSTNode<K,V>* current) {
  220. if (current == NULL) {
  221. return; //nothing to delete
  222. }
  223. traverseAndDelete(current->left);
  224. traverseAndDelete(current->right);
  225. delete current;
  226. }