Showing posts with label source code. Show all posts
Showing posts with label source code. Show all posts

Wednesday, February 1, 2012

Data Structure and Algorithm - Binary Tree - Source code - Main program

//
//  main.cpp
// 
//
//  Created by keovessna vong on 2/2/2012.
//  Copyright (c) 2012 Burapha. All rights reserved.
//

#include <iostream>
#include "bSearchTreeType.h"

using namespace std;

int main(){
    bSearchTreeType treeRoot;
   
    int num;
    cout<<"Enter numbers and exit with -999"<<endl;
    cin>>num;
    while(num != -999){
        treeRoot.insert(num);
        cin>>num;
    }
    cout<<endl<<"Tree node in inorder: ";
    treeRoot.inorderTraversal();
    cout<<endl;
   
    cout<<"Number of Nodes: "<<treeRoot.treeNodeCount()<<endl;
    return 0;
}

Look at this running of the main program :

when the user input -999 then the program will show the result as In-order sequence and show out the number of nodes of the tree then exit program

Look at this running of the main program :
In case user input the value that existed then will show an error message !!!

Data Structure and Algorithm - Binary Tree - Source code - bSearchTreeType.h

>>> Go to see main program and example :

//
//  bSearchTreeType.h
// 
//
//  Created by keovessna vong on 1/2/2012.
//  Copyright (c) 2012 Burapha. All rights reserved.
//
#include "binaryTreeType.h"
#include <cassert>
#include <iostream>
using namespace std;

class bSearchTreeType:public binaryTreeType{
public:
    bool search(const int& searchItem);
    //Function to determine whether searchItem is in the binary search tree.
    //Postcondition : Return true if searchITem is found in th binary search tree;
    // otherwise , return false.
    void insert(const int& insertItem);
    //Function to insert in the binary search tree.
    //Postcondition : If no node in the bianry tree has the same
    // into as insertItem, a node with the info insertItem is created and inserted
    // in the binary search tree.
    void deleteNode(const int& deleteItem);
    //Function to delete deleteItem from the binary search tree.
    //Postcondition : If a node with the same info as deleteItem
    // isfound, it is delete from the binary search tree.
private:
    void deleteFromTree(nodeType* &p);
    //Function to delete the node, to which p points, from the bianry search tree.
    //Postcodition : The node to which p points is deleted from the binary search tree
};

