#include<iostream>
#include<cstdlib>
using namespace std;
struct bstnode
{
bstnode *lchild;
int data;
bstnode *rchild;
};
bstnode *insert(bstnode * &t,int k)
{
if(t==NULL)
{
t=new(bstnode);
t->lchild=NULL;
t->rchild=NULL;
t->data=k;
}
else if(t->data>k)
insert(t->lchild,k);
else
insert(t->rchild,k);
}
int search(bstnode *t,int k)
{
if(t==NULL)
return 0;
else if(t->data>k)
return(search(t->lchild,k));
else if(t->data<k)
return(search(t->rchild,k));
else
return 1;
}
void printsort(bstnode * t)
{
if(t!=NULL)
{
printsort(t->lchild);
cout<<t->data<<endl;
printsort(t->rchild);
}
}
int max(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->rchild==NULL)
return t->data;
else
return max(t->rchild);
}
int max2(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->lchild==NULL)
return t->data;
else
return max2(t->lchild);
}
void del(bstnode * &t,int k)
{
if(t!=NULL)
{
if(k==t->data && (t->rchild!=NULL || t->lchild!=NULL))
{
int a=max(t->lchild);
if(a){
del(t,a);
t->data=a;
}
else{
int b=max2(t->rchild);
if(b)
del(t,b);
t->data=b;
}
}
else if(k==t->data && (t->rchild==NULL && t->lchild==NULL))
{
t=NULL;
}
else if(k>t->data)
del(t->rchild,k);
else
del(t->lchild,k);
}
}
void begin()
{
bstnode *s,*t;
int k,i,choice;
t=NULL;
while(1)
{
cout<<"Select any of the options provided below....\n1:Enter the bstree\n2:Search for a no\n3:Max no\n4:Min no\n5:Printing terminal node\n6:Printing Sorted no's\n";
cin>>choice;
switch(choice)
{
case 1 :cout<<"Press -1 for aborting....\n";
cin>>k;
while(k!=-1)
{
insert(t,k);
cin>>k;
}
break;
case 2 :cout<<"Enter the no:";
cin>>k;
del(t,k);
break;
case 3 :cout<<"The sorted nos's are\n";
printsort(t);
break;
case 10:cout<<"Thanks\n";
exit(1);
default:cout<<"Wrong Choice Entered....Please enter again\n";
break;
}
}
}
int main()
{
begin();
}
#include<cstdlib>
using namespace std;
struct bstnode
{
bstnode *lchild;
int data;
bstnode *rchild;
};
bstnode *insert(bstnode * &t,int k)
{
if(t==NULL)
{
t=new(bstnode);
t->lchild=NULL;
t->rchild=NULL;
t->data=k;
}
else if(t->data>k)
insert(t->lchild,k);
else
insert(t->rchild,k);
}
int search(bstnode *t,int k)
{
if(t==NULL)
return 0;
else if(t->data>k)
return(search(t->lchild,k));
else if(t->data<k)
return(search(t->rchild,k));
else
return 1;
}
void printsort(bstnode * t)
{
if(t!=NULL)
{
printsort(t->lchild);
cout<<t->data<<endl;
printsort(t->rchild);
}
}
int max(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->rchild==NULL)
return t->data;
else
return max(t->rchild);
}
int max2(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->lchild==NULL)
return t->data;
else
return max2(t->lchild);
}
void del(bstnode * &t,int k)
{
if(t!=NULL)
{
if(k==t->data && (t->rchild!=NULL || t->lchild!=NULL))
{
int a=max(t->lchild);
if(a){
del(t,a);
t->data=a;
}
else{
int b=max2(t->rchild);
if(b)
del(t,b);
t->data=b;
}
}
else if(k==t->data && (t->rchild==NULL && t->lchild==NULL))
{
t=NULL;
}
else if(k>t->data)
del(t->rchild,k);
else
del(t->lchild,k);
}
}
void begin()
{
bstnode *s,*t;
int k,i,choice;
t=NULL;
while(1)
{
cout<<"Select any of the options provided below....\n1:Enter the bstree\n2:Search for a no\n3:Max no\n4:Min no\n5:Printing terminal node\n6:Printing Sorted no's\n";
cin>>choice;
switch(choice)
{
case 1 :cout<<"Press -1 for aborting....\n";
cin>>k;
while(k!=-1)
{
insert(t,k);
cin>>k;
}
break;
case 2 :cout<<"Enter the no:";
cin>>k;
del(t,k);
break;
case 3 :cout<<"The sorted nos's are\n";
printsort(t);
break;
case 10:cout<<"Thanks\n";
exit(1);
default:cout<<"Wrong Choice Entered....Please enter again\n";
break;
}
}
}
int main()
{
begin();
}
No comments:
Post a Comment