Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. template <class T>
  6. struct Node {
  7. // ##data memebers##
  8. T value;
  9. Node *left;
  10. Node *right;
  11.  
  12. // ##constructors##
  13. Node(T val) {
  14. this->value = val;
  15. }
  16.  
  17. Node(T val, Node<T> left, Node<T> right) {
  18. this->value = val;
  19. this->left = left;
  20. this->right = right;
  21. }
  22. };
  23.  
  24. template <class T>
  25. class BST {
  26. private:
  27. // ## data members ##
  28. Node<T> *root;
  29.  
  30. // ## private access methods ##
  31.  
  32. //add a new node starting at root
  33. void addHelper(Node<T> *root, T val) {
  34. if (root->value > val) {
  35. if (!root->left) {
  36. root->left = new Node<T>(val);
  37. } else {
  38. addHelper(root->left, val);
  39. }
  40. } else {
  41. if (!root->right) {
  42. root->right = new Node<T>(val);
  43. } else {
  44. addHelper(root->right, val);
  45. }
  46. }
  47. }
  48.  
  49. //print tree using In-Order traversal
  50. //left - parent - right
  51. void inOrderPrint(Node<T> *root) {
  52. //TODO create this method
  53. }
  54.  
  55. //print tree using Post-Order traversal
  56. //parent - left - right
  57. void postOrderPrint(Node<T> *root) {
  58. //TODO create this method
  59. }
  60.  
  61. //count nodes in tree
  62. int nodesCountHelper(Node<T> *root) {
  63. if (!root) return 0;
  64. else return 1 + nodesCountHelper(root->left) + nodesCountHelper(root->right);
  65. }
  66.  
  67. //get height of tree
  68. int heightHelper(Node<T> *root) {
  69. if (!root) return 0;
  70. else return 1 + max(heightHelper(root->left), heightHelper(root->right));
  71. }
  72.  
  73. //return longest path
  74. void printMaxPathHelper(Node<T> *root) {
  75. if (!root) return;
  76. cout<<root->value<<' ';
  77. if (heightHelper(root->left) > heightHelper(root->right)) {
  78. printMaxPathHelper(root->left);
  79. } else {
  80. printMaxPathHelper(root->right);
  81. }
  82. }
  83.  
  84. //delete a value from true by providing NULL, ROOT, VAL
  85. //traverse tree, remove node, and fix pointers
  86. //return false if value is not found
  87. bool deleteValueHelper(Node<T>* parent, Node<T>* current, T value) {
  88. if (!current) return false;
  89. if (current->value == value) {
  90. if (current->left == NULL || current->right == NULL) {
  91. Node<T>* temp = current->left;
  92. if (current->right) temp = current->right;
  93. if (parent) {
  94. if (parent->left == current) {
  95. parent->left = temp;
  96. } else {
  97. parent->right = temp;
  98. }
  99. } else {
  100. this->root = temp;
  101. }
  102. } else {
  103. Node<T>* validSubs = current->right;
  104. while (validSubs->left) {
  105. validSubs = validSubs->left;
  106. }
  107. T temp = current->value;
  108. current->value = validSubs->value;
  109. validSubs->value = temp;
  110. return deleteValueHelper(current, current->right, temp);
  111. }
  112. delete current;
  113. return true;
  114. }
  115. return deleteValueHelper(current, current->left, value) ||
  116. deleteValueHelper(current, current->right, value);
  117. }
  118.  
  119. public:
  120. //Public access methods
  121. //Use default constructor
  122.  
  123. //add value to tree
  124. void add(T val) {
  125. if (root) {
  126. this->addHelper(root, val);
  127. } else {
  128. root = new Node<T>(val);
  129. }
  130. }
  131.  
  132. void printInOrder() {
  133. //TODO create this method
  134. }
  135.  
  136. void printPostOrder() {
  137. //TODO create this method
  138. }
  139.  
  140. int nodesCount() {
  141. return nodesCountHelper(root);
  142. }
  143.  
  144. void printNodeCount() {
  145. //TODO create this method
  146. }
  147.  
  148. int height() {
  149. return heightHelper(this->root);
  150. }
  151.  
  152. void printHeight() {
  153. //TODO create this method
  154. }
  155.  
  156. void printMaxPath() {
  157. printMaxPathHelper(this->root);
  158. }
  159.  
  160. bool deleteValue(T value) {
  161. return this->deleteValueHelper(NULL, this->root, value);
  162. }
  163. };
  164.  
  165. int main() {
  166. //create binary search tree
  167. BST<int> *bst = new BST<int>();
  168.  
  169. //array of values to add to tree
  170. int vals [6] = {11, 1, 6, -1, -10, 100};
  171.  
  172. //add values
  173. for(const int &val : vals) {
  174. bst->add(val);
  175. }
  176.  
  177. bst->printInOrder();
  178. bst->printPostOrder();
  179. bst->printNodeCount();
  180. bst->printHeight();
  181. bst->printMaxPath();
  182.  
  183. //remove values
  184. for(const int &val : vals) {
  185. bst->deleteValue(val);
  186. std::cout << val << " removed: ";
  187. bst->printInOrder();
  188. }
  189. return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement