Tut 10
Tut 10
Tutorial 10
1/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Binary Search Tree Example
Two possible BSTs on S = {3, 11, 12, 15, 18, 29, 40, 41, 47, 68, 71, 92}:
3
11
12
92
40
71
15 68
68
11 29 41 92 15
18
3 12 18 47 71
47
41
40
29
2/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Predecessor Query
3/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Example
Suppose that S = {3, 11, 12, 15, 18, 29, 40, 41, 47, 68, 71, 92} and we have
a balanced BST T on S:
40
15 68
11 29 41 92
3 12 18 47 71
4/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Successor Query
5/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Example
15 68
11 29 41 92
3 12 18 47 71
6/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Construction of a Balanced BST
7/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Construction of a Balanced BST
Main idea:
The median A[b n+1
2
c]
8/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Example
29
12 47
3 15 40 71
11 18 41 68 92
A 3 11 12 15 18 29 40 41 47 68 71 92
9/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Construction of a Balanced BST
Let f (n) be the maximum running time for constructing a balanced BST
from an array of length n. We have:
f (1) = O(1)
f (n) = O(1) + 2 · f (⌈n/2⌉)
Solving the recurrence gives f (n) = O(n).
10/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Rebalancing
h h+2 h h+2
h or h + 1 h+1 h+1 h
11/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Right-Right
a b
h h+2
x+1 h+1
b
a
x h+1
h x
A =⇒
B A B
C
12/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Right-Left
a c
h h+2 h h+2 h+1 h+1
b a b
h x y h
h+1 h h
⇒ ⇒
A c
x y
A B C D
D
B C
Inserting 50:
20 20 30
1 ? 1 3 2 2
10 30 10 30 20 40
1 ? 1 2 1 1 0 1
⇒ ⇒
25 40 25 40 10 25 50
? 0 1
50 50
14/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Right-Left Example
Inserting 38:
30 30 35
2 ? 2 4 3 3
20 40 20 40 30 40
1 1 ? 2 1 1 3 2 2 1 2 2
10 25 35 50 10 25 35 50 20 33 37 50
1 ? 0 1 1 2 0 1 1 1 0 1 0 1
⇒ ⇒
33 37 60 33 37 60 10 25 38 60
0 ? 0 1
38 38
15/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Range Reporting
30 40
20 33 37 50
10 25 38 60
For the query [27, +∞), we need to report the integers in pink.
16/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Range Reporting
T1 T2
17/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
Range Reporting
The tutor will explain the algorithm using [27, +∞) as the example query.
35
30 40
20 33 37 50
10 25 38 60
18/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10
In each level of the recursion, we do the following:
Compare q to the integer stored in the root, the cost of which is
O(1).
(If necessary) report all the integers in the right subtree, the cost of
which is proportional to the number of integers in the right subtree.
As the height of the BST is O(log n), the first bullet costs O(log n) in
total. The second step reports integers from disjoint subtrees and,
therefore, incurs cost O(k) in total. The overall cost is O(log n + k)
19/19
CSCI2100, The Chinese University of Hong Kong Tutorial 10