#include<stdio.h>
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root,*r,*l;
void create(node*);
void preorder(node*);
void postorder(node*);
void inorder(node*);
void main()
{
int choice;
choice=0;
clrscr();
while(choice!=5)
{
cout<<"\n1.Crete [ Insert ]\n";
cout<<"2.Preorder\n";
cout<<"3.Inorder\n";
cout<<"4.Postorder\n";
cout<<"5.Quit\n";
cout<<"Enter your choice [ 1- 5 ] \n";
cin>>choice;
switch(choice)
{
case 1: create(root);
break;
case 2: preorder(root);
break;
case 3: inorder(root);
break;
case 4: postorder(root);
break;
case 5: choice=5;cout<<"Exiting .. ";
break;
default:cout<<"\nInvalid choice !! \n";
}
}
getch();
}
void create( struct node *temp)
{
struct node *nw;
int x;
nw=new node;
cout<<"\nEnter element to be inserted :\n";
cin>>x;
nw->data=x;
nw->left=nw->right=NULL;
if(temp == NULL)
{
root=nw;
l=r=root;
}
else if(x>r->data)
{
if(r->right != NULL)create(r);
else{
r->right=nw;
r=nw;}
}
else if(x<l->data)
{
if(r->left != NULL)create(r);
else{
l->left=nw;
l=nw;}
}
else
{
cout<<"\n"<<x<<" is already exist in binary search tree \n";
}
}
void preorder(struct node *q)
{
if(q == NULL)
cout<<"Error .. EMPTY";
else
{
cout<<q->data<<" ";
if(q->left!=NULL)
preorder(q->left);
if(q->right!=NULL)
preorder(q->right);
}
}
void inorder(struct node *q)
{
if(q == NULL)
cout<<"\nError ..EMPTY\n";
else
{
if(q->left!=NULL)
inorder(q->left);
cout<<q->data<<" ";
if(q->right!=NULL)
inorder(q->right);
}
}
void postorder(struct node *q)
{
if(q == NULL)
cout<<"\nError.. EMPTY\n";
else
{
if(q->left!=NULL)
postorder(q->left);
if(q->right!=NULL)
postorder(q->right);
cout<<q->data<<" ";
}
}
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root,*r,*l;
void create(node*);
void preorder(node*);
void postorder(node*);
void inorder(node*);
void main()
{
int choice;
choice=0;
clrscr();
while(choice!=5)
{
cout<<"\n1.Crete [ Insert ]\n";
cout<<"2.Preorder\n";
cout<<"3.Inorder\n";
cout<<"4.Postorder\n";
cout<<"5.Quit\n";
cout<<"Enter your choice [ 1- 5 ] \n";
cin>>choice;
switch(choice)
{
case 1: create(root);
break;
case 2: preorder(root);
break;
case 3: inorder(root);
break;
case 4: postorder(root);
break;
case 5: choice=5;cout<<"Exiting .. ";
break;
default:cout<<"\nInvalid choice !! \n";
}
}
getch();
}
void create( struct node *temp)
{
struct node *nw;
int x;
nw=new node;
cout<<"\nEnter element to be inserted :\n";
cin>>x;
nw->data=x;
nw->left=nw->right=NULL;
if(temp == NULL)
{
root=nw;
l=r=root;
}
else if(x>r->data)
{
if(r->right != NULL)create(r);
else{
r->right=nw;
r=nw;}
}
else if(x<l->data)
{
if(r->left != NULL)create(r);
else{
l->left=nw;
l=nw;}
}
else
{
cout<<"\n"<<x<<" is already exist in binary search tree \n";
}
}
void preorder(struct node *q)
{
if(q == NULL)
cout<<"Error .. EMPTY";
else
{
cout<<q->data<<" ";
if(q->left!=NULL)
preorder(q->left);
if(q->right!=NULL)
preorder(q->right);
}
}
void inorder(struct node *q)
{
if(q == NULL)
cout<<"\nError ..EMPTY\n";
else
{
if(q->left!=NULL)
inorder(q->left);
cout<<q->data<<" ";
if(q->right!=NULL)
inorder(q->right);
}
}
void postorder(struct node *q)
{
if(q == NULL)
cout<<"\nError.. EMPTY\n";
else
{
if(q->left!=NULL)
postorder(q->left);
if(q->right!=NULL)
postorder(q->right);
cout<<q->data<<" ";
}
}