linkedBST.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*Dylan Jeffers
  2. *Tahmid Rahman
  3. *founders of RahmROMJeffers ltd.
  4. */
  5. #ifndef LINKEDBST_H_
  6. #define LINKEDBST_H_
  7. #include "BST.h"
  8. #include "pair.h"
  9. #include "library/queue.h"
  10. #include <stdexcept>
  11. #include <iostream> // get rid of this please - TRUST US FUTURE YOU
  12. // Forward declaration of the BSTNode class, so we can use it in
  13. // LinkedBST before we've defined it. This is like declaring a function
  14. // before defining it
  15. template <typename K, typename V> class BSTNode;
  16. /**
  17. * A LinkedBST is a templated binary search tree, implementing the
  18. * BST interface using linked node structures.
  19. * Most of the externally-accessible LinkedBST functions
  20. * simply call a private recursive methods, that performs the
  21. * required function on a given sub-tree.
  22. * (see BST.h for documentation on inherited functions)
  23. */
  24. template <typename K, typename V>
  25. class LinkedBST : public BST<K,V> {
  26. private:
  27. int size; // Current number of items in the tree.
  28. BSTNode<K,V>* root; // Pointer to the root node (possibly NULL).
  29. public:
  30. LinkedBST();
  31. ~LinkedBST();
  32. /* All public functions defined in BST.h*/
  33. /* These methods are defined in linkedBST-inl.h*/
  34. /* sizing operations */
  35. int getSize();
  36. bool isEmpty();
  37. int getHeight();
  38. /* Key operations */
  39. K getMax();
  40. K getMin();
  41. /* dictionary operations */
  42. void insert (K key, V value);
  43. void update (K key, V value);
  44. bool contains(K key);
  45. void remove (K key);
  46. V find (K key);
  47. /* traversal operations */
  48. Queue< Pair<K,V> >* getPreOrder();
  49. Queue< Pair<K,V> >* getInOrder();
  50. Queue< Pair<K,V> >* getPostOrder();
  51. Queue< Pair<K,V> >* getLevelOrder();
  52. private:
  53. /* Private recursive internal methods that
  54. * correspond to the public methods defined above.
  55. * These methods are defined in linkedBST-private-inl.h
  56. */
  57. /* insertInSubtree - Recursive function that inserts a new node into
  58. * a sub-tree pointed to by current
  59. * @param current : a pointer to the root of the sub-tree
  60. * @param key : the key for the new node being inserted
  61. * @param value : the value for the new node being inserted
  62. * @error runtime_error if the key already exists
  63. * @return BSTNode<K,V>*: the root of the sub-tree. This is for the parent
  64. * node to set it's left or right child. On an actual insert,
  65. * the pointer will be to the newly allocated node. Otherwise,
  66. * the structure hasn't changed and we return a pointer to current
  67. */
  68. BSTNode<K,V>* insertInSubtree (BSTNode<K,V>* current, K key, V value);
  69. /* updateInSubtree - Recursive function that updates a key-value pair
  70. * in the tree
  71. * @param current : pointer to root node of the sub-tree
  72. * @param key : the key being searched for
  73. * @param value : the new value to associate with the given key
  74. * @error runtime_error if key not found
  75. */
  76. void updateInSubtree (BSTNode<K,V>* current, K key, V value);
  77. /* removeFromSubtree - Recursive function that searches for and removes
  78. * the element associated with the given key in the
  79. * sub-tree pointed to by current
  80. * @param current : a pointer to the root of the sub-tree
  81. * @param key : the key for the node to be removed
  82. * @error runtime_error if the key does not exist
  83. * @return BSTNode<K,V>*: the root of the sub-tree. This is for the parent
  84. * node to set it's left or right child
  85. */
  86. BSTNode<K,V>* removeFromSubtree (BSTNode<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 (BSTNode<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 (BSTNode<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 (BSTNode<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 (BSTNode<K,V>* current);
  117. /* getHeightOfSubtree - Recursive function that calculates the height
  118. * of the sub-tree pointed to be current
  119. *
  120. * @param current: pointer to root node of the sub-tree
  121. * @return int : the maximal depth of a node in the subtree; -1 if
  122. * current is NULL
  123. */
  124. int getHeightOfSubtree(BSTNode<K,V>* current);
  125. /* build{Pre,In,Post} - Recursive helper functions for building
  126. * iterators for the data structure.
  127. * Each enqueues all key-value pairs for the
  128. * sub-tree pointed to by current
  129. *
  130. * @param current : pointer to root node of the sub-tree
  131. * @param it : a pointer to the Queue to fill with the key-value
  132. * pairs based on the traversal order
  133. */
  134. void buildPreOrder (BSTNode<K,V>* current, Queue< Pair<K,V> >* it);
  135. void buildInOrder (BSTNode<K,V>* current, Queue< Pair<K,V> >* it);
  136. void buildPostOrder(BSTNode<K,V>* current, Queue< Pair<K,V> >* it);
  137. /* traverseAndDelete - Recursive helper function for the destructor
  138. * Performs a post-order traversal of the sub-tree
  139. * freeing memory for all nodes in the sub-tree
  140. * pointed to by current
  141. * @param current - : pointer to root node of the sub-tree to be deleted
  142. */
  143. void traverseAndDelete (BSTNode<K,V>* current);
  144. };
  145. /*****************************************************************************/
  146. /**
  147. * The BSTNode is a templated class that stores data for each node
  148. * in the LinkedBST.
  149. */
  150. template <typename K, typename V>
  151. class BSTNode {
  152. private:
  153. K key; // The key stored in this node.
  154. V value; // The value stored in this node.
  155. BSTNode<K,V>* left; // Pointer to this node's left subtree.
  156. BSTNode<K,V>* right; // Pointer to this node's right subtree.
  157. /* default constructor */
  158. BSTNode();
  159. /* preferred constructor to initialize key and value to given
  160. * parameters; left and right are set to NULL
  161. */
  162. BSTNode(K k, V v);
  163. /* indicates that LinkedBST is a friend class so that it can directly
  164. * access private aspects of this class.
  165. * friend is a C++ keyword that allows another function or class to
  166. * access all features. Note that without this, we cannot actually
  167. * create a BSTNode since the constructors are private
  168. */
  169. friend class LinkedBST<K,V>;
  170. };
  171. //all the public methods are defined here as well as the BSTNode class
  172. #include "linkedBST-inl.h"
  173. //all the private methods are defined here
  174. #include "linkedBST-private-inl.h"
  175. #endif // LINKEDBST_H_