- /*****************************************************************
- *
- * Programmer: Sean Ephraim
- * Date: 4/20/2009
- * Name: HW7.cpp
- * Function:
- * Input: none
- * Output: none
- *
- *****************************************************************/
- #include <iostream>
- using std::cout;
- using std::cin;
- using std::endl;
- class Node {
- public:
- Node(int value = 0, Node* leftnode = NULL, Node* rightnode = NULL); //constructor
- ~Node(); //destructor
- //Set functions
- void setValue(int in);
- void setLeftChildPtr(Node * node);
- void setRightChildPtr(Node * node);
- //Get functions
- static int getNumNodes(); //keeps track of how many node objects have been instantiated
- int getValue();
- Node* getLeftChildPtr();
- Node* getRightChildPtr();
- void printNode(); //prints the value of the node
- private:
- static int numNodes; //number of node objects that have been instantiated
- int value;
- Node *leftChildPtr;
- Node *rightChildPtr;
- }; //end class Node
- int Node::numNodes = 0;
- //default constructor
- Node::Node(int value, Node* leftChildPtr, Node* rightChildPtr){
- setValue(value);
- setLeftChildPtr(leftChildPtr);
- setRightChildPtr(rightChildPtr);
- numNodes++;
- cout << "The Node constructor is running." << endl;
- } //end constructor Node
- //destructor
- Node::~Node(){
- //decrements numNodes; takes the places of set
- numNodes--;
- cout << "The Node destructor is running." << endl;
- } //end destructor Node
- void Node::setValue(int in){
- if( in >= 0 )
- value = in;
- else{
- value = 0;
- cout << "A negative number was entered. Value was set to 0." << endl;
- } //end else
- }
- void Node::setLeftChildPtr(Node * node){
- leftChildPtr = node;
- }
- void Node::setRightChildPtr(Node * node){
- rightChildPtr = node;
- }
- int Node::getNumNodes(){
- return numNodes; //keeps track of how many node objects have been instantiated
- }
- int Node::getValue(){
- return value;
- }
- Node* Node::getLeftChildPtr(){
- return leftChildPtr;
- }
- Node* Node::getRightChildPtr(){
- return rightChildPtr;
- }
- void Node::printNode(){ //prints the value of the node
- cout << "The value of the node is: " << getValue() << endl;
- } //end printNode
- class bsTree {
- public:
- bsTree(); //default constructor
- ~bsTree(); //destructor
- Node* getRootNodePtr();
- void addNode(int num); //adds a node to the binary search tree
- void setRootPtr(Node* root);
- Node* getRootPtr();
- Node* searchTree(Node*); //searches the tree for a key value
- void printInOrder();
- void printPreOrder();
- void printPostOrder();
- private:
- int input;
- Node* rootNodePtr;
- void addNodeRecursive(int data, Node* rPtr);
- void printInOrderRecursive(Node* rPtr);
- void printPreOrderRecursive(Node* rPtr);
- void printPostOrderRecursive(Node* rPtr);
- }; //end class bsTree
- //constructor
- bsTree::bsTree(){
- rootNodePtr = NULL;
- cout << "The bsTree constructor is running." << endl;
- } //end constructor bsTree
- //destructor
- bsTree::~bsTree(){
- delete rootNodePtr;
- cout << "The bsTree destructor is running." << endl;
- cout << "The node with value " << " was deleted." << endl;
- } //end destructor bsTree
- void bsTree::setRootPtr(Node* root){
- rootNodePtr = root;
- }
- Node* bsTree::getRootPtr(){
- return rootNodePtr;
- }
- void bsTree::addNode(int num){
- addNodeRecursive(num, rootNodePtr);
- }
- Node* bsTree::searchTree(Node*){ //searches the tree for a key value
- Node* currentPtr = rootNodePtr;
- if(currentPtr->getValue() == input){
- return currentPtr;
- }
- else if(currentPtr == NULL){
- return NULL;
- }
- if(currentPtr->getValue() > input){
- searchTree(currentPtr->getRightChildPtr());
- }
- else{
- searchTree(currentPtr->getLeftChildPtr());
- }
- } //end search tree
- void bsTree::addNodeRecursive(int data, Node* rPtr){
- if(rPtr == NULL){
- rPtr = new Node();
- rPtr->setValue(data);
- setRootPtr(rPtr);
- return;
- } //end if
- else
- rPtr->setValue(data);
- if(data < rPtr->getValue()){
- if(rPtr->getLeftChildPtr() == NULL){
- rPtr->setLeftChildPtr(rPtr);
- } //end if
- else{
- addNodeRecursive(data, rPtr->getLeftChildPtr());
- } //end else
- } //end else if
- else if(data > rPtr->getValue()){
- if(rPtr->getRightChildPtr() == NULL){
- rPtr->setRightChildPtr(rPtr);
- } //end if
- else{
- addNodeRecursive(data, rPtr->getRightChildPtr());
- } //end else
- } //end else if
- else{ //if the same number is entered twice
- return;
- } //end else
- } //end addNodeRecursive
- void bsTree::printPreOrder(){
- cout << "The tree's pre order is" << endl;
- printPreOrderRecursive(rootNodePtr);
- }
- void bsTree::printInOrder(){
- cout << "The tree in order is" << endl;
- printInOrderRecursive(rootNodePtr);
- }
- void bsTree::printPostOrder(){
- cout << "The tree's post order is" << endl;
- printPostOrderRecursive(rootNodePtr);
- }
- void bsTree::printPreOrderRecursive(Node* rPtr){
- if(rPtr != NULL){
- cout << rPtr->getValue() << " ";
- printPreOrderRecursive(rPtr->getLeftChildPtr());
- printPreOrderRecursive(rPtr->getRightChildPtr());
- }
- } //end printPreOrder
- void bsTree::printInOrderRecursive(Node* rPtr){
- if(rPtr != NULL){
- printInOrderRecursive(rPtr->getLeftChildPtr());
- cout << rPtr->getValue() << " ";
- printInOrderRecursive(rPtr->getRightChildPtr());
- }
- } //end printInOrder
- void bsTree::printPostOrderRecursive(Node* rPtr){
- if(rPtr != NULL){
- printPostOrderRecursive(rPtr->getLeftChildPtr());
- printPostOrderRecursive(rPtr->getRightChildPtr());
- cout << rPtr->getValue() << " ";
- }
- } //end printPostOrder
- int main(){
- bsTree x;
- x.addNode(5);
- x.addNode(8);
- x.addNode(6);
- x.printInOrder();
- cout << endl;
- bsTree y;
- y.addNode(13);
- y.addNode(10);
- y.printPostOrder();
- cout << endl;
- } //end main