Friday, June 14, 2013

BINARY SEARCH TREE

BINARY SEARCH TREE (C++ CODE):

/*TITLE : TO PERFORM OPERATIONS ON BINARY SEARCH TREE

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

class bt
{
struct node{
int data;
node *lchild,*rchild;
  }*root;

public:
bt()
{
root=NULL;
}
void create();
void recc();
void preorder(node*);
void postorder(node*);
void inorder(node*);
void preorder();
void inorder();
void postorder();
void search(int t);
void del_node();

};
void bt::create()
{
node *temp,*curr,*req;
int ch;
do
{
temp=new node;
temp->lchild=NULL;
temp->rchild=NULL;
cout<<"\nenter the data\n";
cin>>temp->data;
if(root==NULL)
   root=temp;
else
{
req=root;
while(req!=NULL)
{
curr=req;
if(curr->data>temp->data)
ch=1;

else
ch=2;
switch(ch)
{
case 1:
req=curr->lchild;
break;
case 2:
req=curr->rchild;
}
}
switch(ch)
{
case 1:
curr->lchild=temp;
break;
case 2:
curr->rchild=temp;
}
}
cout<<"do you want to enter more data? (1/0)";
cin>>ch;
}while(ch);
}

void bt::recc()
{
if(root==NULL)
{
cout<<"\ntree is empty";
return;
}

int ch;
do
{
cout<<"\n********menu**********\n";
cout<<"1\tpreorder\n2\tinorder\n3\tpostorder\n";
cin>>ch;
switch(ch)
{
case 1:
preorder(root);
break;
case 2:
inorder(root);
break;
case 3:
postorder(root);
break;
}
cout<<"\nDo you want to continue (0/1)\t:";
cin>>ch;
}while(ch);
}

void bt::preorder(node *t)
{
if(t==NULL)
{
return;
}
else
{
cout<<"  "<<t->data;
preorder(t->lchild);
preorder(t->rchild);
}
}

void bt::postorder(node *t)
{
if(t==NULL)
{
return;
}
else
{
postorder(t->lchild);
postorder(t->rchild);
cout<<"  "<<t->data;
}
}
void bt::inorder(node *t)
{

if(t==NULL)
{
return;
}
else
{
inorder(t->lchild);
cout<<"  "<<t->data;
inorder(t->rchild);
}
}

void bt::search(int t)
{
int flag=0;
node *temp=root;
while(temp!=NULL)
{
if(t==temp->data)
{
flag=1;
break;
}
else if(t>temp->data)
temp=temp->rchild;
else
temp=temp->lchild;
}
if(flag==1)
cout<<"\ndata is present\n";
}

void bt::del_node()
{
int t;
cout<<"\nenter the data to be deleted\n";
cin>>t;
int flag=0;
node *par,*curr,*temp=root;
while(temp!=NULL)
{
if(t==temp->data)
{
flag=1;
break;
}
else if(t>temp->data)
{
curr=temp;
temp=temp->rchild;
}
else
{
curr=temp;
temp=temp->lchild;
}
}
if(flag==1)
{
if((temp->lchild==NULL)&&(temp->rchild==NULL))
{
if(curr->lchild==temp)
curr->lchild=NULL;
if(curr->rchild==temp)
curr->rchild=NULL;
delete temp;
}
else if((temp->lchild==NULL)||(temp->rchild==NULL))
{
if(temp->lchild==NULL)
{ temp->data=temp->rchild->data;
delete temp->rchild;
temp->rchild=NULL;
}
else if(temp->rchild==NULL)
{
temp->data=temp->lchild->data;
delete temp->lchild;
temp->lchild=NULL;
}
}

else
{
curr=temp;
temp=temp->rchild;
par=NULL;
while(temp->lchild!=NULL)
{
par=temp;
temp=temp->lchild;
}

if(par!=NULL)
{
par->lchild=temp->rchild;
curr->data=temp->data;
delete temp;
}
else
{
curr->data=temp->data;
curr->rchild=NULL;
delete temp;
}

}
}
else
cout<<"\ndata not present\n";
}

void main()
{
clrscr();
bt a;
int ch,t;
cout<<"\nCreate a tree\n";
a.create();
do
{
cout<<"\n1. inserting\n2. traversal\n3.search \n4. delete\n5.exit\n";
cin>>ch;
switch(ch)
{
case 1:
a.create();
break;
case 2:
a.recc();
break;
case 3: cout<<"\nenter the data to be searched\n";
cin>>t;
a.search(t);
break;
case 4:
a.del_node();
case 5:exit(1);

}
}while(ch);
getch();

}

No comments:

Post a Comment

Thanks for your valuable comment