DSPS Practical : Binary Search Tree

#include <iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

class binary
{
private:
    struct tree
    {
        char node;
        struct tree *left,*right;
    }*headp,*headi,*root;
public:
    binary()
    {
        headp=NULL;
        headi=NULL;
        root=NULL;
    }

    void getorder();
    void displayorder();
    void maketree();
    void pretree(tree *headi);
    void displaytree();
    void postdisp(tree *temp);
};
void binary::getorder()
{
    int i,l;
    tree *temp,*new1;
    cout<<"\nProgram to create a tree Using Preorder and inorder.\n";
    cout<<"\nEnter no of nodes: ";
    cin>>l;
    cout<<"\nEnter Preorder elements:\n";
    for(i=0;i<l;i++)
    {
        new1=NULL;
        new1=new tree;
        cout<<"Enter node at "<<i<<" : ";
        cin>>new1->node;
        if(headp==NULL)
        {
            headp=temp=new1;
        }
        else
        {
            temp->right=new1;
            new1->left=temp;
            temp=new1;
            temp->right=NULL;
        }
    }
    cout<<"\nEnter Inorder elements:\n";
        for(i=0;i<l;i++)
        {
            new1=NULL;
            new1=new tree;
            cout<<"Enter node at "<<i<<" : ";
            cin>>new1->node;
            if(headi==NULL)
            {
                headi=temp=new1;
                temp->left=NULL;
                temp->right=NULL;
            }
            else
            {
                temp->right=new1;
                new1->left=temp;
                temp=new1;
                temp->right=NULL;
            }
        }
        displayorder();
    }

void binary::displayorder()
{
    //int i;
    tree *temp;
    cout<<"\nEntered Preorder elements are:  ";
    temp=headp;
    do
    {
        cout<<temp->node<<"  ";
        temp=temp->right;
    }while(temp!=NULL);
    temp=headi;
        cout<<"\nEntered Inorder elements are:  ";
        do
        {
            cout<<temp->node<<"  ";
            temp=temp->right;
        }while(temp!=NULL);
    }

void binary::maketree()
{
    root=headi;
    while(root!=NULL)
    {
        if(headp->node==root->node)
        {
            headp=headp->right;
            headp->left=NULL;
            cout<<"\nRoot created!!\nRoot is: "<<root->node<<"\n";
            pretree(root);
            break;
        }
        root=root->right;
    }
}

void binary::pretree(tree *root)
{
    tree *temp,*tempnode;
    if(root->left!=NULL)
    {
        temp=root;
        do
        {
            temp=temp->left;
        }while(headp->node!=temp->node);
        if(headp->node==temp->node)
        {
            tempnode=root->left;
            tempnode->right=NULL;
            root->left=temp;
            headp=headp->right;

            cout<<root->node<<"'s left: "<<temp->node<<"\n";
            pretree(temp);
        }
    }
    if(root->right!=NULL)
    {
        temp=root;
        do
        {
            temp=temp->right;
        }while(headp->node!=temp->node);
        if(headp->node==temp->node)
        {
            tempnode=root->right;
            tempnode->left=NULL;
            root->right=temp;
            headp=headp->right;

            cout<<root->node<<"'s right: "<<temp->node<<"\n";
            pretree(temp);
        }
    }
}

void binary::displaytree()
{
    cout<<"\nDisplaying Tree in postorder.\n";
    cout<<"\nPostorder:  ";
    postdisp(root);
    cout<<"\n";

}


void binary::postdisp(tree *pre)
{
    if(pre->left!=NULL)
        postdisp(pre->left);
    if(pre->right!=NULL)
        postdisp(pre->right);
    cout<<pre->node<<"  ";
}

int main()
{

    binary m;
    m.getorder();
    m.maketree();
    m.displaytree();
    return 0;
}
SHARE
    Blogger Comment
    Facebook Comment