SlideShare a Scribd company logo
BINARY SEARCH TREE OPERATIONS */
#include
#include
#define NULL 0
struct tree
{
int info;
struct tree *left;
struct tree *right;
};
struct tree *p;
struct tree *search(struct tree *,int);
struct tree *insert(struct tree *i,int);
void inorder(struct tree *);
void delete(struct tree *);
main()
{
int l,ch,key,x,y;
struct tree *z;
clrscr();
do
{
printf(" BINARY SEARCH TREE OPERATIONS ");
printf(" 1.INSERTING ELEMENT");
printf(" 2.DELETING ELEMENT");
printf(" 3.SEARCH FOR ELEMENT");
printf(" 4.DISPLAY");
printf(" 5.EXIT");
printf("  ENTER CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf(" enter element to insertat end 0 ");
scanf("%d",&x);
while(x!=0)
{
p=insert(p,x);
printf(" enter element to insert:");
scanf("%d",&x);
}
break;
case 2:printf(" enter node to delete");
scanf("%d",&key);
z=search(p,key);
if(z!=NULL)
{
delete(z);
printf(" given key node is deleted ");
}
else
printf(" the given key node not found");
break;
case 3:printf(" enter node to be search");
scanf("%d",&y);
p=search(p,y);
if(p!=NULL)
printf(" the key element is :%d",p->info);
else
printf(" the key is not found");
break;
case 4:printf(" tree elements in inorder :");
inorder(p);
case 5:exit(0);
break;
}
}
while(ch>=1||ch<=5);
}
struct tree *insert(struct tree *s,int x)
{
struct tree *trail,*p,*q;
q=(struct tree *)malloc(sizeof(struct tree));
q->info=x;
q->left=NULL;
q->right=NULL;
p=s;
trail=NULL;
while(p!=NULL)
{
trail=p;
if(p->info>x)
p=p->left;
else
p=p->right;
}
if(trail==NULL)
{
s=q;
return(s);
}
if(trail->info>x)
trail->left=q;
else
trail->right=q;
return(s);
}
struct tree *search(struct tree *p,int x)
{
while(p!=NULL && p->info!=x)
{
if(p->info>x)
p=p->left;
else
p=p->right;
}
return(p);
}
void delete(struct tree *p)
{
struct tree *temp;
if(p==NULL)
printf(" trying to delete a noexistent node  ");
else if(p->left==NULL)
{
temp=p;
p=p->right;
free(temp);
}
else if(p->right==NULL)
{
temp=p;
p=p->left;
free(temp);
}
else if((p->left!=NULL) && (p->right!=NULL))
{
temp=p->right;
while(temp->left!=NULL)
temp=temp->left;
temp->left=p->left;
temp=p;
p=p->right;
free(temp);
}
}
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%dt",p->info);
inorder(p->right);
}
}
Solution
BINARY SEARCH TREE OPERATIONS */
#include
#include
#define NULL 0
struct tree
{
int info;
struct tree *left;
struct tree *right;
};
struct tree *p;
struct tree *search(struct tree *,int);
struct tree *insert(struct tree *i,int);
void inorder(struct tree *);
void delete(struct tree *);
main()
{
int l,ch,key,x,y;
struct tree *z;
clrscr();
do
{
printf(" BINARY SEARCH TREE OPERATIONS ");
printf(" 1.INSERTING ELEMENT");
printf(" 2.DELETING ELEMENT");
printf(" 3.SEARCH FOR ELEMENT");
printf(" 4.DISPLAY");
printf(" 5.EXIT");
printf("  ENTER CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf(" enter element to insertat end 0 ");
scanf("%d",&x);
while(x!=0)
{
p=insert(p,x);
printf(" enter element to insert:");
scanf("%d",&x);
}
break;
case 2:printf(" enter node to delete");
scanf("%d",&key);
z=search(p,key);
if(z!=NULL)
{
delete(z);
printf(" given key node is deleted ");
}
else
printf(" the given key node not found");
break;
case 3:printf(" enter node to be search");
scanf("%d",&y);
p=search(p,y);
if(p!=NULL)
printf(" the key element is :%d",p->info);
else
printf(" the key is not found");
break;
case 4:printf(" tree elements in inorder :");
inorder(p);
case 5:exit(0);
break;
}
}
while(ch>=1||ch<=5);
}
struct tree *insert(struct tree *s,int x)
{
struct tree *trail,*p,*q;
q=(struct tree *)malloc(sizeof(struct tree));
q->info=x;
q->left=NULL;
q->right=NULL;
p=s;
trail=NULL;
while(p!=NULL)
{
trail=p;
if(p->info>x)
p=p->left;
else
p=p->right;
}
if(trail==NULL)
{
s=q;
return(s);
}
if(trail->info>x)
trail->left=q;
else
trail->right=q;
return(s);
}
struct tree *search(struct tree *p,int x)
{
while(p!=NULL && p->info!=x)
{
if(p->info>x)
p=p->left;
else
p=p->right;
}
return(p);
}
void delete(struct tree *p)
{
struct tree *temp;
if(p==NULL)
printf(" trying to delete a noexistent node  ");
else if(p->left==NULL)
{
temp=p;
p=p->right;
free(temp);
}
else if(p->right==NULL)
{
temp=p;
p=p->left;
free(temp);
}
else if((p->left!=NULL) && (p->right!=NULL))
{
temp=p->right;
while(temp->left!=NULL)
temp=temp->left;
temp->left=p->left;
temp=p;
p=p->right;
free(temp);
}
}
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%dt",p->info);
inorder(p->right);
}
}

