Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 6.17 KB  |  hits: 8  |  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();
  120.         void printPreOrder();
  121.         void printPostOrder();
  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. //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.         return;
  188.   }  //end if
  189.  
  190.   else
  191.     rPtr->setValue(data);
  192.  
  193.   if(data < rPtr->getValue()){
  194.     if(rPtr->getLeftChildPtr() == NULL){
  195.           rPtr->setLeftChildPtr(rPtr);
  196.         }   //end if
  197.        
  198.         else{
  199.           addNodeRecursive(data, rPtr->getLeftChildPtr());
  200.         }  //end else
  201.   } //end else if
  202.  
  203.   else if(data > rPtr->getValue()){
  204.     if(rPtr->getRightChildPtr() == NULL){
  205.           rPtr->setRightChildPtr(rPtr);
  206.         }   //end if
  207.        
  208.         else{
  209.           addNodeRecursive(data, rPtr->getRightChildPtr());
  210.         }  //end else
  211.   }  //end else if
  212.  
  213.   else{  //if the same number is entered twice
  214.     return;
  215.   }  //end else
  216. }  //end addNodeRecursive
  217.  
  218. void bsTree::printPreOrder(){
  219.   cout << "The tree's pre order is" << endl;
  220.   printPreOrderRecursive(rootNodePtr);
  221. }
  222.  
  223. void bsTree::printInOrder(){
  224.   cout << "The tree in order is" << endl;
  225.   printInOrderRecursive(rootNodePtr);
  226. }
  227.  
  228. void bsTree::printPostOrder(){
  229.   cout << "The tree's post order is" << endl;
  230.   printPostOrderRecursive(rootNodePtr);
  231. }
  232.  
  233. void bsTree::printPreOrderRecursive(Node* rPtr){
  234.   if(rPtr != NULL){
  235.     cout << rPtr->getValue() << " ";
  236.     printPreOrderRecursive(rPtr->getLeftChildPtr());
  237.         printPreOrderRecursive(rPtr->getRightChildPtr());
  238.   }
  239. }  //end printPreOrder
  240.  
  241. void bsTree::printInOrderRecursive(Node* rPtr){
  242.   if(rPtr != NULL){
  243.         printInOrderRecursive(rPtr->getLeftChildPtr());
  244.     cout << rPtr->getValue() << " ";
  245.         printInOrderRecursive(rPtr->getRightChildPtr());
  246.   }
  247. }  //end printInOrder
  248.  
  249. void bsTree::printPostOrderRecursive(Node* rPtr){
  250.   if(rPtr != NULL){
  251.         printPostOrderRecursive(rPtr->getLeftChildPtr());
  252.     printPostOrderRecursive(rPtr->getRightChildPtr());
  253.         cout << rPtr->getValue() << " ";
  254.   }
  255. }  //end printPostOrder
  256.  
  257.  
  258.  
  259. int main(){
  260.   bsTree x;
  261.   x.addNode(5);
  262.   x.addNode(8);
  263.   x.addNode(6);
  264.   x.printInOrder();
  265.   cout << endl;
  266.  
  267.   bsTree y;
  268.   y.addNode(13);
  269.   y.addNode(10);
  270.   y.printPostOrder();
  271.   cout << endl;
  272.  
  273. }  //end main