Lecture-20- BST-1
Lecture-20- BST-1
▪ Insert 60 –
▪ As 60 > 50, so insert 60 to the right of 50.
▪ As 60 < 70, so insert 60 to the left of 70.
▪ Insert 20 –
▪ As 20 < 50, so insert 20 to the left of 50.
Binary Search Tree -
▪ Construct a Binary Search Tree (BST) for the following sequence of numbers-
• Insert 90 –
▪ As 90 > 50, so insert 90 to the right of 50.
▪ As 90 > 70, so insert 90 to the right of 70.
• Insert 10 –
▪ As 10 < 50, so insert 10 to the left of 50.
▪ As 10 < 20, so insert 10 to the left of 20.
Binary Search Tree -
▪ Construct a Binary Search Tree (BST) for the following sequence of numbers-
• Insert 40 –
▪ As 40 < 50, so insert 40 to the left of 50.
▪ As 40 > 20, so insert 40 to the right of 20.
• Insert 100 –
▪ As 100 > 50, so insert 100 to the right of 50.
▪ As 100 > 70, so insert 100 to the right of 70.
▪ As 100 > 90, so insert 100 to the right of 90.
Binary Search Tree -
Practice
Binary Search Tree -
▪ A binary search tree is generated by inserting in order of the following
integers-
21, 28, 14, 32, 25, 18, 11, 30, 19, 15
Binary Search Tree -
▪ A binary search tree is generated by inserting in order of the following
integers-
21, 28, 14, 32, 25, 18, 11, 30, 19, 15
Practice
Binary Search Tree -
▪ A binary search tree is generated by inserting in order of the following
integers-
50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24
Binary Search Tree Construction -
▪ A binary search tree is generated by inserting in order of the following
integers-
50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24
Binary Search Tree -
▪ A binary search tree is generated by inserting in order of the following
integers-
30 , 20 , 10 , 15 , 25 , 23 , 39 , 35 , 42
?
BINARY SEARCH Tree
OPERATIONS
Binary Search Tree Operations
▪ Commonly performed operations on binary search tree are-
1. Traversing Operation
2. Search Operation
3. Insertion Operation
4. Deletion Operation
TRAVERSING
Binary Search Tree Traversal -
▪ A Binary Search Tree traversal is same as binary tree traversal.
Example:
▪ Consider the following binary search tree -
Preorder Traversal-
(Root → Left → Right)
100 , 20 , 10 , 30 , 200 , 150 , 300
Inorder Traversal-
(Left → Root → Right)
Postorder Traversal-
(Left → Right → Root)
10 , 30 , 20 , 150 , 300 , 200 , 100
Practice
Binary Search Tree Traversal -
Binary Search Tree Traversal -
Pre-order:
25, 15, 10, 4, 12, 22, 18, 24, 50, 35, 31, 44, 70, 66, 90
In-order:
4, 10, 12, 15, 18, 22, 24, 25, 31, 35, 44, 50, 66, 70, 90
Post-order:
4, 12, 10, 18, 24, 22, 15, 31, 44, 35, 66, 90, 70, 50, 25
SEARCHING
Binary Search Tree Searching
RULES
For searching a given key in the BST,
▪ Compare the key with the value of root node.
▪ If the key is present at the root node, then return the root node.
▪ If the key is greater than the root node value, then recur for the root node’s right
subtree.
▪ If the key is smaller than the root node value, then recur for the root node’s left
subtree.
Example:
Example-
Consider key = 45 has to be searched in the given BST
▪ While as it is searched in
Sorted array in 10 steps.
Practice
Search 23 in the following tree
INSERTION
Binary Search Tree Insertion
▪ Consider the following example where key = 40 is inserted in the given BST.
Example-
▪ Consider the following example where node with key = 20 is deleted from the
BST.
Binary Search Tree Deletion -
▪ Case-02: Deletion of a Node Having Only One Child.
• Just remove / disconnect the node that has a single child.
Example-
▪ Consider the following example where node with key = 30 is deleted
from the BST.
Binary Search Tree Deletion
▪ Case-03: Deletion of a Node Having Only Two Children.
• A node with two children may be deleted from the BST in the following
two ways
Method 01:
• Visit to the right subtree of the deleting node.
• Pluck the least value element called as inorder successor.
• Replace the deleting element with its inorder successor.
Example -
• Consider the following example where node with key = 15 is deleted from the BST.
• The inorder successor of 15 is 16.
• Replace 15 by 16.
Binary Search Tree Deletion
Method 02-
• Visit to the left subtree of the deleting node.
• Pluck the greatest value element called as inorder predecessor.
• Replace the deleting element with its inorder predecessor.
Example -
• Consider the following example where node with key = 15 is deleted from the BST.
Practice
Binary Search Tree Deletion
• Delete key = 11
11
6 19
4 8 17 43
31 49
5
10
Binary Search Tree Deletion
• Delete 11
• In order Successor
17
6 19
4 8 43
31 49
5
10
Binary Search Tree Deletion
• Delete 20
Binary Search Tree Deletion -
• Delete 20
• In order Predecessor
Representation of Tree in Computer
4 5
3 6
7 8
Representation of Tree in Computer
0
▪ If a node is at ith index
▪ Left child would be at : [(2 × i) + 1]
1 2
▪ Right child would be at : [(2 × i) + 2]
(𝑖−1)
▪ Parent would be at : 2
3 6
7 8
0 1 2 3 4 5 6 7 8
66 50 21 94 75 47 18