More Related Content

PDF
My C proggram is having trouble in the switch in main. Also the a co.pdf
PDF
Can you give an example of a binary heap programCan you give an .pdf
PDF
selection_sort..c#include stdio.hheader file input output func.pdf
PDF
C program
My C proggram is having trouble in the switch in main. Also the a co.pdf
Can you give an example of a binary heap programCan you give an .pdf
selection_sort..c#include stdio.hheader file input output func.pdf
C program

Similar to BINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdf (20)

PDF
a) Complete both insert and delete methods. If it works correctly 10.pdf
PPTX
Single linked list
PPTX
Circular linked list
PDF
VTU DSA Lab Manual
PPTX
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
PDF
Program of sorting using shell sort #include stdio.h #de.pdf
PDF
Data structure doubly linked list programs
DOC
Sorting programs
PPTX
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
DOCX
#C programming Question 35Implement the functions required for the.docx
DOCX
DS Code (CWH).docx
TXT
Data Structures : array operations in c program
DOCX
Linked list imp of list
DOCX
C program to implement linked list using array abstract data type
PPTX
QueueUsingArray-1.pptxnansbsbssnsnxbxbhs
PDF
c++ program to operate on binary search tree#includeiostream.pdf
PDF
program on string in java Lab file 2 (3-year)
DOCX
Data Structures Using C Practical File
PDF
mainpublic class AssignmentThree {    public static void ma.pdf
a) Complete both insert and delete methods. If it works correctly 10.pdf
Single linked list
Circular linked list
VTU DSA Lab Manual
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
Program of sorting using shell sort #include stdio.h #de.pdf
Data structure doubly linked list programs
Sorting programs
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
#C programming Question 35Implement the functions required for the.docx
DS Code (CWH).docx
Data Structures : array operations in c program
Linked list imp of list
C program to implement linked list using array abstract data type
QueueUsingArray-1.pptxnansbsbssnsnxbxbhs
c++ program to operate on binary search tree#includeiostream.pdf
program on string in java Lab file 2 (3-year)
Data Structures Using C Practical File
mainpublic class AssignmentThree {    public static void ma.pdf
Ad

More from ARYAN20071 (20)

PDF
you need to know the half life of this element in.pdf
PDF
1. The termites get their nourishment form the cellulose, which is t.pdf
PDF
1- D-These acannot be synthesised by the human body as we lack the D.pdf
PDF
1. In situ hybridisation of satellite DNA clusters in heterochromati.pdf
PDF
let p(n) be the number of partition types of an n element set. Find.pdf
PDF
Statistics From Wikipedia, the free encyclopedia Jump to navigation.pdf
PDF
the link is not working i think the file size is .pdf
PDF
In terms of thermodynamics, when molecules of sub.pdf
PDF
Iodine is used for visualization of compounds on .pdf
PDF
en.wikipedia.orgwikiGlossary_of_graph_theory .pdf
PDF
E, all S.pdf
PDF
density = massvolume .pdf
PDF
d. sublimation of dry ice because it gets conver.pdf
PDF
c. different resonance forms of the same species .pdf
PDF
Lets put this in something that python can understandCode Example.pdf
PDF
B. one is primary and one is tertiary .pdf
PDF
True.fluids is used for both liquids and gasesSolutionTrue..pdf
PDF
Virus-induced mortality of microbes has direct effects on ecosystem .pdf
PDF
There are only 2 non proper subsets one is NULL set and other is the.pdf
PDF
The ruth setSolutionThe ruth set.pdf
you need to know the half life of this element in.pdf
1. The termites get their nourishment form the cellulose, which is t.pdf
1- D-These acannot be synthesised by the human body as we lack the D.pdf
1. In situ hybridisation of satellite DNA clusters in heterochromati.pdf
let p(n) be the number of partition types of an n element set. Find.pdf
Statistics From Wikipedia, the free encyclopedia Jump to navigation.pdf
the link is not working i think the file size is .pdf
In terms of thermodynamics, when molecules of sub.pdf
Iodine is used for visualization of compounds on .pdf
en.wikipedia.orgwikiGlossary_of_graph_theory .pdf
E, all S.pdf
density = massvolume .pdf
d. sublimation of dry ice because it gets conver.pdf
c. different resonance forms of the same species .pdf
Lets put this in something that python can understandCode Example.pdf
B. one is primary and one is tertiary .pdf
True.fluids is used for both liquids and gasesSolutionTrue..pdf
Virus-induced mortality of microbes has direct effects on ecosystem .pdf
There are only 2 non proper subsets one is NULL set and other is the.pdf
The ruth setSolutionThe ruth set.pdf
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
Cell Types and Its function , kingdom of life
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
IGGE1 Understanding the Self1234567891011
PPTX
Digestion and Absorption of Carbohydrates, Proteina and Fats
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
advance database management system book.pdf
Final Presentation General Medicine 03-08-2024.pptx
RMMM.pdf make it easy to upload and study
Cell Types and Its function , kingdom of life
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
What if we spent less time fighting change, and more time building what’s rig...
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Orientation - ARALprogram of Deped to the Parents.pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
Supply Chain Operations Speaking Notes -ICLT Program
Practical Manual AGRO-233 Principles and Practices of Natural Farming
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
IGGE1 Understanding the Self1234567891011
Digestion and Absorption of Carbohydrates, Proteina and Fats
Chinmaya Tiranga quiz Grand Finale.pdf
advance database management system book.pdf

BINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdf

  • 1. BINARY SEARCH TREE OPERATIONS */ #include #include #define NULL 0 struct tree { int info; struct tree *left; struct tree *right; }; struct tree *p; struct tree *search(struct tree *,int); struct tree *insert(struct tree *i,int); void inorder(struct tree *); void delete(struct tree *); main() { int l,ch,key,x,y; struct tree *z; clrscr(); do { printf(" BINARY SEARCH TREE OPERATIONS "); printf(" 1.INSERTING ELEMENT"); printf(" 2.DELETING ELEMENT"); printf(" 3.SEARCH FOR ELEMENT"); printf(" 4.DISPLAY"); printf(" 5.EXIT"); printf(" ENTER CHOICE:"); scanf("%d",&ch); switch(ch) { case 1:printf(" enter element to insertat end 0 "); scanf("%d",&x); while(x!=0)
  • 2. { p=insert(p,x); printf(" enter element to insert:"); scanf("%d",&x); } break; case 2:printf(" enter node to delete"); scanf("%d",&key); z=search(p,key); if(z!=NULL) { delete(z); printf(" given key node is deleted "); } else printf(" the given key node not found"); break; case 3:printf(" enter node to be search"); scanf("%d",&y); p=search(p,y); if(p!=NULL) printf(" the key element is :%d",p->info); else printf(" the key is not found"); break; case 4:printf(" tree elements in inorder :"); inorder(p); case 5:exit(0); break; } } while(ch>=1||ch<=5); } struct tree *insert(struct tree *s,int x) { struct tree *trail,*p,*q;
  • 3. q=(struct tree *)malloc(sizeof(struct tree)); q->info=x; q->left=NULL; q->right=NULL; p=s; trail=NULL; while(p!=NULL) { trail=p; if(p->info>x) p=p->left; else p=p->right; } if(trail==NULL) { s=q; return(s); } if(trail->info>x) trail->left=q; else trail->right=q; return(s); } struct tree *search(struct tree *p,int x) { while(p!=NULL && p->info!=x) { if(p->info>x) p=p->left; else p=p->right; } return(p); }
  • 4. void delete(struct tree *p) { struct tree *temp; if(p==NULL) printf(" trying to delete a noexistent node "); else if(p->left==NULL) { temp=p; p=p->right; free(temp); } else if(p->right==NULL) { temp=p; p=p->left; free(temp); } else if((p->left!=NULL) && (p->right!=NULL)) { temp=p->right; while(temp->left!=NULL) temp=temp->left; temp->left=p->left; temp=p; p=p->right; free(temp); } } void inorder(struct tree *p) { if(p!=NULL) { inorder(p->left); printf("%dt",p->info); inorder(p->right); }
  • 5. } Solution BINARY SEARCH TREE OPERATIONS */ #include #include #define NULL 0 struct tree { int info; struct tree *left; struct tree *right; }; struct tree *p; struct tree *search(struct tree *,int); struct tree *insert(struct tree *i,int); void inorder(struct tree *); void delete(struct tree *); main() { int l,ch,key,x,y; struct tree *z; clrscr(); do { printf(" BINARY SEARCH TREE OPERATIONS "); printf(" 1.INSERTING ELEMENT"); printf(" 2.DELETING ELEMENT"); printf(" 3.SEARCH FOR ELEMENT"); printf(" 4.DISPLAY"); printf(" 5.EXIT"); printf(" ENTER CHOICE:"); scanf("%d",&ch); switch(ch) {
  • 6. case 1:printf(" enter element to insertat end 0 "); scanf("%d",&x); while(x!=0) { p=insert(p,x); printf(" enter element to insert:"); scanf("%d",&x); } break; case 2:printf(" enter node to delete"); scanf("%d",&key); z=search(p,key); if(z!=NULL) { delete(z); printf(" given key node is deleted "); } else printf(" the given key node not found"); break; case 3:printf(" enter node to be search"); scanf("%d",&y); p=search(p,y); if(p!=NULL) printf(" the key element is :%d",p->info); else printf(" the key is not found"); break; case 4:printf(" tree elements in inorder :"); inorder(p); case 5:exit(0); break; } } while(ch>=1||ch<=5); }
  • 7. struct tree *insert(struct tree *s,int x) { struct tree *trail,*p,*q; q=(struct tree *)malloc(sizeof(struct tree)); q->info=x; q->left=NULL; q->right=NULL; p=s; trail=NULL; while(p!=NULL) { trail=p; if(p->info>x) p=p->left; else p=p->right; } if(trail==NULL) { s=q; return(s); } if(trail->info>x) trail->left=q; else trail->right=q; return(s); } struct tree *search(struct tree *p,int x) { while(p!=NULL && p->info!=x) { if(p->info>x) p=p->left; else p=p->right;
  • 8. } return(p); } void delete(struct tree *p) { struct tree *temp; if(p==NULL) printf(" trying to delete a noexistent node "); else if(p->left==NULL) { temp=p; p=p->right; free(temp); } else if(p->right==NULL) { temp=p; p=p->left; free(temp); } else if((p->left!=NULL) && (p->right!=NULL)) { temp=p->right; while(temp->left!=NULL) temp=temp->left; temp->left=p->left; temp=p; p=p->right; free(temp); } } void inorder(struct tree *p) { if(p!=NULL) { inorder(p->left);