Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. #include "BSTree.h"
  2. #include <cstdlib>
  3. #include <iostream>
  4. //#include <vector>
  5.  
  6. BSTree::BSTree(){
  7. this->root = NULL;
  8. }
  9.  
  10. BSTree::BSTree(const BSTree &old_tree){
  11. //loop thru and pre order copy into a new (this) tree
  12. /* this->root = new Node();
  13. this->root->parent = NULL;
  14. this->root->left = old_tree->left;
  15. this->root->right = old_tree->right;
  16. */
  17. this->root = NULL;
  18. copyTree(old_tree.root);
  19. }
  20.  
  21. BSTree::~BSTree(){
  22. deleteTree();
  23. }
  24.  
  25. bool BSTree::empty(){
  26. return this->root == NULL;
  27.  
  28. }
  29.  
  30. bool BSTree::insert(int val){
  31. bool nextup = false;
  32. bool retval = false;
  33. if(this->root == NULL){
  34. this->root = new Node();
  35. this->root->value = val;
  36. this->root->parent = this->root->left = this->root->right = NULL;
  37. return true;
  38. } else {
  39. Node * toInsert = new Node();
  40. toInsert->value = val;
  41. toInsert->left = toInsert->right = NULL;
  42. Node * temp = root;
  43. bool rightside = false;
  44. while(temp){
  45. toInsert->parent = temp;
  46. if(val < temp->value){
  47. temp = temp->left;
  48. rightside = false;
  49. } else if(val > temp->value){
  50. temp = temp->right;
  51. rightside = true;
  52. } else /*if(val == temp->value)*/{
  53. //here they are equal
  54. return false;
  55. }
  56. if(!temp){
  57. //temp is now null
  58. if(rightside){
  59. toInsert->parent->right = toInsert;
  60. } else {
  61. toInsert->parent->left = toInsert;
  62. }
  63. }
  64. }
  65. }
  66.  
  67. }
  68.  
  69. BSTree::Node * BSTree::search(Node * n, int val){
  70. if(n != NULL){
  71. if(n->value == val){
  72. std::cout << "found" << std::endl;
  73. return n;
  74. }
  75. return search(n->left, val);
  76. return search(n->right, val);
  77. }
  78. std::cout << "missing" << std::endl;
  79. //return NULL;
  80. }
  81.  
  82. bool BSTree::find(int val){
  83. if(root == NULL){
  84. return false;
  85. }
  86. return search(root, val) != NULL;
  87. //return found != NULL;
  88.  
  89. }
  90.  
  91. void BSTree::deleteTreeLoop(Node * n){
  92. if(n != NULL){
  93. deleteTreeLoop(n->left);
  94. deleteTreeLoop(n->right);
  95. delete n;
  96. }
  97. }
  98.  
  99. void BSTree::deleteTree(){
  100. if(root == NULL){
  101. return;
  102. }
  103. deleteTreeLoop(root);
  104. }
  105.  
  106. void BSTree::copyTreeLoop(Node * n){
  107. if(n != NULL){
  108. //code to iterate and copy thru...
  109. //Node * newNode = new Node();
  110. //newNode = n;
  111. insert(n->value);
  112.  
  113. copyTreeLoop(n->left);
  114. copyTreeLoop(n->right);
  115. }
  116. }
  117.  
  118. void BSTree::copyTree(Node * n){
  119. if(n == NULL){
  120. return;
  121. }
  122. copyTreeLoop(n);
  123. }
  124.  
  125. BSTree::Node * BSTree::smallestNode(Node * n){
  126. Node * retval = NULL;
  127. if(n != NULL){
  128. while(n->left){
  129. n = n->left;
  130. }
  131. if(n->right){
  132. n->right->parent = n->parent;
  133. n->parent->right = n->right;
  134. //node was pinched out of the tree
  135. }
  136. retval = n;
  137. }
  138. return retval;
  139. }
  140.  
  141. BSTree::Node * BSTree::largestNode(Node * n){
  142. Node * retval = NULL;
  143. if(n != NULL){
  144. //retval = n;
  145. while(n->right){
  146. n = n->right;
  147. }
  148. //if it has a left child set left child to where it used to be ***********
  149. if(n->left){
  150. n->left->parent = n->parent;
  151. n->parent->left = n->left;
  152. //n left is pushed up to where n used to be
  153. }
  154. //n now holds the value of the greatest node in the tree
  155. retval = n;
  156. }
  157. return retval;
  158. }
  159.  
  160. bool BSTree::removeLoop(Node * n, int val){
  161. if(n != NULL){
  162. if(n->value == val){
  163. //So add greatest in left tree, or least in right tree into the node hole.
  164. //add a function: greatest in tree and pass the left child to it
  165. //add a function: least in tree and pass right child to it if there is no left child
  166. //if no children at all, do nothing
  167. if(n->left == NULL && n->right == NULL){
  168. //no children
  169. delete n;
  170. return true;
  171. }
  172. Node * temp = n;
  173. //here we have atleast one child, so we try the left side first.
  174. if(n->left){
  175. //left child exists, so we will find the greatest node in a tree with root being left child
  176. Node * greatest = largestNode(n->left);
  177. n->value = greatest->value;
  178. //should now have the greatest from the smaller tree inserted where the old node was
  179. delete greatest;
  180. } else {
  181. //right child exists, so we find least node in a tree with root being right child.
  182. Node * least = smallestNode(n->right);
  183. n->value = least->value;
  184. delete least;
  185. }
  186. //value is found, so no need to recurse more once its gone
  187. return true;
  188. }
  189. return removeLoop(n->left, val);
  190. return removeLoop(n->right, val);
  191. }
  192. return false;
  193. }
  194.  
  195. bool BSTree::remove(int num){
  196. if(root == NULL){
  197. return false;
  198. }
  199. return removeLoop(root, num);
  200.  
  201. }
  202.  
  203. void BSTree::fillArrayLoop(std::vector<int> &list, Node * n){
  204. if(n != NULL){
  205. fillArrayLoop(list, n->left);
  206. list.push_back(n->value);
  207. fillArrayLoop(list, n->right);
  208. }
  209. }
  210.  
  211. void BSTree::fillArray(std::vector<int> &list){
  212. if(root == NULL){
  213. return;
  214. }
  215. Node * n = root;
  216. fillArrayLoop(list, root);
  217. }
  218.  
  219. void BSTree::sortedArray(std::vector<int> &list){
  220. //So like... put the list into a tree, then remove them into an array of size(list)
  221. //and yeah that? or are we overwriting list to hold this new array because we have
  222. //its address
  223. fillArray(list);
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement