Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. /*
  2. * Binary Search Tree implementation
  3. * - heavily lifted from https://gist.github.com/mgechev/5911348
  4. *
  5. * Simple integer keys and basic operations BST
  6. *
  7. * Aaron Crandall - 2016 - Added / updated:
  8. * * Inorder, Preorder, Postorder printouts
  9. * * Stubbed in level order printout
  10. *
  11. */
  12.  
  13. #include <iostream>
  14. #include <queue>
  15. using namespace std;
  16.  
  17. /*
  18. * Node datastructure for single tree node
  19. */
  20. template <class T>
  21. struct Node {
  22. T value;
  23. Node *left;
  24. Node *right;
  25.  
  26. Node(T val) {
  27. this->value = val;
  28. this ->left = nullptr;
  29. this ->right = nullptr;
  30. }
  31.  
  32. Node(T val, Node<T> left, Node<T> right) {
  33. this->value = val;
  34. this->left = nullptr;
  35. this->right = nullptr;
  36. }
  37. };
  38.  
  39. /*
  40. * Binary Search Tree (BST) class implementation
  41. */
  42. template <class T>
  43. class BST {
  44.  
  45. private:
  46. Node<T> *root;
  47.  
  48. void addHelper(Node<T> *root, T val) {
  49. if (root->value > val) {
  50. if (!root->left) {
  51. root->left = new Node<T>(val);
  52. } else {
  53. addHelper(root->left, val);
  54. }
  55. } else {
  56. if (!root->right) {
  57. root->right = new Node<T>(val);
  58. } else {
  59. addHelper(root->right, val);
  60. }
  61. }
  62. }
  63.  
  64. void printInOrderHelper(Node<T> *root) {
  65. if (!root) return;
  66. printInOrderHelper(root->left);
  67. cout<<root->value<<' ';
  68. printInOrderHelper(root->right);
  69. }
  70.  
  71. void printPostOrderHelper(Node<T> *root) {
  72. if (!root) return;
  73. printPostOrderHelper(root->left);
  74. printPostOrderHelper(root->right);
  75. cout<<root->value<<' ';
  76. }
  77.  
  78. void printPreOrderHelper(Node<T> *root) {
  79. if (!root) return;
  80. cout<<root->value<<' ';
  81. printPreOrderHelper(root->left);
  82. printPreOrderHelper(root->right);
  83. }
  84.  
  85. void printLevelOrderHelper(Node<T> *root) {
  86. if (!root) return;
  87. std::queue pLevel; //print level
  88.  
  89.  
  90. }
  91.  
  92. int nodesCountHelper(Node<T> *root) {
  93. if (!root) return 0;
  94. else return 1 + nodesCountHelper(root->left) + nodesCountHelper(root->right);
  95. }
  96.  
  97. int heightHelper(Node<T> *root) {
  98. if (!root) return 0;
  99. else return 1 + max(heightHelper(root->left), heightHelper(root->right));
  100. }
  101.  
  102. void printMaxPathHelper(Node<T> *root) {
  103. if (!root) return;
  104. cout<<root->value<<' ';
  105. if (heightHelper(root->left) > heightHelper(root->right)) {
  106. printMaxPathHelper(root->left);
  107. } else {
  108. printMaxPathHelper(root->right);
  109. }
  110. }
  111.  
  112. bool deleteValueHelper(Node<T>* parent, Node<T>* current, T value) {
  113. if (!current) return false;
  114. if (current->value == value) {
  115. if (current->left == NULL || current->right == NULL) {
  116. Node<T>* temp = current->left;
  117. if (current->right) temp = current->right;
  118. if (parent) {
  119. if (parent->left == current) {
  120. parent->left = temp;
  121. } else {
  122. parent->right = temp;
  123. }
  124. } else {
  125. this->root = temp;
  126. }
  127. } else {
  128. Node<T>* validSubs = current->right;
  129. while (validSubs->left) {
  130. validSubs = validSubs->left;
  131. }
  132. T temp = current->value;
  133. current->value = validSubs->value;
  134. validSubs->value = temp;
  135. return deleteValueHelper(current, current->right, temp);
  136. }
  137. delete current;
  138. return true;
  139. }
  140. return deleteValueHelper(current, current->left, value) ||
  141. deleteValueHelper(current, current->right, value);
  142. }
  143.  
  144. public:
  145. void add(T val) {
  146. if (root) {
  147. this->addHelper(root, val);
  148. } else {
  149. root = new Node<T>(val);
  150. }
  151. }
  152.  
  153. void print() {
  154. printInOrderHelper(this->root);
  155. }
  156.  
  157. void printInOrder() {
  158. printInOrderHelper(this->root);
  159. }
  160.  
  161. void printPostOrder() {
  162. printPostOrderHelper(this->root);
  163. }
  164.  
  165. void printPreOrder() {
  166. printPreOrderHelper(this->root);
  167. }
  168.  
  169. void printLevelOrder() {
  170. printLevelOrderHelper(this->root);
  171. }
  172.  
  173. int nodesCount() {
  174. return nodesCountHelper(root);
  175. }
  176.  
  177. int height() {
  178. return heightHelper(this->root);
  179. }
  180.  
  181. void printMaxPath() {
  182. printMaxPathHelper(this->root);
  183. }
  184.  
  185. bool deleteValue(T value) {
  186. return this->deleteValueHelper(NULL, this->root, value);
  187. }
  188. };
  189.  
  190.  
  191. int main() {
  192. BST<int> *bst = new BST<int>();
  193. bst->add(4);
  194. bst->add(2);
  195. bst->add(1);
  196. bst->add(3);
  197. bst->add(6);
  198. bst->add(5);
  199. bst->add(7);
  200. bst->add(9);
  201. bst->add(8);
  202.  
  203.  
  204. cout << "Inorder: \t";
  205. bst->printInOrder();
  206. cout << endl;
  207.  
  208. cout << "Postorder: \t";
  209. bst->printPostOrder();
  210. cout << endl;
  211.  
  212. cout << "Preorder: \t";
  213. bst->printPreOrder();
  214. cout << endl;
  215.  
  216. cout << "Levelorder: \t";
  217. bst->printLevelOrder();
  218. cout << endl;
  219. cout << "Should produce:\t4 2 6 1 3 5 7 9 8" << endl;
  220.  
  221.  
  222. cout<<endl;
  223. cout<<"Nodes count: "<<bst->nodesCount();
  224. cout<<endl;
  225. cout<<"Height: "<<bst->height();
  226. cout<<endl;
  227. cout<<"Max path: ";
  228. bst->printMaxPath();
  229. cout<<endl;
  230. return 0;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement