Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #ifndef BINARY_SEARCH_TREE_H
  2. #define BINARY_SEARCH_TREE_H
  3.  
  4. #include <algorithm>
  5. #include <ostream>
  6. #include <iostream>
  7. #include <cstdlib>
  8. using namespace std;
  9.  
  10. template <typename Comparable>
  11. class BinarySearchTree
  12. {
  13. public:
  14. BinarySearchTree() : root{ nullptr }
  15. {
  16. }
  17.  
  18. ~BinarySearchTree()
  19. {
  20. makeEmpty();
  21. }
  22.  
  23. bool isEmpty() const
  24. {
  25. return root == nullptr;
  26. }
  27.  
  28. //in-order traversal
  29. void printTree(ostream & out = cout) const
  30. {
  31. if (isEmpty())
  32. out << "Empty tree" << endl;
  33. else
  34. printTree(root, out);
  35. }
  36.  
  37. void makeEmpty()
  38. {
  39. makeEmpty(root);
  40. }
  41.  
  42.  
  43. void insert(const Comparable & x)
  44. {
  45. insert(x, root);
  46. }
  47. void printTreePreOrder(ostream & out = cout) {
  48. printTreePreOrder(root, out);
  49. }
  50. void printTreePostOrder(ostream & out = cout) {
  51. printTreePostOrder(root, out);
  52. }
  53. int height() const {
  54. return height(root);
  55. }
  56. bool isBalanced() const {
  57. return isBalanced(root);
  58. }
  59.  
  60. private:
  61. struct BinaryNode
  62. {
  63. Comparable element;
  64. BinaryNode *left;
  65. BinaryNode *right;
  66.  
  67. BinaryNode(const Comparable & theElement, BinaryNode *lt, BinaryNode *rt)
  68. : element{ theElement }, left{ lt }, right{ rt } { }
  69.  
  70. BinaryNode(Comparable && theElement, BinaryNode *lt, BinaryNode *rt)
  71. : element{ std::move(theElement) }, left{ lt }, right{ rt } { }
  72. };
  73.  
  74. BinaryNode *root;
  75.  
  76.  
  77. void insert(const Comparable & x, BinaryNode * & t)
  78. {
  79. if (t == nullptr)
  80. t = new BinaryNode{ x, nullptr, nullptr };
  81. else if (x < t->element)
  82. insert(x, t->left);
  83. else if (t->element < x)
  84. insert(x, t->right);
  85. else
  86. ; // Duplicate; do nothing
  87. }
  88.  
  89. void makeEmpty(BinaryNode * & t)
  90. {
  91. if (t != nullptr)
  92. {
  93. makeEmpty(t->left);
  94. makeEmpty(t->right);
  95. delete t;
  96. }
  97. t = nullptr;
  98. }
  99.  
  100. //in-order traversal
  101. void printTree(BinaryNode *t, ostream & out) const
  102. {
  103. if (t != nullptr)
  104. {
  105. printTree(t->left, out);
  106. out << t->element << " ";
  107. printTree(t->right, out);
  108. }
  109. }
  110.  
  111. void printTreePreOrder(BinaryNode *t, ostream & out) const {
  112. if (t == nullptr) {
  113. //intentionally empty
  114. }
  115. else {
  116. out << t->element << " ";
  117. printTreePreOrder(t->left, out);
  118. printTreePreOrder(t->right, out);
  119. }
  120. }
  121. void printTreePostOrder(BinaryNode *t, ostream & out) const {
  122. if (t == nullptr) {
  123. //intentionally empty
  124. }
  125. else {
  126. printTreePostOrder(t->left, out);
  127. printTreePostOrder(t->right, out);
  128. out << t->element << " ";
  129. }
  130. }
  131.  
  132. int height(BinaryNode *t) const {
  133. if (t == nullptr) {
  134. return -1;
  135. }
  136. else {
  137. int leftHeight = height(t->left);
  138. int rightHeight = height(t->right);
  139. if (leftHeight > rightHeight) {
  140. return leftHeight + 1;
  141. }
  142. else {
  143. return rightHeight + 1;
  144. }
  145. }
  146. }
  147.  
  148. bool isBalanced(BinaryNode *t) const {
  149. if (t == nullptr) {
  150. return true;
  151. }
  152. if (abs(height(t->left) - height(t->right)) > 1) {
  153. return false;
  154. }
  155. if (!isBalanced(t->left)) {
  156. return false;
  157. }
  158. if (!isBalanced(t->right)) {
  159. return false;
  160. }
  161. return true;
  162. }
  163. };
  164.  
  165. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement