Introduction To Algorithms: 6.046J/18.401J/SMA5503
Introduction To Algorithms: 6.046J/18.401J/SMA5503
6.046J/18.401J/SMA5503
Lecture 11
Prof. Erik Demaine
Dynamic order statistics
OS-SELECT(i, S): returns the i th smallest element
in the dynamic set S.
OS-RANK(x, S): returns the rank of x ∈ S in the
sorted order of S’s elements.
key
key
Notation for nodes:
size
size
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 20 L11.2
Example of an OS-tree
M
M
99
CC PP
55 33
AA FF NN QQ
11 33 11 11
DD HH
11 11
7 3 3 4
∴RB-INSERT and RB-DELETE still run in O(lg n) time.
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 20 L11.8
Data-structure augmentation
Methodology: (e.g., order-statistics trees)
1. Choose an underlying data structure (red-
black trees).
2. Determine additional information to be
stored in the data structure (subtree sizes).
3. Verify that this information can be
maintained for modifying operations (RB-
INSERT, RB-DELETE — don’t forget rotations).
4. Develop new dynamic-set operations that use
the information (OS-SELECT and OS-RANK).
These steps are guidelines, not rigid rules.
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 20 L11.9
Interval trees
Goal: To maintain a dynamic set of intervals,
such as time intervals.
i = [7, 10]
low[i] = 7 10 = high[i]
5 11 17 19
4 8 15 18 22 23
int
int
mm
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 20 L11.11
Example interval tree
17,19
17,19
23
23
5,11
5,11 22,23
22,23
18
18 23
23
4,8
4,8 15,18
15,18
88 18
18
high[int[x]]
7,10
7,10 m[x] = max m[left[x]]
10
10
m[right[x]]
30
30 14
14 14
14 19
19