Guest User

Untitled

a guest
Jul 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. template <class T>
  8. class BSTree {
  9. template <class T1>
  10. struct Node {
  11. T key;
  12. Node* left;
  13. Node* right;
  14. };
  15.  
  16. private:
  17. Node<T>* root; /// a root of node pointer
  18. void removeSubtree(Node<T>* ptr);
  19. Node<T>* createLeafPrivate(T key);
  20. void addLeafPrivate(T key, Node<T>* ptr);
  21. void addLeafPrivate(T key, Node<T>* ptr, int(*compareTwo)(const T& newData, const T& node)); //compare two objects with callback
  22. void findInOrderCallbackPrivate(Node<T>* ptr, void(*callback)(T & data));
  23. void traverse(int order, Node<T>* root);
  24. void removeNodePrivate(T key, Node<T>* ptr);
  25. void removeRootMatch();
  26. void removeMatch(Node<T>* parent, Node<T>* match, bool left);
  27.  
  28. public:
  29. BSTree();
  30. ~BSTree();
  31. void addLeaf(T key);
  32. void addLeaf(T key, int(*compareTwo)(const T& newData, const T& node));
  33. void findInOrderCallback(void(*callback)(T & data));
  34. T returnRootKey();
  35. void removeNode(T key);
  36. void traverse(int order);
  37. };
  38.  
  39. template<class T>
  40. BSTree<T>::BSTree() {
  41. root = NULL;
  42. }
  43.  
  44. template<class T>
  45. BSTree<T>::~BSTree()
  46. {
  47. //
  48. }
  49.  
  50. template<class T>
  51. void BSTree<T>::removeSubtree(Node<T> * ptr)
  52. {
  53. if (root != NULL) {
  54. if (ptr->left != NULL) {
  55. removeSubtree(ptr->left);
  56. }
  57. if (ptr->right != NULL) {
  58. removeSubtree(ptr->right);
  59. }
  60. delete ptr;
  61. }
  62. }
  63.  
  64. template<class T>
  65. BSTree<T>::Node<T>* BSTree<T>::createLeafPrivate(T key) {
  66. Node<T>* n = new Node<T>;
  67. n->key = key;
  68. n->left = nullptr;
  69. n->right = nullptr;
  70. return n;
  71. }
  72.  
  73. template<class T>
  74. void BSTree<T>::addLeafPrivate(T key, Node<T>* ptr) {
  75. if (ptr == NULL)
  76. root = createLeafPrivate(key);
  77. else if (key < ptr->key) {
  78. if (ptr->left != 0) {
  79. addLeafPrivate(key, ptr->left);
  80. }
  81. else {
  82. ptr->left = createLeafPrivate(key);
  83. }
  84. }
  85. else if (key > ptr->key) {
  86. if (ptr->right != 0) {
  87. addLeafPrivate(key, ptr->right);
  88. }
  89. else {
  90. ptr->right = createLeafPrivate(key);
  91. }
  92. }
  93. else {
  94. cout << "The value " << key << " has already added to the tree";
  95. }
  96. }
  97.  
  98. template<class T>
  99. void BSTree<T>::addLeafPrivate(T key, Node<T>* ptr, int(*compareTwo)(const T &newData, const T &node))
  100. {
  101. if (ptr == NULL) {
  102. root = createLeafPrivate(key);
  103. }
  104. else if (compareTwo(key, ptr->key) == -1) {
  105. if (ptr->left != NULL)
  106. addLeafPrivate(key, ptr->left, compareTwo);
  107. else
  108. ptr->left = createLeafPrivate(key);
  109. }
  110. else if (compareTwo(key, ptr->key) == 1) {
  111. if (ptr->right != NULL)
  112. addLeafPrivate(key, ptr->right, compareTwo);
  113. else
  114. ptr->right = createLeafPrivate(key);
  115. }
  116. else
  117. return;
  118. }
  119.  
  120. template<class T>
  121. void BSTree<T>::addLeaf(T key) {
  122. addLeafPrivate(key, root);
  123. }
  124.  
  125. template<class T>
  126. void BSTree<T>::addLeaf(T key, int(*compareTwo)(const T &newData, const T &node))
  127. {
  128. addLeafPrivate(key, root, compareTwo);
  129. }
  130.  
  131. template<class T>
  132. void BSTree<T>::findInOrderCallback(void(*callback)(T & data)) {
  133. findInOrderPrivate(root, callback);
  134. }
  135.  
  136. template<class T>
  137. void BSTree<T>::findInOrderCallbackPrivate(Node<T>* ptr, void(*callback)(T & data)) {
  138. if (root != NULL) {
  139. if (ptr->left != 0) {
  140. findInOrderPrivate(ptr->left, callback);
  141. }
  142. callback(ptr->key);
  143. if (ptr->right != 0) {
  144. findInOrderPrivate(ptr->right, callback);
  145. }
  146. }
  147. else {
  148. cout << "The tree is empty";
  149. }
  150. }
  151.  
  152. template<class T>
  153. BSTree<T>::Node<T>* BSTree<T>::findNode(T key) {
  154. return findNodePrivate(key, root);
  155. }
  156.  
  157. template<class T>
  158. T BSTree<T>::returnRootKey()
  159. {
  160. return root->key;
  161. }
  162.  
  163. template<class T>
  164. void BSTree<T>::traverse(int order)
  165. {
  166. traverse(order, root);
  167. }
  168.  
  169. template<class T>
  170. void BSTree<T>::traverse(int order, Node<T>* ptr)
  171. {
  172. if (order == 1)
  173. callback(ptr->key);
  174. if (ptr->left != NULL)
  175. traverse(order, ptr->left);
  176. if (order == 2)
  177. callback(ptr->key);
  178. if (ptr->right != NULL)
  179. traverse(order, ptr->right);
  180. if (order == 3)
  181. callback(ptr->key);
  182. }
  183.  
  184. template<class T>
  185. void BSTree<T>::removeNode(T key)
  186. {
  187. removeNodePrivate(key, root);
  188. }
  189.  
  190. template<class T>
  191. void BSTree<T>::removeNodePrivate(T key, Node<T> * parent)
  192. {
  193. if (root != NULL) {
  194. if (root->key == key) {
  195. removeRootMatch();
  196. }
  197. else {
  198. if (key < parent->key && parent->left != NULL) {
  199. parent->left->key == key ?
  200. removeMatch(parent, parent->left, true) :
  201. removeNodePrivate(key, parent->left);
  202. }
  203. else if (key > parent->key && parent->right != NULL) {
  204. parent->right->key == key ?
  205. removeMatch(parent, parent->right, false) :
  206. removeNodePrivate(key, parent->right);
  207. }
  208. else {
  209. cout << "The key " << key << " was not found in the tree.\n";
  210. }
  211. }
  212. }
  213. else {
  214. cout << "The tree is empty\n";
  215. }
  216. }
  217.  
  218. template<class T>
  219. void BSTree<T>::removeRootMatch()
  220. {
  221. if (root != NULL) {
  222. Node<T>* delPtr = root;
  223. int smallestInRightSubtree;
  224. if (root->left == NULL && root->right == NULL) {
  225. root = NULL;
  226. delete delPtr;
  227. }
  228. else if (delPtr->left == NULL || delPtr->right == NULL) {
  229. root = delPtr->left;
  230. if (delPtr->right != NULL)
  231. root = delPtr->right;
  232. delete delPtr;
  233. cout << "The new root is" << root->key << endl;
  234. }
  235. else {
  236. smallestInRightSubtree = findSmallestPrivate(root->right);
  237. removeNodePrivate(smallestInRightSubtree, root);
  238. root->key = smallestInRightSubtree;
  239. cout << "The new root is" << root->key << endl;
  240. }
  241. }
  242. else {
  243. cout << "Cannot remove the root. The tree is empty";
  244. }
  245. }
  246.  
  247. template<class T>
  248. void BSTree<T>::removeMatch(Node<T> * parent, Node<T> * match, bool left)
  249. {
  250. if (root != NULL) {
  251. Node<T>* delPtr;
  252. int matchKey = match->key;
  253. int smallestInRightSubtree;
  254.  
  255. if (match->left == NULL && match->right == NULL) {
  256. delPtr = match;
  257. left == true ?
  258. parent->left = NULL :
  259. parent->right = NULL;
  260. delete delPtr;
  261. cout << "The Node containing key " << matchKey << " was removed";
  262. }
  263. else if (match->left == NULL || match->right == NULL) {
  264. if (match->right != NULL) {
  265. left == true ?
  266. parent->left = match->right :
  267. parent->right = match->right;
  268. match->right = NULL;
  269. }
  270. else {
  271. left == true ?
  272. parent->left = match->left :
  273. parent->right = match->left;
  274. match->left = NULL;
  275. }
  276. delPtr = match;
  277. delete match;
  278. }
  279. else {
  280. smallestInRightSubtree = findSmallestPrivate(match->right);
  281. removeNodePrivate(smallestInRightSubtree, match);
  282. match->key = smallestInRightSubtree;
  283. }
  284. }
  285. else {
  286. cout << "The tree is empty. Cannot remove the match";
  287. }
  288. }
Add Comment
Please, Sign In to add comment