//hi friends at last here it is the perfectly executed economical BST program
# include <conio.h>
# include <process.h>
# include <iostream.h>
# include <alloc.h>
static int cnt;
struct node
{
int ele;
node *left;
node *right;
};
# include <process.h>
# include <iostream.h>
# include <alloc.h>
static int cnt;
struct node
{
int ele;
node *left;
node *right;
};
typedef struct node *nodeptr;
class bstree
{
public:
void insert(int,nodeptr &);
void del(int,nodeptr &);
int deletemin(nodeptr &);
void find(int,nodeptr &);
void preorder(nodeptr);
void inorder(nodeptr);
void postorder(nodeptr);
class bstree
{
public:
void insert(int,nodeptr &);
void del(int,nodeptr &);
int deletemin(nodeptr &);
void find(int,nodeptr &);
void preorder(nodeptr);
void inorder(nodeptr);
void postorder(nodeptr);
};
void bstree::insert(int x,nodeptr &p)
{
if (p==NULL)
{
p = new node;
p->ele=x;
p->left=NULL;
p->right=NULL;
}
else
{
if (x < p->ele)
insert(x,p->left);
else if (x>p->ele)
insert(x,p->right);
else
cout<<"Element already Exits !";
}
{
if (p==NULL)
{
p = new node;
p->ele=x;
p->left=NULL;
p->right=NULL;
}
else
{
if (x < p->ele)
insert(x,p->left);
else if (x>p->ele)
insert(x,p->right);
else
cout<<"Element already Exits !";
}
}
void bstree:: del(int x,nodeptr &p)
{
nodeptr d;
if (p==NULL)
cout<<"Element not found ";
else if (x < p->ele)
del(x,p->left);
else if (x > p->ele)
del(x,p->right);
else if ((p->left == NULL) && (p->right ==NULL))
{
d=p;
free(d);
p=NULL;
}
else if (p->left == NULL)
{
d=p;
free(d);
p=p->right;
}
else if (p->right ==NULL)
{
d=p;
p=p->left;
free(d);
}
else
p->ele=deletemin(p->right);
}
{
nodeptr d;
if (p==NULL)
cout<<"Element not found ";
else if (x < p->ele)
del(x,p->left);
else if (x > p->ele)
del(x,p->right);
else if ((p->left == NULL) && (p->right ==NULL))
{
d=p;
free(d);
p=NULL;
}
else if (p->left == NULL)
{
d=p;
free(d);
p=p->right;
}
else if (p->right ==NULL)
{
d=p;
p=p->left;
free(d);
}
else
p->ele=deletemin(p->right);
}
int bstree::deletemin(nodeptr &p)
{
int c;cnt=0;
if (p->left == NULL)
{
c=p->ele;
p=p->right;
return c;
}
else
c=deletemin(p->left);
return c;
}
void bstree::find(int x,nodeptr &p)
{
if (p==NULL)
cout<<"Element not found !";
else
{
if (x <p->ele)
find(x,p->left);
else if ( x> p->ele)
find(x,p->right);
else
cout<<"Element Found !";
}
}
void bstree::preorder(nodeptr p)
{
if (p!=NULL)
{
cout<<p->ele<<"-->";
preorder(p->left);
preorder(p->right);
}
}
void bstree::inorder(nodeptr p)
{
{
int c;cnt=0;
if (p->left == NULL)
{
c=p->ele;
p=p->right;
return c;
}
else
c=deletemin(p->left);
return c;
}
void bstree::find(int x,nodeptr &p)
{
if (p==NULL)
cout<<"Element not found !";
else
{
if (x <p->ele)
find(x,p->left);
else if ( x> p->ele)
find(x,p->right);
else
cout<<"Element Found !";
}
}
void bstree::preorder(nodeptr p)
{
if (p!=NULL)
{
cout<<p->ele<<"-->";
preorder(p->left);
preorder(p->right);
}
}
void bstree::inorder(nodeptr p)
{
if (p!=NULL)
{
inorder(p->left);
cout<<p->ele<<"-->";cnt++;
inorder(p->right);
}
}
{
inorder(p->left);
cout<<p->ele<<"-->";cnt++;
inorder(p->right);
}
}
void bstree::postorder(nodeptr p)
{
if (p!=NULL)
{
postorder(p->left);
postorder(p->right);
cout<<p->ele<<"-->";
}
}
int main()
{
int ch,x;
bstree bst;
char c='y';
nodeptr root;
root=NULL;
do
{
{
if (p!=NULL)
{
postorder(p->left);
postorder(p->right);
cout<<p->ele<<"-->";
}
}
int main()
{
int ch,x;
bstree bst;
char c='y';
nodeptr root;
root=NULL;
do
{
clrscr();
cout<<" Binary Search Tree";
cout<<"\n1.Insertion\n2.Deletion\n3.Find";
cout<<"4.Preorder\n5.Inorder\n6.Postorder\n7.Size";
cout<<"\nEnter your choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<" 1.Insertion";
cout<<"Enter Node Element : ";
cin>>x;
bst.insert(x,root);
break;
cout<<" Binary Search Tree";
cout<<"\n1.Insertion\n2.Deletion\n3.Find";
cout<<"4.Preorder\n5.Inorder\n6.Postorder\n7.Size";
cout<<"\nEnter your choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<" 1.Insertion";
cout<<"Enter Node Element : ";
cin>>x;
bst.insert(x,root);
break;
case 2:
cout<<" 2.Deletion";
cout<<"Enter Element to Delete ?";
cin>>x;
bst.del(x,root);
break;
case 3:
cout<<" 3.Find";
cout<<"Enter the element to Search ?";
cin>>x;
bst.find(x,root);
break;
cout<<" 2.Deletion";
cout<<"Enter Element to Delete ?";
cin>>x;
bst.del(x,root);
break;
case 3:
cout<<" 3.Find";
cout<<"Enter the element to Search ?";
cin>>x;
bst.find(x,root);
break;
case 4:
cout<<" 4.Preorder";
if (root==NULL)
cout<<"\nTree is empty";
else
{
cout<<"\nPreorder traversal is :";
bst.preorder(root);
}
break;
cout<<" 4.Preorder";
if (root==NULL)
cout<<"\nTree is empty";
else
{
cout<<"\nPreorder traversal is :";
bst.preorder(root);
}
break;
case 5:
cout<<" 5.Inorder";
if (root==NULL)
cout<<"\nTree is empty";
else
{
cout<<"\nInorder traversal is :";
bst.inorder(root);
}
break;
cout<<" 5.Inorder";
if (root==NULL)
cout<<"\nTree is empty";
else
{
cout<<"\nInorder traversal is :";
bst.inorder(root);
}
break;
case 6:
cout<<" 6.Postorder";
if (root==NULL)
cout<<"\nTree is empty";
else
{
cout<<"\nPostorder traversal is :";
bst.postorder(root);
}
break;
case 7: cout<<"\nnumber of nodes :"<<cnt;
break;
default:cout<<"invalid option..";
break;
}
cout<<"Continue (y/n) ?";
cin>>c;
}while (c!='n');
return 0;
}
cout<<" 6.Postorder";
if (root==NULL)
cout<<"\nTree is empty";
else
{
cout<<"\nPostorder traversal is :";
bst.postorder(root);
}
break;
case 7: cout<<"\nnumber of nodes :"<<cnt;
break;
default:cout<<"invalid option..";
break;
}
cout<<"Continue (y/n) ?";
cin>>c;
}while (c!='n');
return 0;
}
Chat on a cool, new interface. No download required. Click here.
No comments:
Post a Comment