My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

program which domonstrate the use of tree

Tuesday, September 1, 2009

#include<i-add.h>
class node
{       public:
    int left;
    int right;
    int data;
    node()
    {    data=left=right=NULL;
    }
}tree[21];
void input(int &index,int &item)
{    tree[index].data=item;
    tree[index].left=2*index;
    tree[index].right=2*index+1;
}
void push()
{       clrscr();
    int index=1,temp;
    cout<<"\n\n\n\tEnter the element: ";
    cin>>temp;
    if(tree[1].data==NULL)
    {    input(1,temp);
        return;
    }
    while(index<=20)
    {       if(tree[index].data!=NULL)
        {    if(temp<tree[index].data)
            {    index=tree[index].left;
                continue;
            }
            if(temp>tree[index].data)
            {    index=tree[index].right;
                continue;
            }
            if(temp==tree[index].data)
            {    cout<<"\n\n Sorry this element already exists";
                bioskey(0);
                return;
            }
        }
        else
        {    input(index,temp);
            break;
        }
        cout<<" Index="<<index<<"\n";
    }
}
void show()
{       clrscr();
    cout<<"Index\tLeft\tData\tRight\n";
    for(int i=1;i<=20;i++)
    {    cout<<i<<"\t  "<<tree[i].left<<"\t  "<<tree[i].data<<"\t  "<<tree[i].right<<endl;
    }
    bioskey(0);
}
void main()
{    int choice=0;
    while(choice!=4)
    {       clrscr();
        gotoxy(35,10);
        cout<<"1. Insert";
        gotoxy(35,11);
        cout<<"2. Delete";
        gotoxy(35,12);
        cout<<"3. Show";
        gotoxy(35,13);
        cout<<"4. EXIT";
        gotoxy(35,15);
        cout<<"Your choice: ";
        cin>>choice;

        switch(choice)
        {    case 1:    push();
                break;
            case 2:    //pop();
                break;
            case 3:    show();
                break;
        }
    }
}

0 comments: