Docsity

Augmenting Data Structures - Introduction to Algorithms - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Augmenting Data Structures - Introduction to Algorithms - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Algorithms

Augmenting Data Structures:

Interval Trees

Review: Dynamic Order Statistics

● We’ve seen algorithms for finding the i th

element of an unordered set in O( n ) time

● OS-Trees : a structure to support finding the i th

element of a dynamic set in O(lg n ) time

■ Support standard dynamic set operations

(Insert(), Delete(), Min(), Max(),

Succ(), Pred())

■ Also support these order statistic operations:

void OS-Select(root, i );
int OS-Rank( x );

Review: OS-Select

● Example: show OS-Select( root , 5):

M 8

C 5

P 2

Q 1

A 1

F 3

D 1

H 1

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);

}

Review: OS-Select

● Example: show OS-Select( root , 5):

M 8

C 5

P 2

Q 1

A 1

F 3

D 1

H 1

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);

}

i = 5 r = 6

Review: OS-Select

● Example: show OS-Select( root , 5):

M 8

C 5

P 2

Q 1

A 1

F 3

D 1

H 1

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);

}

i = 5 r = 6

i = 5 r = 2

i = 3 r = 2

Review: OS-Select

● Example: show OS-Select( root , 5):

M 8

C 5

P 2

Q 1

A 1

F 3

D 1

H 1

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);

}

i = 5 r = 6

i = 5 r = 2

i = 3 r = 2

i = 1 r = 1

Review: Determining The

Rank Of An Element

M
C
P
Q
A
F
D
H

OS-Rank(T, x)

{

r = x->left->size + 1;

y = x; while (y != T->root)

if (y == y->p->right)

r = r + y->p->left->size + 1;

y = y->p;

return r;

}

Idea: rank of right child x is one

more than its parent’s rank, plus

the size of x ’s left subtree

Review: Determining The

Rank Of An Element

M
C
P
Q
A
F
D
H

OS-Rank(T, x)

{

r = x->left->size + 1;

y = x; while (y != T->root)

if (y == y->p->right)

r = r + y->p->left->size + 1;

y = y->p;

return r;

}

Example 1:

find rank of element with key H

y
r = 1

Review: Determining The

Rank Of An Element

M
C
P
Q
A
F
D
H

OS-Rank(T, x)

{

r = x->left->size + 1;

y = x; while (y != T->root)

if (y == y->p->right)

r = r + y->p->left->size + 1;

y = y->p;

return r;

}

Example 1:

find rank of element with key H

r = 1
r = 3
y
r = 3+1+1 = 5

Review: Determining The

Rank Of An Element

M
C
P
Q
A
F
D
H

OS-Rank(T, x)

{

r = x->left->size + 1;

y = x; while (y != T->root)

if (y == y->p->right)

r = r + y->p->left->size + 1;

y = y->p;

return r;

}

Example 1:

find rank of element with key H

r = 1
r = 3
r = 5
y
r = 5

Review: Determining The

Rank Of An Element

M
C
P
Q
A
F
D
H

OS-Rank(T, x)

{

r = x->left->size + 1;

y = x; while (y != T->root)

if (y == y->p->right)

r = r + y->p->left->size + 1;

y = y->p;

return r;

}

Example 2:

find rank of element with key P

r = 1
y
r = 1 + 5 + 1 = 7

Review: Maintaining Subtree Sizes

● So by keeping subtree sizes, order statistic

operations can be done in O(lg n) time

● Next: maintain sizes during Insert() and

Delete() operations

■ Insert(): Increment size fields of nodes traversed

during search down the tree

■ Delete(): Decrement sizes along a path from the

deleted node to the root

■ Both: Update sizes correctly during rotations

Review: Methodology For

Augmenting Data Structures

● Choose underlying data structure

● Determine additional information to maintain

● Verify that information can be maintained for

operations that modify the structure

● Develop new operations

Interval Trees

● The problem: maintain a set of intervals

■ E.g., time intervals for a scheduling program:

7 10

5 11

4 8 15 18 21 23

17 19

i = [7,10]; i → low = 7; i → high = 10