Guest User

Untitled

a guest
Dec 12th, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. #include<string>
  2. #include <fstream>
  3. #include<iostream>
  4. #include <cstring>
  5. #include"BST.h"
  6.  
  7. using namespace std;
  8.  
  9. Node*null = NULL;
  10.  
  11. BST::BST(){
  12. Root = null;
  13. }
  14.  
  15. BST::~BST(){
  16. if (Root != null){
  17. DeleteAll(Root);
  18. }
  19. }
  20.  
  21. //An in order traversal for printing to file.
  22. void BST::InOrder(Node* root){
  23. if (root != null)
  24. {
  25. InOrder(root->left);
  26. Process(root);
  27. InOrder(root->right);
  28. }
  29. }
  30.  
  31. //post order traversal for searching for the item to delete.
  32. void BST::PostOrderDelete(Node* root, Node*trail, TreeType itemToDelete){
  33. if (root != null)
  34. {
  35. PostOrderDelete(root->left, root, itemToDelete);
  36. PostOrderDelete(root->right, root, itemToDelete);
  37. if (root->word == itemToDelete){
  38. if (root->left == NULL && root->left == NULL && trail == NULL){
  39. delete root;
  40. Root = NULL;
  41. }
  42. else if (root->left == NULL && root->left == NULL){
  43. delete root;
  44. if (trail->left == root)
  45. {
  46. trail->left = NULL;
  47. }
  48. else{
  49. trail->right = NULL;
  50. }
  51. }
  52. else if (root->left != NULL && root->right != NULL){
  53. if (root->left != NULL && root->left->right != NULL){
  54. Node*trail = root->left;
  55. Node*temp = root->left->right;
  56. while (temp->right != NULL)
  57. {
  58. trail = temp;
  59. temp = temp->right;
  60. }
  61. if (temp->left == NULL){
  62. root = temp;
  63. trail->right = null;
  64. }
  65. else
  66. {
  67. trail->right = temp->left;
  68. root = temp;
  69. }
  70. }
  71. else if (root->left != NULL){
  72. if (root->right != NULL)
  73. root->left->right = root->right;
  74. Node*move = root->left;
  75. delete root;
  76. root = move;
  77. }
  78. }
  79. }
  80. }
  81. }
  82.  
  83. void BST::write(){
  84. InOrder(Root);
  85. }
  86.  
  87. //
  88. //void BST::PostOrder(Node*root, string processType){
  89. // if (root != null)
  90. // {
  91. // PostOrder(root->left, processType);
  92. // PostOrder(root->right, processType);
  93. // Process(root, processType);
  94. // }
  95. //}
  96. //
  97. //void BST::PreOrder(Node*root, string processType){
  98. // if (root != null)
  99. // {
  100. // Process(root, processType);
  101. // PreOrder(root->left, processType);
  102. // PreOrder(root->right, processType);
  103. // }
  104. //}
  105.  
  106. //writes to file. Used by the in order traversal for file writing.
  107. void BST::Process(Node* nodeToProcess){
  108. if (OutputFile.good())
  109. OutputFile << nodeToProcess->word << '\t' << nodeToProcess->count << endl;
  110. else
  111. cout << "Output file not created...\n";
  112. }
  113.  
  114. //checks if empty since head is private.
  115. bool BST::Empty(){
  116. return (Root == null);
  117. }
  118.  
  119. //inserts a word into the BST
  120. bool BST::Insert(TreeType itemToInsert){
  121. // check if the tree is empty
  122. if (Root == null)
  123. {
  124. Root = new Node;
  125. // Rrrrrrrobustness
  126. if (!Root)
  127. {
  128. return false;
  129. cout << "Unable to allocate memory.\n";
  130. }
  131. //set the branches to null
  132. Root->left = null;
  133. Root->right = null;
  134. // insert word and set count to 1
  135. Root->word = itemToInsert;
  136. Root->count = 1;
  137. }
  138. else
  139. {
  140. Node*temp = Root;
  141. //create temp Node ptr to traverse the tree to find an insertion point
  142. // Bool for the loop
  143. bool tempBool = false;
  144. // loop to traverse the tree to find an insertion point.
  145. while (!tempBool)
  146. {
  147. if (temp == null)
  148. {
  149. temp = new Node;
  150. //more Rrrrrrrobustness
  151. if (!temp)
  152. {
  153. return false;
  154. cout << "Unable to allocate memory.\n";
  155. }
  156. //more setting to null
  157. temp->left = null;
  158. temp->right = null;
  159. temp->word = itemToInsert;
  160. temp->count = 1;
  161. //end the loop
  162. tempBool = true;
  163. }
  164. //if the word is already in the tree
  165. else if (temp->word == itemToInsert){
  166. temp->count++;
  167. tempBool = true;
  168. }
  169. else if (temp->word < itemToInsert)
  170. temp = temp->right;
  171. else
  172. temp = temp->left;
  173. }
  174. }
  175. return true;
  176. }
  177.  
  178. int BST::NodeTotal(){
  179. return NodeTotalRecursive(Root);
  180. }
  181.  
  182. //My own concoction >:D
  183. //It totals it all up recursively by returning the sum of the returns of the next calls if it's not null,
  184. //then adding on as it goes.
  185. int BST::NodeTotalRecursive(Node* merp){
  186. if (merp != NULL) { return NodeTotalRecursive(merp->left) + NodeTotalRecursive(merp->right); }
  187. return 1;
  188. }
  189.  
  190. int BST::findMax() {
  191. return height(Root);
  192. }
  193.  
  194. //I'm ashamed to say I didn't figure this out myself... I found it on StackOverflow.
  195. //But dang it, I can't do it better...
  196. //It recursively loops through and returns 1 when it hits a valid node and 0 when the node is null.
  197. //in order to find the max height, it chooses the larger of the two return values to pass to the
  198. //next call. Pretty elogant if you ask me. If only I had come up with it myself...
  199. int BST::height(Node*root){
  200. if (root == null) return 1;
  201. else return 1 + max(height(root->left), height(root->right));
  202. }
  203.  
  204. //finds the max. Pretty straight forward.
  205. int BST::max(int x, int y){
  206. if (x > y) return x;
  207. else return y;
  208. }
  209.  
  210. //calls the post order deleting function to find the item to delete.
  211. void BST::Delete(TreeType itemToDelete){
  212. PostOrderDelete(Root, NULL, itemToDelete);
  213. }
  214.  
  215. //literally just deletes everything. Can't recursively call the deconstructor so I needed this.
  216. void BST::DeleteAll(Node* root)
  217. {
  218. if (root != null)
  219. {
  220. DeleteAll(root->left);
  221. DeleteAll(root->right);
  222. delete root;
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment