Advertisement
lryu1

Untitled

Oct 5th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. #ifndef BST_HPP
  2. #define BST_HPP
  3. #include "BSTNode.hpp"
  4. #include "BSTIterator.hpp"
  5. #include <iostream>
  6.  
  7. template<typename Data>
  8. class BST {
  9.  
  10. protected:
  11.  
  12. /** Pointer to the root of this BST, or 0 if the BST is empty */
  13. BSTNode<Data>* root;
  14.  
  15. /** Number of Data items stored in this BST. */
  16. unsigned int isize;
  17.  
  18. public:
  19.  
  20. /** define iterator as an aliased typename for BSTIterator<Data>. */
  21. typedef BSTIterator<Data> iterator;
  22.  
  23. /** Default constructor.
  24. Initialize an empty BST.
  25. */
  26. BST() : root(0), isize(0) { }
  27.  
  28.  
  29. /** Default destructor.
  30. Delete every node in this BST.
  31. */
  32. delete root;
  33. virtual ~BST() {
  34.  
  35. }
  36.  
  37. /** Given a reference to a Data item, insert a copy of it in this BST.
  38. * Return true if the item was added to this BST
  39. * as a result of this call to insert,
  40. * false if an item equal to this one was already in this BST.
  41. * Note: This function should use only the '<' operator when comparing
  42. * Data items. (should not use >, <=, >=)
  43. */ // TODO
  44. virtual bool insert(const Data& item) {
  45. // create new current node for inserted node
  46. BSTNode<Data> currNode = root;
  47. int test = 0;
  48. // check if there are existing nodes in the tree
  49. if (root == nullptr)
  50. { root = new BSTNode<item>;
  51. return true; }
  52. else {
  53. // compare current node to existing nodes
  54. while (currNode && test == 0) {
  55. if (item < currNode->data) {
  56. currNode = currNode->left;
  57. if (currNode->left == nullptr)
  58. currNode->left->parent = currNode;
  59. isize++;
  60. return true;
  61. test = 1;
  62. }
  63.  
  64. if (currNode->data < item) {
  65. currNode = currNode.right;
  66. if (currNode->right == nullptr)
  67. currNode->right->parent = currNode;
  68. isize++;
  69. return true;
  70. test = 1;
  71. }
  72. }
  73.  
  74. return false;
  75.  
  76.  
  77. }
  78.  
  79.  
  80.  
  81. }
  82.  
  83.  
  84. /** Find a Data item in the BST.
  85. * Return an iterator pointing to the item, or pointing past
  86. * the last node in the BST if not found.
  87. * Note: This function should use only the '<' operator when comparing
  88. * Data items. (should not use >, <=, >=)
  89. */ // TODO
  90. iterator find(const Data& item) const {
  91. if (root == null) return false;
  92.  
  93. else {
  94. BSTNode<Data> currNode = root;
  95.  
  96.  
  97. }
  98.  
  99.  
  100. /** Return the number of items currently in the BST.
  101. */ // TODO
  102. unsigned int size() const {
  103. return 0;
  104. }
  105.  
  106. /** Return true if the BST is empty, else false.
  107. */ // TODO
  108. bool empty() const {
  109. return 0;
  110.  
  111. }
  112.  
  113. /** Empties the Tree
  114. */ // TODO
  115. bool clear(){
  116. return 0;
  117.  
  118. }
  119.  
  120. /** Return an iterator pointing to the first item in the BST (not the root).
  121. */ // TODO
  122. iterator begin() const {
  123. return 0;
  124.  
  125. }
  126.  
  127. /** Return an iterator pointing past the last item in the BST.
  128. */
  129. iterator end() const {
  130. return typename BST<Data>::iterator(0);
  131. }
  132.  
  133. /** Perform an inorder traversal of this BST.
  134. */
  135. void inorder() const {
  136. inorder(root);
  137. }
  138.  
  139.  
  140. private:
  141.  
  142. /** Recursive inorder traversal 'helper' function */
  143.  
  144. /** Inorder traverse BST, print out the data of each node in ascending order.
  145. Implementing inorder and deleteAll base on the pseudo code is an easy way to get started.
  146. */ // TODO
  147. void inorder(BSTNode<Data>* n) const {
  148. /* Pseudo Code:
  149. if current node is null: return;
  150. recursively traverse left sub-tree
  151. print current node data
  152. recursively traverse right sub-tree
  153. */
  154. }
  155.  
  156. /** Find the first element of the BST
  157. */
  158. static BSTNode<Data>* first(BSTNode<Data>* root) {
  159. if(root == 0) return 0;
  160. while(root->left != 0) root = root->left;
  161. return root;
  162. }
  163.  
  164. /** do a postorder traversal, deleting nodes
  165. */ // TODO
  166. static void deleteAll(BSTNode<Data>* n) {
  167. return 0;
  168. /* Pseudo Code:
  169. if current node is null: return;
  170. recursively delete left sub-tree
  171. recursively delete right sub-tree
  172. delete current node
  173. */
  174. }
  175.  
  176.  
  177. };
  178.  
  179.  
  180. #endif //BST_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement