Write a C++ Program to Implemetation of Tree Operation’s

#include<conio.h>
#include<iostream.h>
#include<stdlib.h>

class NODE
{
            public:
                        NODE *lchild,*rchild;
                        int data;
                        void create(NODE *);
                        int height(NODE *);
                        void leaf(NODE *);
                        void mirror(NODE *);
                        void level(NODE *);
                        void inorder(NODE *);
                        void eq(NODE *);
                        NODE *dq();
};
NODE *root,*tag,*q[20],*t,*temp,*nw;
int front=-1,rear=-1;
void NODE::create(NODE *root)
{
            char ch;
            cout<<"Do you want to insert at the left of"<<root->data<<"(y/n)";
            cin>>ch;
            if(ch=='y'|| ch=='Y')
            {
                        nw=new(NODE);
                        nw->lchild=NULL;
                        nw->rchild=NULL;
                        cout<<"Enter a new data";
                        cin>>nw->data;
                        root->lchild=nw;
                        create(root->lchild);
            }
            cout<<"Do you want to insert at the right of"<<root->data<<"(y/n)";
            cin>>ch;
            if(ch=='y'|| ch=='Y')
            {
                        nw=new(NODE);
                        nw->lchild=NULL;
                        nw->rchild=NULL;
                        cout<<"Enter a new data";
                        cin>>nw->data;
                        root->rchild=nw;
                        create(root->rchild);
            }

}
void NODE::inorder(NODE *root)
{
            if(root==NULL)
            return;
            inorder(root->lchild);
            cout<<"\n"<<root->data;
            inorder(root->rchild);
}
int NODE::height(NODE *root)
{
            int max=0,h;
            if(root!=NULL)
            {
                        h=1+height(root->lchild);
                        if(max<h)
                        {
                                    max=h;
                        }
                        h=1+height(root->rchild);
                        if(max<h)
                        {
                                    max=h;
                        }
            }
            return(max);
}
void NODE::leaf(NODE *root)
{
            if(root==NULL)
                        return;
            if(root->lchild==NULL && root->rchild==NULL)
                        cout<<"\t"<<root->data;
            leaf(root->lchild);
            leaf(root->rchild);
}
void NODE::mirror(NODE *root)
{
            eq(root);
            while(front!=rear)
            {
                        temp=dq();
                        if(temp->lchild!=NULL)
                                    eq(temp->lchild);
                        if(temp->rchild!=NULL)
                                    eq(temp->rchild);
                        t=temp->lchild;
                        temp->lchild=temp->rchild;
                        temp->rchild=t;

            }
                        inorder(root);
}
void NODE::level(NODE *root)
{
            int flag;
            tag=new(NODE);
            tag->lchild=NULL;
            tag->rchild=NULL;
            tag->data=-1;
            eq(root);
            eq(tag);
            while(1)
            {
                        temp=dq();
                        if(temp->data==-1 && flag==1)
                        {
                                    break;
                        }
                        else
                        {
                                    cout<<"\t";
                                    if(temp->data==-1 && flag==0)
                                    {
                                                cout<<"\n";
                                                eq(tag);
                                                flag=1;
                                    }
                                    else
                                    {
                                                flag=0;
                                                cout<<temp->data;
                                                if(temp->lchild!=NULL)
                                                            eq(temp->lchild);
                                           if(temp->rchild!=NULL)
                                                            eq(temp->rchild);
                                    }
                        }
            }
}

void NODE::eq(NODE *t)
{
            front++;
            q[front]=t;

}
NODE *NODE::dq()
{
            rear++;
            t=q[rear];
            return(t);
}
void main()
{
            NODE d;
            int ch;
            int h=0;
                       
            do
            {
                        cout<<"\n\n\n1.Create\n2.Height of tree\n3.Leaf nodes\n4.Mirror image\n5.levelwise printing \n6.Exit" ;
                        cout<<"\nEnter Your choice\n";
                        cin>>ch;
                        switch(ch)
                        {
                                    case 1:
                                                root=new(NODE);
                                                root->lchild=NULL;
                                                root->rchild=NULL;
                                                cout<<"Enter a new data";
                                                cin>>root->data;
                                                d.create(root);

                                    break;
                                    case 2:
                                                cout<<"\n\nHeight of tree ";
                                                h=d.height(root);
                                                cout<<"\n Height of the tree is:"<<h;
                                    break;
                                    case 3:
                                                cout<<"\n\nLeaf node";
                                                d.leaf(root);
                                    break;
                                    case 4:
                                                cout<<"\n\nmirror image";
                                                d.mirror(root);
                                    break;
                                    case 5:
                                                cout<<"levelwise printing";
                                                d.level(root);
                                    break;
                                    case 6:
                                                exit(0);
                                    break;
                                    default:
                                                cout<<"\ninvalid choice";
                                    break;
                        }
            }
            while(ch!=7);
            getch();
}
/*Output

1.Create
2.Height of tree
3.Leaf nodes
4.Mirror image
5.levelwise printing
6.Exit
Enter Your choice
1
Enter a new data
32
Do you want to insert at the left of32(y/n)y
Enter a new data48
Do you want to insert at the left of48(y/n)y
Enter a new data56
Do you want to insert at the left of56(y/n)n
Do you want to insert at the right of56(y/n)n
Do you want to insert at the right of48(y/n)y
Enter a new data62
Do you want to insert at the left of62(y/n)n
Do you want to insert at the right of62(y/n)n
Do you want to insert at the right of32(y/n)y
Enter a new data59
Do you want to insert at the left of59(y/n)y
Enter a new data8
Do you want to insert at the left of8(y/n)n
Do you want to insert at the right of8(y/n)n
Do you want to insert at the right of59(y/n)y
Enter a new data72
Do you want to insert at the left of72(y/n)n
Do you want to insert at the right of72(y/n)n



1.Create
2.Height of tree
3.Leaf nodes
4.Mirror image
5.levelwise printing
6.Exit
Enter Your choice
2


Height of tree
 Height of the tree is:3


1.Create
2.Height of tree
3.Leaf nodes
4.Mirror image
5.levelwise printing
6.Exit
Enter Your choice
3


Leaf node       56      62      8       72


1.Create
2.Height of tree
3.Leaf nodes
4.Mirror image
5.levelwise printing
6.Exit
Enter Your choice
4


mirror image
72
59
8
32
62
48
56


1.Create
2.Height of tree
3.Leaf nodes
4.Mirror image
5.levelwise printing
6.Exit
Enter Your choice
5
levelwise printing      32
        59      48
        72      8       62      56



1.Create
2.Height of tree
3.Leaf nodes
4.Mirror image
5.levelwise printing
6.Exit

Enter Your choice6*/
Previous
Next Post »

Disqus Shortname

Comments system