#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;
}
int max(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->rchild==NULL)
return t->data;
else
return max(t->rchild);
}
int min(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->lchild==NULL)
return t->data;
else
return min(t->lchild);
}
void ter_print(bstnode *t)
{
if(t!=NULL)
{
if(t->lchild==NULL && t->rchild==NULL)
cout<<t->data<<endl;
ter_print(t->lchild);
ter_print(t->rchild);
}
}
void printsort(bstnode *t)
{
if(t!=NULL)
{
printsort(t->lchild);
cout<<t->data<<endl;
printsort(t->rchild);
}
}
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 element you are looking for:\n";
cin>>k;
s=t;
i=search(s,k);
if(i==1)
cout<<"Found\n";
else
cout<<"Not found\n";
break;
case 3 :cout<<"Thus the maximum no is:";
s=t;
i=max(s);
if(i==0)
cout<<"No maximum value as the BStree is Null\n";
else
cout<<i<<endl;
break;
case 4 :cout<<"Thus the minimum no is:";
s=t;
i=min(s);
if(i==0)
cout<<"No minimum value as the BStree is Null\n";
else
cout<<i<<endl;
break;
case 5 :cout<<"Thus the Terminal Nodes are\n";
ter_print(t);
break;
case 6 :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;
}
int max(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->rchild==NULL)
return t->data;
else
return max(t->rchild);
}
int min(bstnode *t)
{
if(t==NULL)
return 0;
else if(t->lchild==NULL)
return t->data;
else
return min(t->lchild);
}
void ter_print(bstnode *t)
{
if(t!=NULL)
{
if(t->lchild==NULL && t->rchild==NULL)
cout<<t->data<<endl;
ter_print(t->lchild);
ter_print(t->rchild);
}
}
void printsort(bstnode *t)
{
if(t!=NULL)
{
printsort(t->lchild);
cout<<t->data<<endl;
printsort(t->rchild);
}
}
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 element you are looking for:\n";
cin>>k;
s=t;
i=search(s,k);
if(i==1)
cout<<"Found\n";
else
cout<<"Not found\n";
break;
case 3 :cout<<"Thus the maximum no is:";
s=t;
i=max(s);
if(i==0)
cout<<"No maximum value as the BStree is Null\n";
else
cout<<i<<endl;
break;
case 4 :cout<<"Thus the minimum no is:";
s=t;
i=min(s);
if(i==0)
cout<<"No minimum value as the BStree is Null\n";
else
cout<<i<<endl;
break;
case 5 :cout<<"Thus the Terminal Nodes are\n";
ter_print(t);
break;
case 6 :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