REFERRED FROM : PRITHVIRAJ JAIN
#include<stdio.h>
#include<stdlib.h>
struct bst
{
int data;
struct bst *lc;
struct bst *rc;
}*root,*tmp,*new,*temp,*parent;
int flag;
typedef struct bst node;
node *create()
{
temp=(node *)malloc(sizeof(node));
printf("ENTER THE ITEM \n");
scanf("%d",&temp->data);
temp->lc=NULL;
temp->rc=NULL;
return(temp);
}
void insert(node *root,node *new)
{
if(new->data<root->data)
{
if(root->lc==NULL)
root->lc=new;
else
insert(root->lc,new);
}
if(new->data>root->data)
{
if(root->rc==NULL)
root->rc=new;
else
insert(root->rc,new);
}
}
node *search(node *root,int key,node **parent)
{
flag=0;
temp=root;
while(temp!=NULL)
{
if(key==temp->data)
{
flag=1;
return(temp);
}
*parent=temp;
if(key<root->data)
temp=temp->lc;
else
temp=temp->rc;
}
}
void inorder(node *temp)
{
if(temp==NULL)
return;
inorder(temp->lc);
printf("%d\t",temp->data);
inorder(temp->rc);
}
void postorder(node *temp)
{
if(temp==NULL)
return;
postorder(temp->lc);
postorder(temp->rc);
printf("%d\t",temp->data);
}
void preorder(node *temp)
{
if(temp==NULL)
return;
printf("%d\t",temp->data);
preorder(temp->lc);
preorder(temp->rc);
}
void main()
{
int ans=1,choice,key;
while(1)
{
printf("\n-----MENU-----\n1. CREATE\n2. SEARCH\n3. RECURSIVE TRAVERSE\n4. EXIT\nENTER YOUR CHOICE :\n");
scanf("%d",&choice);
switch(choice)
{
case 1: do
{
new=create();
if(root==NULL)
root=new;
else
insert(root,new);
printf("DO YOU WISH TO INSERT MORE?(1/0)\n");
scanf("%d",&ans);
}
while(ans);
break;
case 2: if(root==NULL)
printf("EMPTY BINARY SEARCH TREE\n");
else
{
printf("ENTER THE KEY TO BE SEARCHED :\n");
scanf("%d",&key);
if(key==root->data)
{
printf("THE NODE IS PRESENT AND %d ITSELF IS THE ROOT NODE\n",root->data);
break;
}
tmp=search(root,key,&parent);
if(flag==1)
printf("THE NODE %d IS PRESENT AND ITS PARENT IS %d",tmp->data,parent->data);
else
printf("THE SEARCH IS UNSUCCESSFUL\n");
}
break;
case 3: printf("THE RECURSIVE TRAVERSE ARE :\n");
printf("\nINORDER :\n");
inorder(root);
printf("\nPREORDER :\n");
preorder(root);
printf("\nPOSTORDER :\n");
postorder(root);
break;
case 4: exit(0);
default: printf("INVALID CHOICE :\n");
}
}
}
REFERRED FROM : GURUPRASAD M S
#include<stdio.h>
struct node
{
int data;
struct node* left;
struct node* right;
}*root=NULL;
void insert(int d)
{
struct node *t,*p;
t=(struct node*)malloc(sizeof(struct node));
t->data=d;
t->left=NULL;
t->right=NULL;
if(root==NULL)
{
root=t;
}
else
{
struct node* cur;
cur=root;
while(cur)
{
p=cur;
if(t->data > p->data){
cur=cur->right;
}
else
{
cur=cur->left;
}
}
if(t->data > p->data)
{
p->right=t;
}
else
{
p->left=t;
}
}
}
void inorder(struct node* root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d \t",root->data);
inorder(root->right);
}
}
void preorder(struct node* root)
{
if(root!=NULL)
{
printf("%d \t",root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node* root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d \t",root->data);
}
}
void search(struct node* temp,int key)
{
if(temp==NULL)
{
printf("\n KEY ELEMENT NOT FOUND \n");
return;
}
else if(temp->data==key)
{
printf("\n Key Element found \n");
return;
}
else if(key>temp->data)
{
temp=temp->right;
search(temp,key);
}
else
{
temp=temp->left;
search(temp,key);
}
}
void main()
{
int ch,d,key;
while(1)
{
printf("\n\n-------MENU------\n\n 1.INSERT \t 2.TRAVERSAL \t 3.SEARCH \t 4.EXIT \n\nEnter your choice\n\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the Data\n");
scanf("%d",&d);
insert(d);
break;
case 2: if(root==NULL)
{
printf("\n TREE IS EMPTY \n");
}
else
{
printf("\n Inorder Traversal IS \n");
inorder(root);
printf("\n Preorder Traversal IS \n");
preorder(root);
printf("\n Postorder Traversal IS \n");
postorder(root);
}
break;
case 3: printf("Enter the Key Element\n");
scanf("%d",&key);
search(root,key);
break;
case 4: exit(0);
default: printf("\n Invalid Choice \n "); break;
}
}
}