Advertisement
rinab333

CSCI 340 assignmentsix.cc

Jul 16th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <iomanip>
  5. #include <algorithm>
  6. //Terina Burr
  7. //Section One
  8. //Due 10/30/15
  9. //Assignment 6
  10.  
  11. #include "assignment5.h"
  12. #include "assignment6.h"
  13. using namespace std;
  14. /***************************************************************
  15. Function: BST
  16.  
  17. Use:      it is a constructor it initializes the tree sets root to null
  18.  
  19. Arguments: none
  20.  
  21. Returns:   nothing
  22. ***************************************************************/
  23. //BST:: binTree( )
  24. //{
  25. //   root= NULL;
  26. //}
  27.  
  28. /***************************************************************
  29. Function: insert
  30.  
  31. Use:      it inserts a node of the tree calling the function insert recursively
  32.  
  33. Arguments: 1. x: the item that is inserted into tree
  34.                            
  35.  
  36. Returns:  none
  37. ***************************************************************/
  38. void BST::insert (  int x )
  39.  
  40. {
  41.     insert( this->root, x );
  42. }
  43.  
  44. // searches leaf with value x
  45. /***************************************************************
  46. Function: search
  47.  
  48. Use:      it searches for the data x of the tree calling the function search recursively
  49.  
  50. Arguments: 1. x: the item that is searched for
  51.                            
  52.  
  53. Returns:  none
  54. ***************************************************************/
  55.  
  56. bool BST::search (  int x )
  57. {
  58.     return search(this-> root, x );
  59. }
  60.  
  61. // removes leaf with value x
  62. /***************************************************************
  63. Function: remove
  64.  
  65. Use:      it removes  x from the  tree calling the function remove recursively
  66.  
  67. Arguments: 1. x: the item that is removed
  68.                            
  69.  
  70. Returns:  none
  71. ***************************************************************/
  72.  
  73. bool BST::remove (  int x )
  74. {
  75.     if(root!=0)
  76.         return  remove(this->root,x );
  77.  
  78.     return false;
  79. }
  80. // private version of insert
  81. /***************************************************************
  82. Function: insert
  83.  
  84. Use:      private version of insert it inserts a node of the tree
  85.  
  86. Arguments: 1. p: reference pointer to a tree
  87.            2. v: what is being inserted into the tree
  88.                            
  89.  
  90. Returns:  none
  91. ***************************************************************/
  92. void BST::insert ( Node *& p,  int v )
  93. {
  94.     if(p == 0)
  95.     {
  96.         p = new Node( v );
  97.     }
  98.     else if( v < p->data)
  99.     {
  100.         insert( p->left, v );
  101.     }
  102.     else if( v > p->data )
  103.     {
  104.         insert( p->right, v );
  105.     }
  106.     else
  107.     {
  108.         return; // duplicate
  109.     }
  110. }
  111.  
  112. // private version of search
  113. /***************************************************************
  114. Function: search
  115.  
  116. Use:      private version of search it searches for v in the tree p
  117.  
  118. Arguments: 1. p: reference pointer to a tree
  119.            2. v: what is being searched for
  120.                            
  121.  
  122. Returns:  true of false if in the tree
  123. ***************************************************************/
  124.  
  125. bool BST::search (Node *&p, int v )
  126. {
  127.  
  128.  
  129.     if( p == NULL )
  130.          return false;
  131.      else
  132.         {
  133.         if( v == p->data )
  134.                 {
  135. //                      if(p->left == NULL)
  136.             return true;
  137. //          if( p->right == NULL) return true;
  138.   //                      else return false;
  139.              }
  140.         else if( v < p->data )
  141.                         return search( p->left, v );
  142.         else
  143.             return search( p->right, v );
  144. }
  145.  
  146. }
  147. /***************************************************************
  148. Function: remove
  149.  
  150. Use:      private version of remove it removes an item in the tree
  151.  
  152. Arguments: 1. node: reference pointer to a tree
  153.            2. x: what is being removed
  154. ***************************************************************/
  155. bool BST:: remove(Node *& node, int x)
  156. {
  157. if( node == 0)//if the node is empty
  158.         return false;
  159.     else    if(node->data == x ) //if the node equals the one being asked and it is a leaf then
  160.     {
  161.         if(node->left==NULL && node->right!=NULL)   //only has left child
  162.         {  
  163.             Node *p = node;
  164.             node =node->right;
  165.             p->left=NULL;  // Done for safety
  166.                 p->right=NULL; // Done for safety
  167.                 delete p;
  168.             return true;
  169.         }
  170.         else if (node->right==NULL && node->left!=NULL)//only has right child
  171.         {
  172.             Node *p = node;
  173.                         node =node->left;
  174.                         p->left=NULL;  // Done for safety
  175.                         p->right=NULL; // Done for safety
  176.                         delete p;
  177.                         return true;
  178.         }
  179. else              if(node->left == NULL && node->right==NULL){//is a leaf
  180.                         delete node;
  181.  
  182.                         node = NULL;//can delete later
  183. return true;
  184.             }
  185.  
  186.         else
  187.         {
  188.             return true;
  189.         }
  190.     }  
  191.     else if (x < node->data)//
  192.         return  remove (node->left, x);
  193.     else if (x > node-> data)
  194.         return  remove (node->right, x);
  195.   else // if (node->right!=NULL &&node->left!=NULL) //jhas both children
  196.                         {
  197.                                 Node *temp = node->left;
  198.                                 while(temp->right)
  199.                                 temp = temp->right;
  200.                 Node*pred= temp;
  201.                                 pred = node;
  202.                                 return  remove(node->left,pred->data);
  203.                         }
  204.  
  205. }
  206. /***************************************************************
  207. Function: print left leaves
  208.  
  209. Use:      it prints left leaves  of the tree calling the function print left leaves recursively
  210.  
  211. Arguments: none
  212.                            
  213.  
  214. Returns:  none
  215. ***************************************************************/
  216.  
  217. void BST::printLeftLeaves()
  218. {
  219.                 printLeftLeaves(root->left);
  220. }
  221. void BST:: sumAncestors(int x, int &y)
  222. {
  223. if(sumAncestors(root->left,x,y)||sumAncestors(root->right,x,y))
  224. {
  225.         y = y+ root->data;
  226. }
  227. }
  228.  
  229. /**************************************************************
  230. Function: print left leaves
  231.  
  232. Use:      it prints left leaves  of the tree
  233.  
  234. Arguments: 1. *p: pointer to node that is going to be printed
  235.  
  236. Returns:  none
  237. ***************************************************************/
  238.  
  239.  
  240. void BST:: printLeftLeaves(Node* p )
  241. {
  242.         if(p!=0)//if node isnt null
  243.         {
  244.         //  printLeftLeaves(p->left);
  245.             if(p->left == NULL && p->right==NULL)//     if(!(p->left) && !(p->right)  
  246.                 printLeftLeaves(p->left);
  247.             cout << p->data << ",";
  248. //          printLeftLeaves(p->left);  
  249.         }  
  250. }
  251. bool BST:: sumAncestors(Node *p,int x,int&y )
  252. {
  253.     if (p == 0)//if p is null
  254.         return false;
  255.     if(p->data == x )//if the data in p maches what is searching for
  256.         return true;
  257.  
  258. if(sumAncestors(p->left,x,y)||sumAncestors(p->right,x,y)) //if there is dATA IN the right or left subtree
  259. {
  260.     y = y+ p->data;//then the sum = the data lus the sum
  261.     return true;
  262. }
  263. return false;
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement