Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 5.81 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. /*****************************************************************
  2.  *                                                              
  3.  * Programmer: Sean Ephraim                                      
  4.  * Date: 4/20/2009                                              
  5.  * Name: HW7.cpp                                            
  6.  * Function:                                                                                     
  7.  * Input: none           
  8.  * Output: none                  
  9.  *                                                              
  10.  *****************************************************************/
  11.  
  12. #include <iostream>
  13. using std::cout;
  14. using std::cin;
  15. using std::endl;
  16.  
  17.  
  18. class Node {
  19.        
  20.   public:
  21.     Node(int value = 0, Node* leftnode = NULL, Node* rightnode = NULL);  //constructor
  22.     ~Node();  //destructor
  23.        
  24.         //Set functions
  25.     void setValue(int in);
  26.         void setLeftChildPtr(Node * node);
  27.     void setRightChildPtr(Node * node);
  28.          
  29.     //Get functions
  30.     static int getNumNodes();  //keeps track of how many node objects have been instantiated    
  31.     int getValue();
  32.         Node* getLeftChildPtr();
  33.     Node* getRightChildPtr();
  34.  
  35.         void printNode();  //prints the value of the node
  36.        
  37.   private:
  38.         static int numNodes; //number of node objects that have been instantiated
  39.         int value;
  40.         Node *leftChildPtr;
  41.         Node *rightChildPtr;
  42.  
  43. };  //end class Node
  44.  
  45. int Node::numNodes = 0;
  46.  
  47. //default constructor
  48. Node::Node(int value, Node* leftChildPtr, Node* rightChildPtr){
  49.   setValue(value);
  50.   setLeftChildPtr(leftChildPtr);
  51.   setRightChildPtr(rightChildPtr);
  52.   numNodes++;
  53.   cout << "The Node constructor is running." << endl;
  54. }  //end constructor Node
  55.  
  56. //destructor
  57. Node::~Node(){
  58.   //decrements numNodes; takes the places of set
  59.   numNodes--;
  60.   cout << "The Node destructor is running." << endl;
  61. }  //end destructor Node
  62.  
  63. void Node::setValue(int in){
  64.   if( in > 0 )
  65.         value = in;
  66.                
  67.   else{
  68.         value = 0;
  69.         cout << "A negative number was entered. Value was set to 0." << endl;
  70.   }  //end else
  71. }
  72.  
  73. void Node::setLeftChildPtr(Node * node){
  74.   leftChildPtr = node;
  75. }
  76.  
  77. void Node::setRightChildPtr(Node * node){
  78.   rightChildPtr = node;
  79. }
  80.  
  81. int Node::getNumNodes(){
  82.   return numNodes;  //keeps track of how many node objects have been instantiated    
  83. }
  84.  
  85. int Node::getValue(){
  86.   return value;
  87. }
  88.  
  89. Node* Node::getLeftChildPtr(){
  90.   return leftChildPtr;
  91. }
  92.  
  93. Node* Node::getRightChildPtr(){
  94.   return rightChildPtr;
  95. }
  96.  
  97. void Node::printNode(){  //prints the value of the node
  98.   cout << "The value of the node is: " << getValue() << endl;
  99. }  //end printNode
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. class bsTree {
  110.        
  111.   public:
  112.     bsTree();  //default constructor
  113.     ~bsTree();  //destructor
  114.     Node* getRootNodePtr();
  115.         void addNode(int num);  //adds a node to the binary search tree
  116.     void setRootPtr(Node* root);
  117.     Node* getRootPtr();
  118.         Node* searchTree(Node*); //searches the tree for a key value
  119.     void printInOrder(Node* rPtr);
  120.         void printPreOrder(Node* rPtr);
  121.         void printPostOrder(Node* rPtr);
  122.  
  123.   private:
  124.     int input;
  125.         Node* rootNodePtr;
  126.     void addNodeRecursive(int data, Node* rPtr);
  127.     void printInOrderRecursive(Node* rPtr);
  128.     void printPreOrderRecursive(Node* rPtr);
  129.     void printPostOrderRecursive(Node* rPtr);
  130.  
  131. };  //end class bsTree
  132.  
  133. //default constructor
  134. bsTree::bsTree(){
  135.   rootNodePtr = NULL;
  136.   cout << "The bsTree constructor is running." << endl;
  137.  
  138. }  //end constructor bsTree
  139.  
  140. //destructor
  141. bsTree::~bsTree(){
  142.   delete rootNodePtr;
  143.  
  144.   cout << "The bsTree destructor is running." << endl;
  145.   cout << "The node with value " << " was deleted." << endl;
  146.  
  147. }  //end destructor bsTree
  148.  
  149. void bsTree::setRootPtr(Node* root){
  150.   rootNodePtr = root;
  151. }
  152.  
  153. Node* bsTree::getRootPtr(){
  154.   return rootNodePtr;
  155. }
  156.  
  157. void bsTree::addNode(int num){
  158.   addNodeRecursive(num, rootNodePtr);
  159. }
  160.  
  161. Node* bsTree::searchTree(Node*){  //searches the tree for a key value
  162.   Node* currentPtr = rootNodePtr;
  163.  
  164.   if(currentPtr->getValue() == input){
  165.     return currentPtr;
  166.   }
  167.        
  168.   else if(currentPtr == NULL){
  169.     return NULL;
  170.   }
  171.        
  172.   if(currentPtr->getValue() > input){
  173.     searchTree(currentPtr->getRightChildPtr());
  174.   }
  175.        
  176.   else{
  177.     searchTree(currentPtr->getLeftChildPtr());
  178.   }
  179. }  //end search tree
  180.  
  181. void bsTree::addNodeRecursive(int data, Node* rPtr){
  182.   if(rPtr == NULL){
  183.     rPtr = new Node();
  184.        
  185.         rPtr->setValue(data);
  186.         setRootPtr(rPtr);
  187.   }  //end if
  188.  
  189.   rPtr->setValue(data);
  190.  
  191.   else if(data < rPtr->getValue()){
  192.     if(rPtr->getLeftChildPtr() == NULL){
  193.           rPtr->setLeftChildPtr(rPtr);
  194.         }   //end if
  195.        
  196.         else{
  197.           addNodeRecursive(data, rPtr->getLeftChildPtr());
  198.         }  //end else
  199.   } //end else if
  200.  
  201.   else if{
  202.     if(rPtr->getRightChildPtr() == NULL){
  203.           rPtr->setRightChildPtr(rPtr);
  204.         }   //end if
  205.        
  206.         else{
  207.           addNodeRecursive(data, rPtr->getRightChildPtr());
  208.         }  //end else
  209.   }  //end else if
  210.  
  211.   else{  //if the same number is entered twice
  212.     return;
  213.   }  //end else
  214. }  //end addNodeRecursive
  215.  
  216. void bsTree::printPreOrder(Node* rPtr){
  217.   printPreOrderRecursive(rPtr);
  218. }
  219.  
  220. void bsTree::printInOrder(Node* rPtr){
  221.   printInOrderRecursive(rPtr);
  222. }
  223.  
  224. void bsTree::printPostOrder(Node* rPtr){
  225.   printPostOrderRecursive(rPtr);
  226. }
  227.  
  228. void bsTree::printPreOrderRecursive(Node* rPtr){
  229.   if(rPtr != NULL){
  230.     cout << rPtr->getValue() << " ";
  231.     printPreOrderRecursive(rPtr->getLeftChildPtr());
  232.         printPreOrderRecursive(rPtr->getRightChildPtr());
  233.   }
  234. }  //end printPreOrder
  235.  
  236. void bsTree::printInOrderRecursive(Node* rPtr){
  237.   if(rPtr != NULL){
  238.         printInOrderRecursive(rPtr->getLeftChildPtr());
  239.     cout << rPtr->getValue() << " ";
  240.         printInOrderRecursive(rPtr->getRightChildPtr());
  241.   }
  242. }  //end printInOrder
  243.  
  244. void bsTree::printPostOrderRecursive(Node* rPtr){
  245.   if(rPtr != NULL){
  246.         printPostOrderRecursive(rPtr->getLeftChildPtr());
  247.     printPostOrderRecursive(rPtr->getRightChildPtr());
  248.         cout << rPtr->getValue() << " ";
  249.   }
  250. }  //end printPostOrder