0% found this document useful (0 votes)
16 views

Binary Search Tree

The document discusses binary search trees and operations on them like insertion, deletion, and searching. It contains algorithms to: 1. Create a binary search tree by inserting nodes maintaining the binary search tree properties. 2. Search for a node in a binary search tree using both recursive and iterative approaches. 3. Delete a node from a binary search tree by handling cases like deleting leaf nodes, nodes with one child, and nodes with two children.

Uploaded by

Sneha Mehta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Binary Search Tree

The document discusses binary search trees and operations on them like insertion, deletion, and searching. It contains algorithms to: 1. Create a binary search tree by inserting nodes maintaining the binary search tree properties. 2. Search for a node in a binary search tree using both recursive and iterative approaches. 3. Delete a node from a binary search tree by handling cases like deleting leaf nodes, nodes with one child, and nodes with two children.

Uploaded by

Sneha Mehta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

ASSIGNMENT

NO 2

Implement dictionary using binary search tree where dictionary stores keywords & its
meanings. Perform following operations:
1. Insert a keyword
2. Delete a keyword
3. Create mirror image and display level wise
4. Copy

20-MAR-19 DATA STRUCTURE-II 1


BINARY
SEARCH
TREES
It is a binary tree.It may be empty.If it is not empty then it satisfies the following properties

 Every element has a unique key.


 The keys in a nonempty left subtree are smaller than the key in the root of subtree.
 The keys in a nonempty right subtree are larger than the key in the root of subtree.
 The left and right subtrees are also binary search trees.

◦ Binary search trees provide an excellent structure for searching a list and at the same time for
inserting and deleting data into the list.

20-MAR-19 DATA STRUCTURE-II 2


BINARY
SEARCH TREE

All < K All > K

20-MAR-19 DATA STRUCTURE-II 3


(a), (b) - complete and balanced trees;
(d) – nearly complete and balanced tree;
(c), (e) – neither complete nor balanced trees
20-MAR-19 DATA STRUCTURE-II 4
20-MAR-19 DATA STRUCTURE-II 5
Algorithm create()
{ else {
allocate memory and accept the data for root node; if(temp->right=NULL)
do {
{ temp->right=curr;
flag=1;
temp=root;
}
flag=0;
else
allocate memory and accept the data for curr node;
move temp to temp->right;
while(flag==0)
} //end else
{
} //end while flag
if(curr->data < temp->data)
Accept choice for adding more nodes;
{ }while(choice =yes); //end do
if(temp->left=NULL) } //end algorithm
{
temp->left=curr;
flag=1;
}
else
move temp to temp->left
} //end if compare
20-MAR-19 DATA STRUCTURE-II 6
ALGORITHM Algorithm search_r(temp, string)
SEARCH () {
{
Initialize f to 0;
Initialize flag=0;
if(temp!=NULL)
Accept string to be {
searched ; if(string
flag=search_r(root,str); =temp->data)
if(flag=1) return 1;
print found; if(string< temp->data)

else f=search_r(te
mp->left, str);
print not
} if(string >temp-
found;
>data)
} f=search_r(te
return f;
mp->right, str);
}

20-MAR-19 DATA STRUCTURE-II 7


Algorithm search_nr()
{
Initialize flag to 0;
temp=root;
Accept string to be searched;
while(flag=0)
{
if(string=temp->data)
{
flag=1; break;
}
else if(string<temp->data)
move temp to temp->left;
else
move temp to temp-
>right;
} //end
while
if(flag=1)
Print found;
else 20-MAR-19 DATA STRUCTURE-II 8
Print not
found;
} //end algo
FUNCTION
DELETEITEM
First, find the item; then, delete it

Important: binary search tree property must be

preserved!! We need to consider three different cases:


(1) Deleting a leaf
(2) Deleting a node with only one child
(3) Deleting a node with two children
(4)20-MAR-19
Deleting the root node DATA STRUCTURE-II 9
(1) DELETING
A LEAF

20-MAR-19 DATA STRUCTURE-II 10


DELETION -
LEAF CASE
Algorithm sets corresponding link of the parent to NULL and disposes the
node

Delete(17) 10

5 15

2 9 20

7 17 30

20-MAR-19 DATA STRUCTURE-II 11


(2) DELETING A NODE WITH
ONLY ONE CHILD
It this case, node is cut from the tree and algorithm links single child (with it's subtree) directly to the
parent
of the removed node.

20-MAR-19 DATA STRUCTURE-II 12


DELETION - ONE
CHILD CASE
Delete(15) 10

5 15

2 9 20

7 30

20-MAR-19 DATA STRUCTURE-II 13


(3) DELETING A NODE
WITH TWO
CHILDREN
(CONTD…)
Find inorder successor
◦ Go to the right child and then move to the left till we get NULL for the leftmost
node

◦ To the inorder’s successor ,attach the left of the node which we want to delete

20-MAR-19 DATA STRUCTURE-II 14


//DELETION
if(curr==root)
OF ROOT
{
if(curr->rightc==NULL)
root=root->leftc;
else if(curr->leftc==NULL)
root=root-
>rightc;
else if(curr->rightc!
=NULL && curr->leftc!
=NULL)
{
temp=cur
r->leftc;
root=curr-
>rightc; s=curr-
>rightc;
20-MAR-19 while(s- DATA STRUCTURE-II 15

>leftc!=NULL)
{
else if(curr!=root) //deletion of node which is not root
{ else if(curr->rightc is NULL) //deletion of
if(curr left and right is NULL ) //deletion of a
a
leaf single child
{ if(parent->leftc==curr) { if(parent->leftc==curr)
parent->leftc=NULL; parent->leftc=curr-
else >leftc; else
parent-
parent->rightc=curr-
} >rightc=NULL;
} >leftc;
else if(curr->leftc is NULL) //deletion of a single child
{
if(parent->leftc==curr)
parent->leftc=curr->rightc;
else
parent->rightc=curr-
>rightc;
}
20-MAR-19 DATA STRUCTURE-II 16
else //deletion of a node having two child
{
s=curr->rightc;
temp=curr->leftc; while(s->leftc!=NULL)
{
s=s->leftc;
}
s->leftc=temp;
if(parent->leftc==curr)
parent->leftc=curr->rightc;
else
parent->rightc=curr->rightc;
}
}
Assign curr left and right to NULL;
delete curr;
}
20-MAR-19 DATA STRUCTURE-II 17

You might also like