Lecture 21
Lecture 21
Lecture 21
including x itself:
M
8
C
5
P
2
A
1
F
3
D
1
Q
1
H
1
Review: OS-Select
Example: show OS-Select(root, 5):
OS-Select(x, i)
{
r = x->left->size + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(x->left, i);
else
return OS-Select(x->right, i-r);
}
M
8
C
5
A
1
P
2
F
3
D
1
Q
1
H
1
Review: OS-Select
Example: show OS-Select(root, 5):
OS-Select(x, i)
{
r = x->left->size + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(x->left, i);
else
return OS-Select(x->right, i-r);
}
M
8
i=5
r=6
C
5
A
1
P
2
F
3
D
1
Q
1
H
1
Review: OS-Select
Example: show OS-Select(root, 5):
OS-Select(x, i)
{
r = x->left->size + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(x->left, i);
else
return OS-Select(x->right, i-r);
}
M
8
C
5
A
1
i=5
r=6
i=5
r=2
P
2
F
3
D
1
Q
1
H
1
Review: OS-Select
Example: show OS-Select(root, 5):
OS-Select(x, i)
{
r = x->left->size + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(x->left, i);
else
return OS-Select(x->right, i-r);
}
M
8
C
5
A
1
i=5
r=2
F
3
D
1
i=5
r=6
P
2
i=3
r=2
H
1
Q
1
Review: OS-Select
Example: show OS-Select(root, 5):
OS-Select(x, i)
{
r = x->left->size + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(x->left, i);
else
return OS-Select(x->right, i-r);
}
M
8
C
5
A
1
i=5
r=2
F
3
D
1
i=5
r=6
P
2
i=3
r=2
H
1
Q
1
i=1
r=1
Review: OS-Select
Example: show OS-Select(root, 5):
OS-Select(x, i)
{
r = x->left->size + 1;
if (i == r)
return x;
else if (i < r)
return OS-Select(x->left, i);
else
return OS-Select(x->right, i-r);
}
M
8
C
5
A
1
i=5
r=2
F
3
D
1
i=5
r=6
P
2
i=3
r=2
H
1
Q
1
i=1
r=1
M
8
C
5
OS-Rank(T, x)
A
F
{
1
3
r = x->left->size + 1;
y = x;
D
1
while (y != T->root)
if (y == y->p->right)
r = r + y->p->left->size + 1;
y = y->p;
return r;
}
P
2
Q
1
H
1
M
8
C
5
OS-Rank(T, x)
A
F
{
1
3
r = x->left->size + 1;
y = x;
D
1
while (y != T->root)
if (y == y->p->right)
r = r + y->p->left->size + 1;
y = y->p;
return r;
}
P
2
Q
1
H
1
y
r=1
M
8
C
5
OS-Rank(T, x)
A
F
{
1
3
r = x->left->size + 1;
y = x;
D
1
while (y != T->root)
if (y == y->p->right)
r = r + y->p->left->size + 1;
y = y->p;
return r;
}
P
2
y
r = 1+1+1 = 3
H
1
r=1
Q
1
M
8
C
5
y
r = 3+1+1 = 5
OS-Rank(T, x)
A
F
{
1
3
r = x->left->size + 1;
y = x;
D
1
while (y != T->root)
if (y == y->p->right)
r = r + y->p->left->size + 1;
y = y->p;
return r;
}
P
2
Q
1
r=3
H
1
r=1
y
r=5
M
8
C
5
P
2
r=5
OS-Rank(T, x)
A
F
{
1
3
r = x->left->size + 1;
y = x;
D
1
while (y != T->root)
if (y == y->p->right)
r = r + y->p->left->size + 1;
y = y->p;
return r;
}
Q
1
r=3
H
1
r=1
M
8
C
5
OS-Rank(T, x)
A
F
{
1
3
r = x->left->size + 1;
y = x;
D
1
while (y != T->root)
if (y == y->p->right)
r = r + y->p->left->size + 1;
y = y->p;
return r;
}
P
2
y
r=1
Q
1
H
1
M
8
y
r=1+5+1=7
C
5
OS-Rank(T, x)
A
F
{
1
3
r = x->left->size + 1;
y = x;
D
1
while (y != T->root)
if (y == y->p->right)
r = r + y->p->left->size + 1;
y = y->p;
return r;
}
P
2
r=1
Q
1
H
1
x
19
rightRotate(y)
7
leftRotate(x)
y
12
Interval Trees
The problem: maintain a set of intervals
E.g., time intervals for a scheduling program:
7
10
5
4
17
15
19
18 21
23
Interval Trees
The problem: maintain a set of intervals
E.g., time intervals for a scheduling program:
7
10
17
19
query interval
[14,16] [15,18]
[16,19] [15,18] or [17,19]
[12,14] NULL
Interval Trees
Following the methodology:
Pick underlying data structure
Decide what additional information to store
Figure out how to maintain the information
Develop the desired new operations
Interval Trees
Following the methodology:
Pick underlying data structure
Red-black trees will store intervals, keyed on ilow
Decide what additional information to store
Figure out how to maintain the information
Develop the desired new operations
Interval Trees
Following the methodology:
Pick underlying data structure
Red-black trees will store intervals, keyed on ilow
Decide what additional information to store
We will store max, the maximum endpoint in the subtree
rooted at i
Figure out how to maintain the information
Develop the desired new operations
Interval Trees
int
max
[17,19]
[5,11]
[4,8]
[21,23]
[15,18]
[7,10]
Interval Trees
int
max
[17,19]
23
[5,11]
18
[4,8]
8
[21,23]
23
[15,18]
18
[7,10]
10
Note that:
x high
Interval Trees
Following the methodology:
Pick underlying data structure
Red-black trees will store intervals, keyed on ilow
Interval Trees
[11,35]
35
[6,20]
20
14
rightRotate(y)
30
19
[6,20]
???
leftRotate(x)
???
[11,35]
???
???
???
Interval Trees
[11,35]
35
[6,20]
20
14
[6,20]
???
rightRotate(y)
30
leftRotate(x)
[11,35]
???
14
19
19
30
Interval Trees
[11,35]
35
[6,20]
20
14
[6,20]
35
rightRotate(y)
30
leftRotate(x)
19
[11,35]
35
14
19
30
Interval Trees
Following the methodology:
Pick underlying data structure
Red-black trees will store intervals, keyed on ilow
IntervalSearch() Example
Example: search for interval
overlapping [14,16]
IntervalSearch(T, i)
{
x = T->root;
[17,19]
23
[5,11]
18
[4,8]
8
[21,23]
23
[15,18]
18
[7,10]
10
IntervalSearch() Example
Example: search for interval
overlapping [12,14]
IntervalSearch(T, i)
{
x = T->root;
[17,19]
23
[5,11]
18
[4,8]
8
[21,23]
23
[15,18]
18
[7,10]
10
Correctness of IntervalSearch()
Key idea: need to check only 1 of nodes 2
children
Case 1: search goes right
Show that overlap in right subtree, or no overlap at all
Correctness of IntervalSearch()
Case 1: if search goes right, overlap in the right
Correctness of IntervalSearch()
Case 2: if search goes left, overlap in the left