bool bSearchTreeType::search(const int &searchItem){
    nodeType *current ;
    bool found = false ;
    if(root == NULL)
        cerr<<"Cannot search an empty tree.\n";
    else{
        current = root;
        while(current != NULL && !found){
            if(current->info == searchItem)
                found = true;
            else{
                if(current->info >searchItem)
                    current = current->llink;
                else
                    current = current->rlink;
            }
        }
    }
    return found;
}
void bSearchTreeType::insert(const int& insertItem){
    nodeType *current;      //pointer to traverse the tree
    nodeType *trailCurrnt;  //pointer to behind current
    nodeType *newNode;      //pointer to create the node
    newNode = new nodeType;
    assert(newNode != NULL);
    newNode->info = insertItem;
    newNode->llink = NULL;
    newNode->rlink = NULL;
   
    if(root == NULL){
        root = newNode;
    }else{
        current = root;
        while(current != NULL){
            trailCurrnt = current;
           
            if(current->info == insertItem){
                cerr << "The insert item is already in the list - ";
                cerr << "duplicates are not allowed. \n";
                exit(0);
            }else{
                if(current->info > insertItem)
                    current = current->llink;
                else
                    current = current->rlink;
            }
        }
        if(trailCurrnt->info > insertItem)
            trailCurrnt->llink = newNode;
        else
            trailCurrnt->rlink = newNode;
    }
}  
void bSearchTreeType::deleteFromTree(nodeType* &p){
    nodeType *current;      //pointer to travers the tree
    nodeType *trailCurrnt;  //pointer to behind current
    nodeType *temp;         //pointer to delete the node
   
    if(p == NULL)
        cerr << "Error : The node to be delete is NULL. \n";
    else if(p->llink == NULL && p->rlink == NULL){
        temp = p;
        p = NULL ;
        delete temp;
    }else if(p->llink == NULL){
        temp = p;
        p = temp->rlink;
        delete temp;
    }else if(p->rlink == NULL){
        temp = p;
        p = temp->llink;
        delete temp;
    }else{
        current =p->llink;
        trailCurrnt = NULL;
       
        while(current->rlink != NULL){
            trailCurrnt = current;
            current = current->rlink;
        }
        p->info = current->info;
        if(trailCurrnt == NULL)     //current did not move;
            p->llink = current->llink; //current == p->llink; adjust p
        else
            trailCurrnt->rlink = current->llink;
       
        delete current;
    }
}
void bSearchTreeType::deleteNode(const int& deleteItem){
    nodeType *current;      //pointer to travers the tree
    nodeType *trailCurrnt;  //pointer behind current
    bool found = false;
    if(root == NULL)
        cerr << "Cannot delete from the empty tree. \n";
    else{
        current = root;
        trailCurrnt = root;
       
        while(current != NULL && !found){
            if(current->info == deleteItem)
                found = true;
            else{
                trailCurrnt = current;
                if(current->info > deleteItem)
                    current = current->llink;
                else
                    current = current->rlink;
            }
        } // end while
        if(current == NULL)
            cout << "The delete item is not in the list. \n";
        else{
            if(found){
                if(current == root)
                    deleteFromTree(root);
                else{
                    if(trailCurrnt->info >deleteItem)
                        deleteFromTree(trailCurrnt->llink);
                    else
                        deleteFromTree(trailCurrnt->rlink);
                }// end else
            }// end if(found)
        }
    }
}

Data Structure and Algorithm - Binary Tree - Source code - binaryTreeType.h

>>> Go to see main program and example :

/*
  File:   binaryTreeType.h
*/
#include <iostream>
using namespace std;

//Definition of the node

struct nodeType{
    int info;
    nodeType* llink;
    nodeType* rlink;
};

