New Member FAQ | Forums | Earn Revenue


Resources Entrance Ask Experts Exam Papers Jobs English Projects Universities Colleges Courses Schools Training My India



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



Resources » Articles/Knowledge Sharing » Education »

Program for Binary search tree


Posted Date: 23 Oct 2009    Resource Type: Articles/Knowledge Sharing    Category: Education
Author: AkhilaMember Level: Silver    
Rating: 3 out of 53 out of 53 out of 5Points: 2



PROGRAM:

#include
#include
#include
typedef struct bst
{
int data;
struct bst *left,*right;
}node;
void insert(node*,node*);
void inorder(node*);
node *search(node*,int,node**);
void del(node*,int);
void main()
{
int choice;
char ans='N';
int key;
node *new,*root,*tmp,*parent;
node *getnode();
root=NULL;
clrscr();
printf("\n\tprogram for binary search tree");
do
{
printf("\n1.create\n2.search\n3.delete\n4.display\n5.Exit");
printf("\n\nenter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
do
{
new=getnode();
printf("\nenter the element:");
scanf("%d",&new->data);
if(root==NULL)
root=new;
else
insert(root,new);
printf("\ndo you want to enter more elements?(y/n)");
ans=getch();
}while(ans=='y');
break;
case 2:
printf("\nenter the element which you want to search:");
scanf("%d",&key);
tmp=search(root,key,&parent);
printf("\nparent of node %d is %d",tmp->data,parent->data);
break;
case 3:
printf("\nenter the element you wish to delete");
scanf("%d",&key);
del(root,key);
break;
case 4:
if(root==NULL)
printf("tree is not created");
else
{
printf("\nthe tree is:");
inorder(root);
}
break;
}
}while(choice!=5);
}
node *getnode()
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->left=temp->right=NULL;
return temp;
}
void insert(node *root,node *new)
{
if(new->datadata)
{
if(root->left==NULL)
root->left=new;
else
insert(root->left,new);
}
if(new->data>root->data)
{
if(root->right==NULL)
root->right=new;
else
insert(root->right,new);
}
}
node *search(node *root,int key,node **parent)
{
node *temp;
temp=root;
while(temp!=NULL)
{
if(temp->data==key)
{
printf("\nthe %d element is present",temp->data);
return temp;
}
*parent=temp;
if(temp->data>key)
temp=temp->left;
else
temp=temp->right;
}
return NULL;
}
void del(node *root,int key)
{
node *temp,*parent,*temp_succ;
temp=search(root,key,&parent);
if(temp->left!=NULL&&temp->right!=NULL)
{
parent=temp;
temp_succ=temp->right;
while(temp_succ->left!=NULL)
{
parent=temp_succ;
temp_succ=temp_succ->left;
}
temp->data=temp_succ->data;
parent->right=NULL;
printf("\nnow deleted it");
return;
}
if(temp->left!=NULL&&temp->right==NULL)
{
if(parent->left==temp)
parent->left=temp->left;
else
parent->right=temp->left;
temp=NULL;
free(temp);
printf("\nnow deleted it");
return;
}
if(temp->left==NULL&&temp->right!=NULL)
{
if(parent->left==temp)
parent->left=temp->right;
else
parent->right=temp->right;
temp=NULL;
free(temp);
printf("\nnow deleted it ");
return;
}
if(temp->left==NULL&&temp->right==NULL)
{
if(parent->left==temp)
parent->left=NULL;
else
parent->right=NULL;
printf("\nnow deleted it");
return;
}
}
void inorder(node *temp)
{
if(temp!=NULL)
{
inorder(temp->left);
printf("%d ",temp->data);
inorder(temp->right);
}
}

OUTPUT:

program for binary search tree
1.create
2.search
3.delete
4.display
5.Exit

enter your choice:1

enter the element:44

do you want to enter more elements?(y/n)
enter the element:23

do you want to enter more elements?(y/n)
enter the element:31

do you want to enter more elements?(y/n)
enter the element:45

do you want to enter more elements?(y/n)
enter the element:48

do you want to enter more elements?(y/n)
1.create
2.search
3.delete
4.display
5.Exit

enter your choice:4

the tree is:23 31 44 45 48
1.create
2.search
3.delete
4.display
5.Exit

enter your choice:2

enter the element which you want to search:31

the 31 element is present
parent of node 31 is 23
1.create
2.search
3.delete
4.display
5.Exit

enter your choice:2

enter the element which you want to search:23

the 23 element is present
parent of node 23 is 44
1.create
2.search
3.delete
4.display
5.Exit

enter your choice:2

enter the element which you want to search:45

the 45 element is present
parent of node 45 is 44
1.create
2.search
3.delete
4.display
5.Exit

enter your choice:2

enter the element which you want to search:44

the 44 element is present
parent of node 44 is 44
1.create
2.search
3.delete
4.display
5.Exit

enter your choice:4

the tree is:23 31 44 45 48
1.create
2.search
3.delete
4.display
5.Exit

enter your choice:3

enter the element you wish to delete45

the 45 element is present
now deleted it
1.create
2.search
3.delete
4.display
5.Exit

enter your choice:4

the tree is:23 31 44 48
1.create
2.search
3.delete
4.display
5.Exit

enter your choice:2

enter the element which you want to search:48

the 48 element is present
parent of node 48 is 44
1.create
2.search
3.delete
4.display
5.exit

enter your choice:5



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
C program for binary search tree  .  Bst  .  Binary search tree using c  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: SAP Training Center
Previous Resource: Tamilnadu state board 12th board exam blueprint
Return to Discussion Resource Index
Post New Resource
Category: Education


Post resources and earn money!
 
More Resources



Advertise Here





Contact Us   Advertise   Editors    Privacy Policy    Terms Of Use   

ISC Technologies.
2006 - 2009 All Rights Reserved.