ETCS-209: DATA Structure Odd Semester 2020: Binary Search Tree
ETCS-209: DATA Structure Odd Semester 2020: Binary Search Tree
STRUCTURE
ODD SEMESTER 2020
Binary Search Tree
3
BINARY SEARCH TREES
• Binary search tree is a data structure that quickly allows
us to maintain a sorted list of numbers.
4
BST
The properties that separate a binary search tree from a regular
binary tree is
1)All nodes of left subtree are less than the root node
2)All nodes of right subtree are more than the root node
3)Both subtrees of each node are also BSTs i.e. they have the
above two properties
A tree having a right subtree with one value smaller than the
root is shown to demonstrate that it is not a valid binary 5
BST
6
BST
7
8
Difference between BT and BST
• A binary tree is simply a tree in which each node can have at most
two children.
• A binary search tree is a binary tree in which the nodes are
assigned values, with the following restrictions :
1)No duplicate values.
2)The left subtree of a node can only have values less than the node
3)The right subtree of a node can only have values greater than the
node and recursively defined
4)The left subtree of a node is a binary search tree.
5)The right subtree of a node is a binary search tree.
9
BST Operations
Four basic BST operations
10
Preorder Traversal
11
Postorder Traversal
Left Right Root
12 20 18 35 52 44 23
12
Inorder Traversal
12 18 20 23 35 44 52
13
BST
Construct a Binary Search Tree (BST) for the following
sequence of numbers-
14
BST
The binary search tree will be constructed as explained
below-
Insert 50-
Insert 70-
As 70 > 50, so insert 70 to the right
of 50.
15
BST
Insert 60-
16
BST
Insert 20-
17
BST
Insert 90-
18
BST
Insert 10-
As 10 < 50, so insert 10 to the left of 50.
As 10 < 20, so insert 10 to the left of 20.
19
BST
Insert 40-
20
BST
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.
21
Question
Problem-01:
22
ANSWER
23
BST OPERATIONS
24
Search Operation-
25
Consider key = 45 has to be searched in the given BST-
27
Search in a BST: C code
Rules-
29
Consider the following example where key = 40
is inserted in the given BST-
30
BST Operations: Insertion
5 9 10 > 9
4 6 8 10
Insertion in BST ‐ Pseudocode
if tree is empty
create a root node with the new key
else
compare key with the top node if key = node key
replace the node with the new value else if key >
node key
compare key with the right subtree:
if subtree is empty create a leaf node else add key
in right subtree
else key < node key
compare key with the left subtree:
if the subtree is empty create a leaf node else add
key to the left subtree
Insertion in BST – C‐code
34
Case-01:
Deletion Of A Node Having No Child (Leaf Node)-
Just remove / disconnect the leaf node that is to deleted from
the tree.
Example-
Consider the following example where node with value = 20
is deleted from the BST-
35
Case-02:
Deletion Of A Node Having Only One Child-
Just make the child of the deleting node, the child of its
grandparent.
Example-
Consider the following example where node with value = 30 is
deleted from the BST-
36
CASE 2 DELETION(ONE CHILD)
38
Case-03:
Deletion Of A Node Having Two Children-
Method-01:
1)Visit to the right subtree of the deleting node.
2) Pluck the least value element called as inorder successor.
3) Replace the deleting element with its inorder successor.
Consider the following example where node with value = 15
is deleted from the BST-
39
Case-03:
Deletion Of A Node Having Two Children-
Method-02:
1)Visit to the left subtree of the deleting node.
2)Pluck the greatest value element called as inorder predecessor.
3)Replace the deleting element with its inorder predecessor.
Example-
Consider the following example where node with value = 15 is
deleted from the BST-
40
DELETION
42
/* Function to delete the given node */
struct node* delete_node(struct node* root, int data)
{
if (root == NULL)
return root;
// If the key to be deleted is smaller than the root's key,
if (data < root->data)
root->left = delete_node(root->left, data);
// If the key to be deleted is greater than the root's key,
else if (data > root->data)
root->right = delete_node(root->right, data);
43
44
Time Complexity-
45
Best case
46
Yes, certain orders might produce very unbalanced trees!
47
Unbalanced trees are not desirable because search time
increases!
48
Types of BST
49
Applications
Binary Search Tree Applications
50
Applications
51