Professional Documents
Culture Documents
Lesson 5 - Binary Search Tree (BST)
Lesson 5 - Binary Search Tree (BST)
Lesson 5 - Binary Search Tree (BST)
01/19/22 2
Binary Search Trees
BST Property
Let x be a node in a binary search tree.
If y is a node in the left subtree of x, then
key of y ≤ key of x.
If y is a node in the right subtree of x,
then key of x ≤ key of y.
01/19/22 3
Binary Search Trees
Ex.
22
16 55
13 20 30 90
9 21 25 40
01/19/22 4
Binary Search Trees
Counter Example
17
16 55
13
! 20 30 90
9 21 25 40
01/19/22 5
Binary Search Trees
The best and worst case time complexity of most BST
operations is proportional to the height of the tree.
6 2
3 7 3
2 5 8 7
2 BSTs
The one on the left is more efficient than the one 5 8
on the right and both contain the same keys.
5
01/19/22 6
Binary Search Trees
TREE-SEARCH(T,key)
if T = nil return ‘not found’
else if key = Tdata return ‘found’
else if (key < Tdata) TREE-SEARCH(Tleft,key)
else TREE-SEARCH(Tright,key)
2 5 8
01/19/22 7
Binary Search Trees
Ex. 7
3 9
1 4 8 16
2 10
01/19/22 8
Binary Search Trees
TREE-INSERT(T,z)
// z - node to be inserted with left and right set to nil
y nil; x T
while x <> nil {
yx
if zdata < xdata x xleft
else x xright
}
if y = nil T z
6
else if zdata < ydata yleft z
else yright z
3 7
2 5 8
01/19/22 9
Binary Search Trees
TREE-INSERT(T,z)
// z - node to be inserted with left and right set to nil
if T = nil T z
else if zdata = Tdata return ‘duplicate’
else if (zdata < Tdata) TREE-INSERT(Tleft,z)
else TREE-INSERT(Tright,z)
2 5 8
01/19/22 10
Binary Search Trees
Ex. After inserting 13
12
5 18
2 9 15 19
13 17
01/19/22 11
Binary Search Trees
Create a BST by inserting each of the
given values:
1. 6, 7, 30, 43, 28, 25, 10, 2, 1
2. I, B, C, G, Y, H, E, T, O
01/19/22 12
Binary Search Trees
TREE-DELETE(T,z)
// z - node to be deleted
Cases:
1. z has no children
6
2. z has one child
3. z has children 3 7
2 5 8
01/19/22 13
Binary Search Trees
Case 1: z has no children 15
Delete 13 5 16
3 12 20
z
10 13 18 23
7
01/19/22 14
Binary Search Trees
Result: 15
5 16
3 12 20
10 18 23
7
01/19/22 15
Binary Search Trees
Case 2: z has one child 15
Delete 16 5 16
3 12 20
10 13 18 23
7
01/19/22 16
Binary Search Trees
Result: 15
5 20
3 12 18 23
10 13
7
01/19/22 17
Binary Search Trees
Case 3: z has children 15
z
Delete 5 5 16
3 12 20
7
01/19/22 18
Binary Search Trees
Result:
15
6 16
3 12 20
10 13 18 23
01/19/22 19
Binary Search Trees
Ex. Delete 25
12
5 18
3 6 15 30
2 7 17 28 43
1 10 25
01/19/22 20
Binary Search Trees
Ex. Delete 6
12
5 18
3 6 15 30
2 7 17 28 43
1 10
01/19/22 21
Binary Search Trees
Ex. Delete 12
12
5 18
Successor of 12
3 7 15 30
2 10 17 28 43
01/19/22 22
Binary Search Trees
Ex. Delete 12
15 Successor of 15
5 18
3 7 17 30
2 10 28 43
01/19/22 23
Binary Search Trees
TreeDelete(T,z)
if zleft = nil or zright = nil y z
else y = TREE-SUCCESSOR(z)
if yleft <> nil x yleft
else x yright
if x <> nil xparent yparent
if yparent = nil T x
elseif y = yparentleft yparentleft x
else yparentright x
if y <> z {
zdata ydata; copy y’s satellite data into z
}
return y
01/19/22 24
Binary Search Trees
Binary search trees can be used:
1. as a dictionary
2. in databases
3. as a priority queue
01/19/22 25
Binary Search Trees
A. Create a BST by performing the following
operations :
1. INSERT : 50, 9, 16, 3, 10, 14, 1, 4, 2, 8, 30,
70, 55, 25, 15, 20
DELETE : Root, 10, Root
2. Insert : M, K , L , O, P , A, H, R, B, C, G, E, S,
D, T
DELETE : M, H, ROOT, ROOT