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

Lecture-20- BST-1

Binanry Search Trees

Uploaded by

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

Lecture-20- BST-1

Binanry Search Trees

Uploaded by

abdulsammad1305
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Dr.

Muhammad Shuaib Qureshi


Binary Search Tree
Binary Search Tree -
▪ Binary Search Tree is a node-based binary tree data structure which has the
following properties:
i. The left subtree of a node contains only nodes with keys lesser than the node’s key.
ii. The right subtree of a node contains only nodes with keys greater than the node’s key.
iii. The left and right subtree each must also be a binary search tree.
Binary Search Tree Construction
Binary Search Tree -
▪ Construct a Binary Search Tree (BST) for the following sequence of numbers-

50, 70, 60, 20, 90, 10, 40, 100


Example -
▪ When elements are given in a sequence,
• Always consider the first element as the root node.
• Consider the given elements and insert them in the BST one by one.
▪ The binary search tree will be constructed as explained below-
▪ Insert 70 –
▪ As 70 > 50, so insert 70 to the right of 50.
Binary Search Tree -
▪ Construct a Binary Search Tree (BST) for the following sequence of numbers-

50, 70, 60, 20, 90, 10, 40, 100

▪ 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-

50, 70, 60, 20, 90, 10, 40, 100

• 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-

50, 70, 60, 20, 90, 10, 40, 100

• 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)

10 , 20 , 30 , 100 , 150 , 200 , 300

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:

Consider key = 7 has to be searched in the given BST-

Step 1: 7 IS SMALLER THAN 20: GO LEFT


Step 2: 7 IS GREATER THAN 5: GO RIGHT
Step 3: 7 IS SMALLER THAN 15: GO LEFT
Step 4: 7 IS SMALLER THAN 9: GO LEFT
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-
Consider key = 45 has to be searched in the given BST

▪ We start our search from the root node 25.


▪ As 45 > 25, so we search in 25’s right subtree.
▪ As 45 < 50, so we search in 50’s left subtree.
▪ As 45 > 35, so we search in 35’s right subtree.
▪ As 45 > 44, so we search in 44’s right subtree but 44 has no subtrees.

▪ So, we conclude that 45 is Not Present in the above BST.


Binary Search Tree Searching
▪ Consider key = 27 has to be searched in the given BST and in Sorted array.

▪ The key = 27 is searched in the


given BST in 03 steps.

▪ 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.

We start searching for value 40 from the root node 100.

▪ As 40 < 100, so we search in 100’s left subtree.


▪ As 40 > 20, so we search in 20’s right subtree.
▪ As 40 > 30, so we add 40 to 30’s right subtree.
DELETION
Binary Search Tree Deletion -
▪ When it comes to deleting a node from the binary search tree, following
three cases are possible.

▪ 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 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

▪ If a node is at ith index


▪ Left child would be at : [(2 × i) + 1]
▪ Right child would be at : [(2 × i) + 2]
(𝑖−1)
▪ Parent would be at : 2
▪ Left Child of A = [(2 × 0) + 1] = 1
▪ Left Child of F = [(2 × 4) + 1] = 9, greater than highest index value (8),
hence no right left child of F node 0
2
1

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

You might also like