SlideShare a Scribd company logo
DATA STRUCTURES
Binary Search Tree
Santhiya S
Assistant Professor
Department of AI
Kongu Engineering College
Binary search tree -8, 3, 6,1,10, 4, 9, 8.5, 50,35
8
10
3
6
1
4
9
8.5
50
35
Basic Operations
• The basic operations of a BST
• Search
• Insert
• Find minimum value
• Find maximum value
• Delete
• Traversal
• Pre-order Traversal
• In-order Traversal
• Post-order Traversal
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*left;
struct node*right;
}*root;
void insert()
{
struct node *temp, *parent;
struct node *newnode= (struct
node*)malloc(sizeof(struct node));
scanf("%d",&newnode-> data);
newnode-> left=NULL;
newnode->right=NULL;
if(root==NULL)
{
root=newnode;
}
else
{
temp = root;
while(temp)
{
parent=temp;
if(newnode-> data<temp->data)
{
temp = temp-> left;
}
else
{
temp = temp-> right;
}
}
if(newnode-> data< parent->data)
{
parent-> left = newnode;
}
else
{
parent-> right = newnode;
}
}
}
void inorder(struct node *T)
{
if (T==NULL)
return;
inorder(T->left);
printf(" %d ",T->data);
inorder(T->right);
}
int main()
{
//struct node *root=NULL;
int n;
printf("n enter no. of term:- ");
scanf("%d",&n);
while(n!=0)
{
insert();
n--;
}
inorder(root);
print(“Enter the value to search”);
scanf(“%d”, &item);
search(item);
}
void insert()
{
struct node *temp, *parent;
struct node *newnode= (struct
node*)malloc(sizeof(struct node));
scanf("%d",&newnode-> data);
newnode-> left=NULL;
newnode->right=NULL;
if(root==NULL)
{
root=newnode;
}
else
{
temp = root;
while(temp)
{
parent=temp;
if(newnode-> data<temp->data)
{
temp = temp-> left;
}
else
{
temp = temp-> right;
}
}
if(newnode-> data< parent->data)
{
parent-> left = newnode;
}
else
{
parent-> right = newnode;
}
}
}
30
root
100
100
temp
100
parent
100
while(100), true
20<30, true
400
0 0
20<30, true
0
0
20
200
200
0 0
25
300
while(100), true
25<30, true
while(NULL),False
200
while(200),true
200
if 25<20, false
while(NULL),False
300
0 0
15
400
if 25<20, false
while(100), true
15<30, true
while(200),true
if15<20, true
if15<20, true
0 0 0
40
while(100), true
if 40<30, false
while(NULL),False
600
600
if 40<30, false
void search(int item)
{
struct node *parent=NULL, *temp=NULL;
temp = root;
while(temp->data != item)
{
//parent=temp;
if(temp != NULL)
{
printf("%d ",temp->data);
if(item<temp->data )
{
temp = temp->left;
}
else
{
temp = temp->right;
}
if(temp == NULL)
{
printf(“Element Not Found”);
break;
}
}
}
printf("nSearched value (or) least right node value :
%dn",temp->data);
}
30
root
100
100
20
200
300
0 0
15
400
400 0 0
40
600
600
0 0
25
300
200
item
temp
parent
25
100
100
while 30!=25,true
if 100!=NULL,true //op 30
25<30,true
200
200==NULL,false
while 20!=25,true
200
if 200!=NULL,true
//op 20
25<20,false
300
300==NULL,false
while 25!=25, False
op Searched value
(or) least right node
value :25
void deletion(int item)
{
struct node *parent=NULL, *temp=NULL;
temp = root;
search(item); (2)
if (temp == NULL)
return;
if (temp->left == NULL && temp->right ==
NULL) //No child
{
if (temp != root)
{
if (parent->left == temp)
parent->left = NULL;
else
parent->right = NULL;
}
else
root = NULL;
free(temp);
}
else if (temp->left !=NULL&& temp-
>right!=NULL) // if 2 child
{
// find the min value in right sub tree
temp1=temp;
t1=temp1- >right;
while(t1->left!=NULL)
{
t1=t1->left;
}
int val = t1->data;(4)
deletion(t1->data);
temp1->data = val;
}
else //if one child
{
struct node* child = (temp->left!=NULL)? temp-
>left: temp->right;
if (temp != root)
{
if (temp == parent->left)
parent->left = child;
temp->left=NULL;
else
parent->right = child;
temp->right=NULL;
}
else
{
root = child;
}
free(temp);
}
}
void deletion(int item)
{
struct node *parent=NULL, *temp=NULL
*temp1,*t1;
item,temp,parent =search(item);
if (temp == NULL)
return;
if (temp->left == NULL && temp->right == NULL)
//No child
{
if (temp != root)
{
if (parent->left == temp)
parent->left = NULL;
else
parent->right = NULL;
}
else
root = NULL;
free(temp);
}
}
temp
parent
30
root
100
100
20
200
300
0 0
15
400
400 0 0
40
600
600
0 0
25
300
200
300
200
item
25
300==NULL, false
0==NULL&&0==NULL,true,
no child condition
400==300,False
300!=100,true (only root node
with no child condition)
0
temp
parent
30
root
100
100
200 600
100
NULL
item
25
100==NULL, false
0==NULL&&0==NULL,true,
no child condition
100!=100,false (only root node
with no child condition)
0
void deletion(int item)
{
struct node *parent=NULL, *temp=NULL
*temp1,*t1;
temp = root;
item,temp,parent = search(item);
if (temp == NULL)
return;
if (temp->left == NULL && temp->right == NULL)
//No child
{
}
else if (temp->left !=NULL&& temp->right!=NULL)
// if 2 child
{
// find the min value in right sub tree
temp1=temp;
t1=temp1- >right;
while(t1->left!=NULL)
{
t1=t1->left;
}
int val = t1->data;
deletion(t1->data);
temp1->data = val;
}
else // one child
{
}
30
root
100
100
20
200
300
0 0
15
400
400 300 700
40
600
600
0 0
35
300
200
temp
parent
100
NULL
item
30
else if 200!=NULL && 600!=NULL, true,
2 child node condition
temp1 t1
100
600
0 0
60
700
while(300!=NULL),true,
to check whether right sub tree contain left node
300
while(0!=NULL),false
value
35
deletion(t1->data);
0
35
100==NULL,False
if (200===NULL &&600==NULL),false,
no child node condition temp
parent
300
600
void deletion(int item)
{
struct node *parent=NULL, *temp=NULL
*temp1,*t1;
temp = root;
item,temp,parent = search(item);
if (temp == NULL)
return;
if (temp->left == NULL && temp->right == NULL)
//No child
{
}
else if (temp->left !=NULL&& temp->right!=NULL)
// if 2 child
{
}
else // one child
{
struct node* child = (temp->left!=NULL)? temp-
>left: temp->right;
if (temp != root)
{
if (temp == parent->left)
{
parent->left = child;
if(temp->left!=NULL)
temp->left=NULL;
else
temp->right=NULL;
else
{
parent->right = child;
if(temp->left!=NULL)
temp->left=NULL;
else
temp->right=NULL;
}
}
else
{
root = child;
}
free(temp);
}
30
root
100
100
20
200
0
0 0
15
300
400 300 0
40
600
600
0 0
35
400
200
parent
100
item
40
else if 200!=NULL && 600!=NULL, false,
2 child node condition
temp
600
child=(300!=NULL)?300:NULL
if(600!=100), true
else (one child):
100==NULL,False
if (200===NULL &&600==NULL),false,
no child node condition
child= 300
if(600==200), false.enter to else part
300
0
if(300!=NULL), true
TRAVERSAL
inorder(root)
void inorder(struct node *root)
{
if (root!=NULL)
{
inorder(root->left);
printf(" %d “,root->data);
inorder(root->right);
}
}
7
2 9
1 5
14
r
o
o
t
TRAVERSAL
preorder(root)
void preorder(struct node *root)
{
if (root!=NULL)
{
printf(" %d ",root->data);
preorder(root->left);
preorder(root->right);
}
}
7
2 9
1 5
14
r
o
o
t
TRAVERSAL
postorder(root)
void postorder(struct node *root)
{
if (root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf(" %d ",root->data);
}
}
7
2 9
1 5
14
r
o
o
t
FIND MAXIMUM
void max ()
{
if (root==NULL)
return root;
else
{
struct node*temp;
temp=root;
while(tempright !=NULL)
{
temp=temp  right;
}
printf(“n %d”, tempdata);
}
r
o
o
t
Maximum value:14
temp
FIND MNIMUM
void min ()
{
if (root==NULL)
return root;
else
{
struct node*temp;
temp=root;
while(templeft !=NULL)
{
temp=temp  left;
}
printf(“n %d”, tempdata);
}
temp
r
o
o
t
Minimum value:1

More Related Content

PDF
program on string in java Lab file 2 (3-year)
PPTX
CS8391-Data Structures Unit 3
PPTX
DS - BST
PDF
Tree Data Structure by Daniyal Khan
PDF
PPT
binary search tree
PPTX
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
program on string in java Lab file 2 (3-year)
CS8391-Data Structures Unit 3
DS - BST
Tree Data Structure by Daniyal Khan
binary search tree
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE

Similar to Binary search tree.pptx (20)

PPTX
BST.pptx this is Good for data structure
PPTX
BST.pptx this isp used for learning binary search trees
DOCX
Biary search Tree.docx
PPT
1.2 operations of tree representations
PPTX
Binary Search Tree.pptx
DOCX
Binary Tree in C++ coding in the data structure
PDF
This is a c++ binary search program I worked so far but still cant g.pdf
PDF
Binary search tree
PPTX
Binary Search Tree
PPT
Binary Search Tree and AVL
PDF
Unit iv data structure-converted
PPT
Unit 4 tree
PDF
Binary Tree
PDF
My C proggram is having trouble in the switch in main. Also the a co.pdf
PDF
Tree and binary tree
DOCX
10 Solution#include-iostream-h- #include-conio-h- #include-process-h-.docx
PPTX
PPT
BINARY TREE REPRESENTATION.ppt
PPTX
Data Strcutres-Non Linear DS-Advanced Trees
PPTX
Lecture_10 - Revised.pptx
BST.pptx this is Good for data structure
BST.pptx this isp used for learning binary search trees
Biary search Tree.docx
1.2 operations of tree representations
Binary Search Tree.pptx
Binary Tree in C++ coding in the data structure
This is a c++ binary search program I worked so far but still cant g.pdf
Binary search tree
Binary Search Tree
Binary Search Tree and AVL
Unit iv data structure-converted
Unit 4 tree
Binary Tree
My C proggram is having trouble in the switch in main. Also the a co.pdf
Tree and binary tree
10 Solution#include-iostream-h- #include-conio-h- #include-process-h-.docx
BINARY TREE REPRESENTATION.ppt
Data Strcutres-Non Linear DS-Advanced Trees
Lecture_10 - Revised.pptx
Ad

More from Santhiya S (8)

PPTX
Expression tree conversion.pptx
PPTX
Queue implementation using Linked list.pptx
PPTX
Queue implementation using Array.pptx
PPTX
Stack implementation using Array.pptx
PPTX
Circular linked list.pptx
PPTX
Doubly linked list.pptx
PPTX
Singly linked list.pptx
PPTX
Linked list memory allocation and its types.pptx
Expression tree conversion.pptx
Queue implementation using Linked list.pptx
Queue implementation using Array.pptx
Stack implementation using Array.pptx
Circular linked list.pptx
Doubly linked list.pptx
Singly linked list.pptx
Linked list memory allocation and its types.pptx
Ad

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Cell Types and Its function , kingdom of life
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Computing-Curriculum for Schools in Ghana
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Institutional Correction lecture only . . .
PPTX
Presentation on HIE in infants and its manifestations
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Lesson notes of climatology university.
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Complications of Minimal Access Surgery at WLH
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Cell Types and Its function , kingdom of life
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial diseases, their pathogenesis and prophylaxis
Computing-Curriculum for Schools in Ghana
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Structure & Organelles in detailed.
Pharma ospi slides which help in ospi learning
Final Presentation General Medicine 03-08-2024.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
GDM (1) (1).pptx small presentation for students
Institutional Correction lecture only . . .
Presentation on HIE in infants and its manifestations
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Lesson notes of climatology university.
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

Binary search tree.pptx

  • 1. DATA STRUCTURES Binary Search Tree Santhiya S Assistant Professor Department of AI Kongu Engineering College
  • 2. Binary search tree -8, 3, 6,1,10, 4, 9, 8.5, 50,35 8 10 3 6 1 4 9 8.5 50 35
  • 3. Basic Operations • The basic operations of a BST • Search • Insert • Find minimum value • Find maximum value • Delete • Traversal • Pre-order Traversal • In-order Traversal • Post-order Traversal
  • 4. #include<stdio.h> #include<stdlib.h> struct node { int data; struct node*left; struct node*right; }*root; void insert() { struct node *temp, *parent; struct node *newnode= (struct node*)malloc(sizeof(struct node)); scanf("%d",&newnode-> data); newnode-> left=NULL; newnode->right=NULL; if(root==NULL) { root=newnode; } else { temp = root; while(temp) { parent=temp; if(newnode-> data<temp->data) { temp = temp-> left; } else { temp = temp-> right; } } if(newnode-> data< parent->data) { parent-> left = newnode; } else { parent-> right = newnode; } } } void inorder(struct node *T) { if (T==NULL) return; inorder(T->left); printf(" %d ",T->data); inorder(T->right); } int main() { //struct node *root=NULL; int n; printf("n enter no. of term:- "); scanf("%d",&n); while(n!=0) { insert(); n--; } inorder(root); print(“Enter the value to search”); scanf(“%d”, &item); search(item); }
  • 5. void insert() { struct node *temp, *parent; struct node *newnode= (struct node*)malloc(sizeof(struct node)); scanf("%d",&newnode-> data); newnode-> left=NULL; newnode->right=NULL; if(root==NULL) { root=newnode; } else { temp = root; while(temp) { parent=temp; if(newnode-> data<temp->data) { temp = temp-> left; } else { temp = temp-> right; } } if(newnode-> data< parent->data) { parent-> left = newnode; } else { parent-> right = newnode; } } } 30 root 100 100 temp 100 parent 100 while(100), true 20<30, true 400 0 0 20<30, true 0 0 20 200 200 0 0 25 300 while(100), true 25<30, true while(NULL),False 200 while(200),true 200 if 25<20, false while(NULL),False 300 0 0 15 400 if 25<20, false while(100), true 15<30, true while(200),true if15<20, true if15<20, true 0 0 0 40 while(100), true if 40<30, false while(NULL),False 600 600 if 40<30, false
  • 6. void search(int item) { struct node *parent=NULL, *temp=NULL; temp = root; while(temp->data != item) { //parent=temp; if(temp != NULL) { printf("%d ",temp->data); if(item<temp->data ) { temp = temp->left; } else { temp = temp->right; } if(temp == NULL) { printf(“Element Not Found”); break; } } } printf("nSearched value (or) least right node value : %dn",temp->data); } 30 root 100 100 20 200 300 0 0 15 400 400 0 0 40 600 600 0 0 25 300 200 item temp parent 25 100 100 while 30!=25,true if 100!=NULL,true //op 30 25<30,true 200 200==NULL,false while 20!=25,true 200 if 200!=NULL,true //op 20 25<20,false 300 300==NULL,false while 25!=25, False op Searched value (or) least right node value :25
  • 7. void deletion(int item) { struct node *parent=NULL, *temp=NULL; temp = root; search(item); (2) if (temp == NULL) return; if (temp->left == NULL && temp->right == NULL) //No child { if (temp != root) { if (parent->left == temp) parent->left = NULL; else parent->right = NULL; } else root = NULL; free(temp); } else if (temp->left !=NULL&& temp- >right!=NULL) // if 2 child { // find the min value in right sub tree temp1=temp; t1=temp1- >right; while(t1->left!=NULL) { t1=t1->left; } int val = t1->data;(4) deletion(t1->data); temp1->data = val; } else //if one child { struct node* child = (temp->left!=NULL)? temp- >left: temp->right; if (temp != root) { if (temp == parent->left) parent->left = child; temp->left=NULL; else parent->right = child; temp->right=NULL; } else { root = child; } free(temp); } }
  • 8. void deletion(int item) { struct node *parent=NULL, *temp=NULL *temp1,*t1; item,temp,parent =search(item); if (temp == NULL) return; if (temp->left == NULL && temp->right == NULL) //No child { if (temp != root) { if (parent->left == temp) parent->left = NULL; else parent->right = NULL; } else root = NULL; free(temp); } } temp parent 30 root 100 100 20 200 300 0 0 15 400 400 0 0 40 600 600 0 0 25 300 200 300 200 item 25 300==NULL, false 0==NULL&&0==NULL,true, no child condition 400==300,False 300!=100,true (only root node with no child condition) 0 temp parent 30 root 100 100 200 600 100 NULL item 25 100==NULL, false 0==NULL&&0==NULL,true, no child condition 100!=100,false (only root node with no child condition) 0
  • 9. void deletion(int item) { struct node *parent=NULL, *temp=NULL *temp1,*t1; temp = root; item,temp,parent = search(item); if (temp == NULL) return; if (temp->left == NULL && temp->right == NULL) //No child { } else if (temp->left !=NULL&& temp->right!=NULL) // if 2 child { // find the min value in right sub tree temp1=temp; t1=temp1- >right; while(t1->left!=NULL) { t1=t1->left; } int val = t1->data; deletion(t1->data); temp1->data = val; } else // one child { } 30 root 100 100 20 200 300 0 0 15 400 400 300 700 40 600 600 0 0 35 300 200 temp parent 100 NULL item 30 else if 200!=NULL && 600!=NULL, true, 2 child node condition temp1 t1 100 600 0 0 60 700 while(300!=NULL),true, to check whether right sub tree contain left node 300 while(0!=NULL),false value 35 deletion(t1->data); 0 35 100==NULL,False if (200===NULL &&600==NULL),false, no child node condition temp parent 300 600
  • 10. void deletion(int item) { struct node *parent=NULL, *temp=NULL *temp1,*t1; temp = root; item,temp,parent = search(item); if (temp == NULL) return; if (temp->left == NULL && temp->right == NULL) //No child { } else if (temp->left !=NULL&& temp->right!=NULL) // if 2 child { } else // one child { struct node* child = (temp->left!=NULL)? temp- >left: temp->right; if (temp != root) { if (temp == parent->left) { parent->left = child; if(temp->left!=NULL) temp->left=NULL; else temp->right=NULL; else { parent->right = child; if(temp->left!=NULL) temp->left=NULL; else temp->right=NULL; } } else { root = child; } free(temp); } 30 root 100 100 20 200 0 0 0 15 300 400 300 0 40 600 600 0 0 35 400 200 parent 100 item 40 else if 200!=NULL && 600!=NULL, false, 2 child node condition temp 600 child=(300!=NULL)?300:NULL if(600!=100), true else (one child): 100==NULL,False if (200===NULL &&600==NULL),false, no child node condition child= 300 if(600==200), false.enter to else part 300 0 if(300!=NULL), true
  • 11. TRAVERSAL inorder(root) void inorder(struct node *root) { if (root!=NULL) { inorder(root->left); printf(" %d “,root->data); inorder(root->right); } } 7 2 9 1 5 14 r o o t
  • 12. TRAVERSAL preorder(root) void preorder(struct node *root) { if (root!=NULL) { printf(" %d ",root->data); preorder(root->left); preorder(root->right); } } 7 2 9 1 5 14 r o o t
  • 13. TRAVERSAL postorder(root) void postorder(struct node *root) { if (root!=NULL) { postorder(root->left); postorder(root->right); printf(" %d ",root->data); } } 7 2 9 1 5 14 r o o t
  • 14. FIND MAXIMUM void max () { if (root==NULL) return root; else { struct node*temp; temp=root; while(tempright !=NULL) { temp=temp  right; } printf(“n %d”, tempdata); } r o o t Maximum value:14 temp
  • 15. FIND MNIMUM void min () { if (root==NULL) return root; else { struct node*temp; temp=root; while(templeft !=NULL) { temp=temp  left; } printf(“n %d”, tempdata); } temp r o o t Minimum value:1