Lecture 06 - Binary Search Tree (BST) - Design Analysis of Algorithm
Lecture 06 - Binary Search Tree (BST) - Design Analysis of Algorithm
H oney B ear B ra n d y
leaf
Balance Tree
A binary tree is said to be ‘balanced’ if the tree height is O(logN), where ‘N’ is the number of nodes.
A A 1 A
B B 2 B C
C
3 D E F G
D
4 H I
E 5
B C B C
D E F G D E F G
H I J K L M N O
H I
data
left_child data right_child
left_child right_child
13
If y is in left subtree of x,
then key [y] ≤ key [x] 5
3 7
If y is in right subtree of x, 2 5 9
15
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Binary Search Trees
Support many dynamic set operations
On average: (lgn)
The expected height of the tree is lgn
In the worst case: (n)
The tree is a linear chain of n nodes
16
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Worst Case
(n)
17
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Traversing a Binary Search Tree
In-order tree walk:
Root is printed between the values of its left and right subtrees: left, root, right
Keys are printed in sorted order
Preorder tree walk:
root printed first: root, left, right
Post-order tree walk:
root printed last: left, right, root
5 Inorder: 2 3 5 5 7 9
Preorder: 5 3 2 5 7 9
3 7
2 5 9 Postorder: 2 5 3 9 7 5
3 7
Output: 2 3 5 5 7 9
2 5 9
Running time:
(n), where n is the size of the tree rooted at
Design & Analysis of Algorithm
x @copyright – The Islamia University of Bahawalpur
Searching for a Key
Given a pointer to the root of a tree and a key k:
Return a pointer to a node with key k 5
if one exists
3 7
Otherwise return NIL
2 4 9
Idea
Starting at the root: trace down a path by comparing
k with the key of the current node:
If the keys are equal: we have found the key
If k < key[x] search in the left subtree of x
If k > key[x] search in the right subtree of x
20
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Example: TREE-SEARCH
15
Search for key 13:
6 18 15 6 7 13
3 7 17 20
2 4 13
9
21
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Searching for a Key
Alg: TREE-SEARCH(x, k) 5
22
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Finding the Minimum in a Binary Search Tree
Goal: find the minimum value in a BST
Following left child pointers from the root, until a NIL
is encountered
Alg: TREE-MINIMUM(x) 15
1. while left [x] NIL
2. do x ← left [x] 6 18
3. return x 3 7 17 20
2 4 13
Running time: O(h), h – height of tree 9
Minimum = 2
2. do x ← right [x]
6 18
3. return x
3 7 17 20
2 4 13
Running time: O(h), h – height of tree
9
Maximum = 20
12 12
x y
5 18 5 18
2 9 15 19 2 9 15 19
1 3 17 1 3 13 17
x = NIL
y = 15
15 15
5 16 5 16
3 12 20 3 12 20
z
10 13 18 23 10 18 23
6 delete 6
7 7
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Deletion
Case 2: z has one child
Delete z by making the parent of z point to z’s child,
instead of to z
15 delete 15
z
5 16 5 20
3 12 20 3 12 18 23
10 13 18 23 10
6 6
7 7
End
Lecture – 6
Binary Search Tree (BST)