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

Binary search tree.pptx

  • 1.
    DATA STRUCTURES Binary SearchTree 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 • Thebasic 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; structnode*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) { structnode *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) { structnode *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) { structnode *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) { structnode *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) { structnode *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