//Definition of the class
class binaryTreeType{
public:
    bool isEmpty();
    // Function to determine whether the binary tree is empty.
    // Postcondition : Return true if the binary tree is empty;
    // otherwise, return false.
    void inorderTraversal();
    //Function to do a inorder traversal of the binary tree.
    //Postcondition : The node of the binary tree output
    // in the inorder sequence.
    void preorderTraversal();
    //Function to do a preorder traversal of the binary tree.
    //Postcondition : the node of the binary tree output
    // in the preorder sequence.
    void postorderTraversal();
    //Function to do a postorder traversal of the binary tree.
    //Postcondition : The node of the binary tree output
    // in the postorder sequence.
    int treeHeight();
    //Function to determine the height of the binary tree
    //Postcondition : The height of the binary tree is returned.
    int treeNodeCount();
    //Function to determine the number of node in the binary tree
    //Postcondition : THe number of node is the binary tree is returned
    int treeLeaveCount();
    //Function to determine the number of leaves in the binary tree
    //Postcondition : the number of leaves in the binary tree is returned
    void destroyTree();
    //Deallocates the memory space occupied by the binary tree.
    //Postcondition : root = NULL
    binaryTreeType(const binaryTreeType& otherTree);
    //copy constructor
    binaryTreeType();
    //defualt constructor
    ~binaryTreeType();
    //destructor
protected :
    nodeType *root;
private:
    void copyTree(nodeType* &copiedTreeRoot,nodeType* otherTreeRoot);
    //Function to make a copy of the binary tree to
    //whic othertreeRoot points.
    //Postcondition : The pointer copiedTreeRoot points to the
    // root of the copied binary tree.
    void destroy(nodeType* &p);
    //Function to destroy the binary tree to which p points.
    //Postcondition : The node of the binary tre to which
    // p points are dellocated ; p = NULL.
    void inorder(nodeType *p);
    //Function to do inorder traversal of the binary tree to which
    // p points.
    //Postcondition : The node of the binary tree to which p points
    // are output in the inorder sequence.
    void preorder(nodeType *p);
    //Funtion to do a preorder traversal of the binary tree to which
    // a points.
    //Postcondition : The node of the binary tree to which p
    // points are output in the preorder sequence.
    void postorder(nodeType *p);
    //Function to do postorder traversal of the binary free to which p points.
    //Postcodition : The node of the bianry tree to which p
    // points are output in the postorder sequence.
    int max(int x, int y);
    //Function to determine the larger of x and y.
    //Postcondition : The larger of x and y is returned.
    int nodeCount(nodeType *p);
    //Function to determine the number of node in the binary tree
    // to which p points.
    //Postcondition : The number of node in the binary tree
    // to which p points.
    int leavesCount(nodeType *p);
    //Function to determine the number of leaves in the binary
    // tree to which p points.
    //Postcondition : The number of leaves in the binary tree
    // to which p points is returned.
    int height(nodeType *p);
};
bool binaryTreeType::isEmpty(){
    return root == NULL;
}
binaryTreeType::binaryTreeType(){
    root = NULL;
}
void binaryTreeType::inorderTraversal(){
    inorder(root);
}
void binaryTreeType::preorderTraversal(){
    preorder(root);
}
void binaryTreeType::postorderTraversal(){
    postorder(root);
}
int binaryTreeType::treeHeight(){
    return height(root);
}
int binaryTreeType::treeNodeCount(){
    return nodeCount(root);
}
int binaryTreeType::treeLeaveCount(){
    return leavesCount(root);
}
void binaryTreeType::inorder(nodeType* p){
    if(p != NULL){
        inorder(p->llink);       // L node
        cout<< p->info << " ";  // Visit node
        inorder(p->rlink);      // R node
    }
}
void binaryTreeType::postorder(nodeType* p){
    if(p != NULL){
        postorder(p->llink);    //L node
        postorder(p->rlink);    //R node
        cout<< p->info <<" ";   //Visit node
    }
}
void binaryTreeType::preorder(nodeType* p){
    if(p != NULL){
        cout<< p->info<<" ";
        preorder(p->llink);
        preorder(p->rlink);
    }
}
int binaryTreeType::height(nodeType* p){
    if(p == NULL)
        return 0;
    else
        return 1+max(height(p->llink),height(p->rlink));
}
int binaryTreeType::max(int x, int y){
    if(x>=y)
        return x;
    else
        return y;
}
void binaryTreeType::copyTree(nodeType*& copiedTreeRoot,nodeType* otherTreeRoot){
    if(otherTreeRoot == NULL){
        copiedTreeRoot = NULL;
    }else{
        copiedTreeRoot = new nodeType;
        copiedTreeRoot->info = otherTreeRoot->info;
        copyTree(copiedTreeRoot->llink,otherTreeRoot->llink);
        copyTree(copiedTreeRoot->rlink,otherTreeRoot->rlink);
    }
}
void binaryTreeType::destroy(nodeType* &p){
    if(p != NULL){
        destroy(p->llink);
        destroy(p->rlink);
        delete p;
        p = NULL;
    }
}
void binaryTreeType::destroyTree(){
    destroy(root);
}
binaryTreeType::binaryTreeType(const binaryTreeType &otherTree){
    if(otherTree.root == NULL)
        root = NULL;
    else
        copyTree(root,otherTree.root);
}
binaryTreeType::~binaryTreeType(){
    destroy(root);
}
int binaryTreeType::nodeCount(nodeType* p){
    if(p == NULL)
        return 0;
    return (nodeCount(p->llink) + nodeCount(p->rlink)+1);
}
int binaryTreeType::leavesCount(nodeType* p){
    if(p == NULL)
        return 0;
    if((p->llink ==NULL ) && (p->rlink == NULL))
        return 1;
    return (leavesCount(p->llink)+leavesCount(p->rlink));
}