Advertisement
rinab333

CSCI 340 assignfive.h

Jul 16th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.95 KB | None | 0 0
  1. #ifndef ASSIGNMENT5
  2. #define ASSIGNMENT5
  3. //Terina burr
  4. //Assignment 5
  5. //section 1
  6. //due 10/18/15
  7. #include <iostream>
  8. #include <vector>
  9. #include <cstdlib>
  10. #include <iomanip>
  11. #include <algorithm>
  12. using namespace std;
  13. //--------------------------
  14. // You need to add definition of the Node class
  15. //--------------------------
  16. class Node {
  17. friend class binTree ;      
  18. public:
  19. int data;
  20.  Node * left;
  21.  Node * right;
  22. public:
  23. Node (int x = 0){ data = x; left = right = NULL; }
  24.  
  25. };
  26.  
  27. class binTree {
  28.     public:
  29.         binTree();
  30.         virtual void insert( int );
  31.         int height() const;
  32.         int size() const;
  33.         void inorder( void(*)(int) );
  34.         void preorder( void(*)(int) );
  35.         void postorder( void(*)(int) );                
  36.                                
  37.     protected:
  38.         Node* root;
  39.     private:
  40.         void insert( Node*&, int );
  41.         int height( Node* ) const;
  42.         int size( Node* ) const;
  43.     void inorder( Node*, void(*)(int) );
  44.     void preorder( Node*, void(*)(int) );
  45.     void postorder( Node*, void(*)(int) );
  46. };
  47.  
  48.  
  49. /***************************************************************
  50. Function: binTree
  51.  
  52. Use:      it is a constructor it initializes the tree sets root to null
  53.  
  54. Arguments: none
  55.  
  56. Returns:   nothing
  57. ***************************************************************/
  58.  
  59.  
  60. binTree::binTree( )
  61. {
  62.     root = NULL; // initialize root
  63. }
  64. /***************************************************************
  65. Function: size
  66.  
  67. Use:      it calls the function size recursively
  68.  
  69. Arguments: none
  70.  
  71. Returns:   the size of the tree
  72. ***************************************************************/
  73.    
  74.  
  75. int binTree ::size( ) const
  76. {
  77.     return size( root ); // call recursive
  78. }
  79.    
  80. /***************************************************************
  81. Function: height
  82.  
  83. Use:      it calls the function height recursively
  84.  
  85. Arguments: none
  86.  
  87. Returns:   the height of the tree
  88. ***************************************************************/
  89.  
  90. // returns height of tree
  91.    
  92. int binTree ::height( ) const
  93. {
  94.     return height( root ); // call recursive
  95. }
  96.    
  97. /***************************************************************
  98. Function: insert
  99.  
  100. Use:      it inserts a node of the tree calling the function insert recursively
  101.  
  102. Arguments: 1. x: the item that is inserted into tree
  103.                            
  104.  
  105. Returns:  none
  106. ***************************************************************/
  107.  
  108.     // inserts a node in shortest subtree
  109.    
  110. void binTree ::insert( int x )
  111. {
  112.     insert( root, x ); // call recursive
  113. }
  114.    
  115. /***************************************************************
  116. Function: inorder
  117.  
  118. Use:      calls inorder recursively
  119.  
  120. Arguments: 1. fn: pointer to a function accepting one int argument and returning void
  121.  
  122. Returns:   none
  123. ***************************************************************/
  124.  
  125.     // inorder traversal of tree
  126.    
  127. void binTree  ::inorder( void ( *fn ) (int) )
  128. {
  129.     inorder( root, fn ); // call recursive
  130. }
  131.    
  132.  
  133. /***************************************************************
  134. Function: preorder
  135.  
  136. Use:      calls preorder recursively
  137.  
  138. Arguments: 1. fn: pointer to a function accepting one int argument and returning void
  139.  
  140. Returns:   none
  141. ***************************************************************/
  142.  
  143.  
  144.     // preorder traversal of tree
  145. void binTree ::preorder( void ( *fn ) (int ) )
  146. {
  147.     preorder( root, fn ); // call recursive
  148. }
  149.    
  150. /***************************************************************
  151. Function: postorder
  152.  
  153. Use:      calls postorder recursively
  154.  
  155. Arguments: 1. fn: pointer to a function accepting one int argument and returning void
  156.  
  157. Returns:   none
  158. ***************************************************************/
  159.  
  160.     // postorder traversal of tree
  161.    
  162. void binTree ::postorder( void ( *fn ) ( int ) )
  163. {
  164.     postorder( root, fn ); // call recursive
  165. }
  166.    
  167.  
  168.     // private version of size
  169. /***************************************************************
  170. Function: size
  171.  
  172. Use:      private version of size it finds the size of a tree
  173.  
  174. Arguments: 1. Ptr: pointer to tree
  175.  
  176. Returns:   the size of the tree
  177. ***************************************************************/
  178.    
  179. int binTree ::size( Node * ptr ) const
  180. {
  181.     if( ptr != 0 ) // if not empty
  182.     {
  183.         return 1 + size( ptr->left ) + size( ptr->right );
  184.     }
  185.     else
  186.     {
  187.         return 0; // emtpy tree
  188.     }
  189. }
  190.    
  191. /***************************************************************
  192. Function: height
  193.  
  194. Use:      private version of height it finds the height of a tree
  195.  
  196. Arguments: 1. Ptr: pointer to tree
  197.  
  198. Returns:   the height of the tree
  199. ***************************************************************/
  200.  
  201.     // private version of height
  202.    
  203. int binTree ::height( Node * ptr ) const
  204. {
  205.     if( ptr == 0 )
  206.     {
  207.         return 0; // empty tree
  208.     }
  209.     else
  210.     {
  211.         int lHeight = height( ptr->left ); // left side
  212.         int rHeight = height( ptr->right ); // right side
  213.  
  214.         if( lHeight > rHeight ) // which is greater
  215.         {
  216.             return 1 + lHeight; // return left
  217.         }
  218.         else
  219.         {
  220.             return 1 + rHeight; // return right
  221.         }  
  222.     }
  223. }
  224.  
  225.  
  226.     // private version of insert
  227. /***************************************************************
  228. Function: insert
  229.  
  230. Use:      private version of insert it inserts a node of the tree
  231.  
  232. Arguments: 1. p: reference pointer to a tree
  233.            2. v: what is being inserted into the tree
  234.                            
  235.  
  236. Returns:  none
  237. ***************************************************************/
  238.    
  239. void binTree ::insert( Node * & p, int  v )
  240. {
  241.     if( p == 0 ) //if the node is empty
  242.     {
  243.         Node  * newNode;
  244.         newNode = new Node ( v ); // new node with new value
  245.         p = newNode; // set ptr to new node
  246.     }      
  247.     else
  248.     {
  249.         int lHeight = height( p->left );//get hight of left
  250.         int rHeight = height( p->right );//get height of right
  251.      
  252.         if( lHeight <= rHeight )//if left height less than or equal to right
  253.         {
  254.             insert( p->left, v );//insert v on left node
  255.         }
  256.         else
  257.         {
  258.             insert( p->right, v ); //insert v on right node
  259.         }
  260.     }
  261. }
  262.    
  263.  
  264.     // private version of inOrder
  265. /***************************************************************
  266. Function: inorder
  267.  
  268. Use:      puts tree in the inorder version first traverse the left subtree then visit the root and then traverse the right subtree
  269.  
  270. Arguments: 1. fn: pointer to a function accepting one int argument and returning void
  271.            2. p: pointer to a node
  272.    
  273.  
  274. Returns:   none
  275. ***************************************************************/
  276.    
  277. void binTree ::inorder( Node * p, void ( *fn ) (int ) )
  278. {
  279.     if( p != NULL )//if it is not null
  280.     {
  281.         inorder( p->left, fn );//traverse left
  282.         fn( p->data );//traverse root
  283.         inorder( p->right, fn );//traverse right
  284.     }
  285. }
  286. /***************************************************************
  287. Function: preorder
  288.  
  289. Use:      puts tree in preorder version visit the root traverse the left subtree traverse the right subtree
  290.  
  291. Arguments: 1. fn: pointer to a function accepting one int argument and returning void
  292.            2. p: pointer to a node
  293.  
  294. Returns:   none
  295. ***************************************************************/
  296.  
  297.  
  298. // private version of preOrder
  299.  
  300. void binTree ::preorder( Node * p, void ( *fn ) ( int ) )
  301. {
  302.     if( p != NULL )
  303.     {
  304.         fn( p->data ); //traverse root
  305.         preorder( p->left, fn ); //traverse left
  306.         preorder( p->right, fn );// traverse rightt
  307.     }
  308. }
  309.    
  310.  
  311. // private version of postOrder
  312.  
  313. /***************************************************************
  314. Function: postorder
  315.  
  316. Use:      puts tree in post order    traverse the left subtree then traverse the right subtree then visit the root
  317.  
  318. Arguments: 1. fn: pointer to a function accepting one int argument and returning void
  319.  
  320. Returns:   none
  321. ***************************************************************/
  322.  
  323.  
  324.    
  325. void binTree ::postorder( Node * p, void ( *fn ) ( int ) )
  326. {
  327.     if( p != NULL ) // if the node not empty
  328.     {
  329.         postorder( p->left, fn ); ///traverse left
  330.         postorder( p->right, fn ); //traverse right
  331.         fn( p->data ); //traverse root
  332.     }
  333. }
  334.  
  335.  
  336.    
  337.    
  338.    
  339.  
  340.    
  341.  
  342.  
  343. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement