augmenting data structures advanced algorithms & data structures lecture theme 07 – part i...

59
Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

Upload: bradyn-gilder

Post on 14-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

Augmenting Data Structures

Advanced Algorithms & Data Structures

Lecture Theme 07 – Part I

Prof. Dr. Th. Ottmann

Summer Semester 2006

Page 2: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

2

Augmentation is a process of extending a data structure in order to support additional

functionality. It consists of four steps:

1. Choose an underlying data structure.

2. Determine the additional information to be maintained in the underlying data

structure.

3. Verify that the additional information can be maintained for the basic modifying

operations on the underlying data structure.

4. Develop new operations.

Augmentation

Process

Page 3: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

3

Examples for Augmenting DS

• Dynamic order statistics: Augmenting binary search trees by size information

• D-dimensional range trees: Recursive construction of (static) d-dim range trees

• Min-augmented dynamic range trees: Augmenting 1-dim range trees by min-

information

• Interval trees

• Priority search trees

Page 4: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

4

Examples for Augmenting DS

• Dynamic order statistics: Augmenting binary search trees by size information

• D-dimensional range trees: Recursive construction of (static) d-dim range trees

• Min-augmented dynamic range trees: Augmenting 1-dim range trees by min-

information

• Interval trees

• Priority search trees

Page 5: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

5

Problem: Given a set S of numbers that changes under insertions and deletions,

c

o

n

s

t

r

u

c

t

a

d

a

t

a

s

t

r

u

c

t

u

r

e

t

o

s

t

o

r

e

S

t

h

a

t

c

a

n

b

e

u

p

d

a

t

e

d

i

n

O

(

l

o

g

n

)

t

i

m

e

a

n

d

t

h

a

t

c

a

n

r

e

p

o

r

t

t

h

e

k

-

t

h

o

r

d

e

r

s

t

a

t

i

s

t

i

c

f

o

r

a

n

y

k

i

n

O

(

l

o

g

n

)

t

i

m

e

.

51 85

1334

227

14

48

5

S

Dynamic Order Statistics

Page 6: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

6

Binary

Search

Trees and

Order

Statistics

1

5 13

7

17

19

37

25

33

49

18

Page 7: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

7

Binary

Search

Trees and

Order

Statistics

1

5 13

7

17

19

37

25

33

49

18

R

e

t

r

i

e

v

i

n

g

a

n

e

l

e

m

e

n

t

w

i

t

h

a

g

i

v

e

n

r

a

n

k

:

F

o

r

a

g

i

v

e

n

i

,

f

i

n

d

t

h

e

i

-

t

h

s

m

a

l

l

e

s

t

k

e

y

i

n

t

h

e

s

e

t

.

D

e

t

e

r

m

i

n

i

n

g

t

h

e

r

a

n

k

o

f

a

n

e

l

e

m

e

n

t

:

F

o

r

a

g

i

v

e

n

(

p

o

i

n

t

e

r

t

o

a

)

k

e

y

k

,

d

e

t

e

r

m

i

n

e

t

h

e

r

a

n

k

o

f

k

i

n

t

h

e

s

e

t

o

f

k

e

y

s

.

Page 8: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

8

E

v

e

r

y

n

o

d

e

v

s

t

o

r

e

s

t

w

o

p

i

e

c

e

s

o

f

i

n

f

o

r

m

a

t

i

o

n

:

I

t

s

k

e

y

T

h

e

n

u

m

b

e

r

o

f

i

t

s

d

e

s

c

e

n

d

a

n

t

s

(

T

h

e

s

i

z

e

o

f

t

h

e

s

u

b

t

r

e

e

w

i

t

h

r

o

o

t

v

)

Augmenting the

Data

Structure

4

17

21

33

48

51

73

92

81

9

1241

1 1

1

1

2

2 4

4 6

11

Page 9: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

9

Find the rank of key x in the tree

w

i

t

h

r

o

o

t

n

o

d

e

v

:

Rank(v, x)

1 if x = key(v)

2 then return 1 + size(left(v))

3 if x < key(v)

4 then return Rank(left(v), x)

5 else return 1 + size(left(v)) +

R

a

n

k

(

r

i

g

h

t

(

v

)

,

x

)

How

To

Determine

The

Rank of an

Element

4

17

21

33

48

51

73

92

81

9

1241

1 1

1

1

2

2 4

4 6

11

Page 10: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

10

How to

Find the k-th

Order

Statistic

Find (a pointer to) the node containing the

k-th smallest key in the subtree rooted

at node v.

Select(v, k)

1

i

f

k

=

s

i

z

e

(

l

e

f

t

(

v

)

)

+

1

2

t

h

e

n

r

e

t

u

r

n

v

3

i

f

k

s

i

z

e

(

l

e

f

t

(

v

)

)

4

t

h

e

n

r

e

t

u

r

n

S

e

l

e

c

t

(

l

e

f

t

(

v

)

,

k

)

5

e

l

s

e

r

e

t

u

r

n

S

e

l

e

c

t

(

r

i

g

h

t

(

v

)

,

k

1

s

i

z

e

(

l

e

f

t

(

v

)

)

)

4

17

21

33

48

51

73

92

81

9

1241

1 1

1

1

2

2 4

4 6

11

Page 11: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

11

Maintaining

Subtree

Sizes

Under Insertions

4

17

21

33

48

51

73

92

81

9

1241

1 1

1

1

2

2 4

4 6

11

I

n

s

e

r

t

o

p

e

r

a

t

i

o

n

I

n

s

e

r

t

n

o

d

e

a

s

i

n

t

o

a

s

t

a

n

d

a

r

d

b

i

n

a

r

y

s

e

a

r

c

h

t

r

e

e

.

A

d

d

1

t

o

t

h

e

s

u

b

t

r

e

e

s

i

z

e

o

f

e

v

e

r

y

a

n

c

e

s

t

o

r

o

f

t

h

e

n

e

w

n

o

d

e

.

Page 12: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

12

Maintaining

Subtree

Sizes

Under Insertions

4

17

21

33

48

51

73

92

81

9

1241

1 1

1

1

2

2 4

4 6

11

641

I

n

s

e

r

t

o

p

e

r

a

t

i

o

n

I

n

s

e

r

t

n

o

d

e

a

s

i

n

t

o

a

s

t

a

n

d

a

r

d

b

i

n

a

r

y

s

e

a

r

c

h

t

r

e

e

A

d

d

1

t

o

t

h

e

s

u

b

t

r

e

e

s

i

z

e

o

f

e

v

e

r

y

a

n

c

e

s

t

o

r

o

f

t

h

e

n

e

w

n

o

d

e

Page 13: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

13

Maintaining

Subtree

Sizes

Under Insertions

4

17

21

33

48

51

73

92

81

9

1241

1 1

1

1

3

2 5

4 7

12

641

I

n

s

e

r

t

o

p

e

r

a

t

i

o

n

I

n

s

e

r

t

n

o

d

e

a

s

i

n

t

o

a

s

t

a

n

d

a

r

d

b

i

n

a

r

y

s

e

a

r

c

h

t

r

e

e

A

d

d

1

t

o

t

h

e

s

u

b

t

r

e

e

s

i

z

e

o

f

e

v

e

r

y

a

n

c

e

s

t

o

r

o

f

t

h

e

n

e

w

n

o

d

e

Page 14: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

14

Maintaining

Subtree

Sizes

Under

Deletions

D

e

l

e

t

e

o

p

e

r

a

t

i

o

n

D

e

l

e

t

e

n

o

d

e

a

s

f

r

o

m

a

s

t

a

n

d

a

r

d

b

i

n

a

r

y

s

e

a

r

c

h

t

r

e

e

S

u

b

t

r

a

c

t

1

f

r

o

m

t

h

e

s

u

b

t

r

e

e

s

i

z

e

o

f

e

v

e

r

y

a

n

c

e

s

t

o

r

o

f

t

h

e

d

e

l

e

t

e

d

n

o

d

e

Page 15: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

15

Maintaining

Subtree

Sizes

Under

Rotations

s1

s2s3

s4 s5

s1

s3s5

s4

s5 + s3 + 1

Page 16: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

16

Theorem: There exists a data structure to represent a dynamically changing set S of numbers with the following properties:

• The data structure can be updated in

O(log n) time after every insertion or

deletion into or from S.

• The data structure allows us to

determine the rank of an element or to

find the element with a given rank in

O(log n) time.

• The data structure occupies O (n)

space.

Dynamic

Order

Statistics—Summary

Page 17: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

17

Examples for Augmenting DS

• Dynamic order statistics: Augmenting binary search trees by size information

• D-dimensional range trees: Recursive construction of (static) d-dim range trees

• Min-augmented dynamic range trees: Augmenting 1-dim range trees by min-

information

• Interval trees

• Priority search trees

Page 18: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

18

4-Sided

Range

Queries

Page 19: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

19

4-Sided

Range

Queries

Goal: Build a static data structure of size O(n log n) that can answer 4-sided range queries in O(log2 n + k) time.

Page 20: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

20

Orthogonal d-dimensional Range Search

Build a static data structure for a set P of n points in d-space that supports d-dim range

queries:

d-dim range query: Let R be a d-dim orthogonal hyperrectangle, given by

d ranges [x1, x1‘], …, [xd, xd‘]:

Find all points p = (p1, …, pd) P such that x1 ≤ p1≤ x1‘,…,xd ≤ pd ≤ xd.

Special cases:

1-dim range query: 2-dim range query:

x1

x1

x1‘

x1‘

x2‘

x2

Page 21: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

21

1-dim Range Search

Standard binary search trees support also 1-dim range queries:

37

18

99

12 23

21

81

74 90

55

42 61

49

68

30 80

Page 22: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

22

1-dim Range Search

Leaf-search-tree:

37

18

99

12 23

21

81

74 90

55

42 61

49

68

30 80

21

1812

49

4237 6861

55

8174 9990

23

Page 23: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

23

1-dim Range Tree

A 1-dim range tree is a leaf-search tree for the x-values (points on the line).

Internal nodes have routers guiding the search to the leaves: We choose the maximal

x-value in left subtree as router.

Range search: In order to find all points in a given range [l, r] search for the boundary

values l and r.

This is a forked path; report all leaves of subtrees rooted at nodes v in between the

two search paths whose parents are on the search path.

Page 24: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

24

The selected subtrees

l r

Split node

Page 25: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

25

Canonical Subsets

The canonical subset of node v, P(v), is the subset of points of P stored at the leaves

of the subtree rooted at v.

If v is a leaf, P(v) is the point stored at this leaf.

If v is the root, P(v) = P.

Observations:

For each query range [l, r] the set of points with x-coordinates falling into this range is

the disjoint union of O(log n) canonical subsets of P.

A node v is called an umbrella node for the range [l, r], if the x-coordinates of all

points in its canonical subset P(v) fall into the range, but this does not hold for the

predecessor of v.

All k points stored at the leaves of a tree rooted at node v, i.e. the k points in a

canonical subset P(v), can be reported in time O(k).

Page 26: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

26

1-dim Range Tree: Summary

Let P be a set of n points in 1-dim space.

P can be stored in a balanced binary leaf-search tree such that the following holds:

Construction time: O(n log n)

Space requirement: O(n)

Insertion of a point: O(log n) time

Deletion of a point: O(log n) time

1-dim-range-query: Reporting all k points falling into a given query range can be

carried out in time O(log n + k).

The performance of 1-dim range trees does not depend on the chosen balancing

scheme!

Page 27: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

27

2-di

m

Range tree:

The

Pri

mary

Structure

• S

t

a

t

i

c

b

i

n

a

r

y

l

e

a

f

-

s

e

a

r

c

h

t

r

e

e

o

v

e

r

x

-

c

o

o

r

d

i

n

a

t

e

s

o

f

p

o

i

n

t

s

.

Page 28: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

28

The

Pri

mary

Structure

• S

t

a

t

i

c

b

i

n

a

r

y

l

e

a

f

-

s

e

a

r

c

h

t

r

e

e

o

v

e

r

x

-

c

o

o

r

d

i

n

a

t

e

s

o

f

p

o

i

n

t

s

.

Page 29: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

29

The

Pri

mary

Structure

• S

t

a

t

i

c

b

i

n

a

r

y

l

e

a

f

-

s

e

a

r

c

h

t

r

e

e

o

v

e

r

x

-

c

o

o

r

d

i

n

a

t

e

s

o

f

p

o

i

n

t

s

.

Page 30: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

30

The

Pri

mary

Structure

• S

t

a

t

i

c

b

i

n

a

r

y

l

e

a

f

-

s

e

a

r

c

h

t

r

e

e

o

v

e

r

x

-

c

o

o

r

d

i

n

a

t

e

s

o

f

p

o

i

n

t

s

.

• E

v

e

r

y

l

e

a

f

r

e

p

r

e

s

e

n

t

s

a

v

e

r

t

i

c

a

l

s

l

a

b

o

f

t

h

e

p

l

a

n

e

.

Page 31: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

31

The

Pri

mary

Structure

• Static binary leaf-search tree over x-coordinates of points.

• Every leaf represents a vertical slab of the plane.

• Every internal node represents a slab that is the union of the slabs of its children.

Page 32: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

32

The

Pri

mary

Structure

• Static binary leaf-search tree over x-coordinates of points.

• Every leaf represents a vertical slab of the plane.

• Every internal node represents a slab that is the union of the slabs of its children.

Page 33: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

33

The

Pri

mary

Structure

• Static binary leaf-search tree over x-coordinates of points.

• Every leaf represents a vertical slab of the plane.

• Every internal node represents a slab that is the union of the slabs of its children.

Page 34: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

34

The

Pri

mary

Structure

• Static binary leaf-search tree over x-coordinates of points.

• Every leaf represents a vertical slab of the plane.

• Every internal node represents a slab that is the union of the slabs of its children.

Page 35: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

35

Answering 2-di

m

Range

Queries

• N

o

r

m

a

l

i

z

e

q

u

e

r

i

e

s

t

o

e

n

d

o

n

s

l

a

b

b

o

u

n

d

a

r

i

e

s

.

• Q

u

e

r

y

d

e

c

o

m

p

o

s

e

s

i

n

t

o

O

(

l

o

g

n

)

s

u

b

q

u

e

r

i

e

s

.

• E

v

e

r

y

s

u

b

q

u

e

r

y

i

s

a

1

-

d

i

m

e

n

s

i

o

n

a

l

r

a

n

g

e

q

u

e

r

y

o

n

y

-

c

o

o

r

d

i

n

a

t

e

s

o

f

a

l

l

p

o

i

n

t

s

i

n

t

h

e

s

l

a

b

o

f

t

h

e

c

o

r

r

e

s

p

o

n

d

i

n

g

n

o

d

e

.

(

x

-

c

o

o

r

d

i

n

a

t

e

s

d

o

n

o

t

m

a

t

t

e

r

!

)

Page 36: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

36

The selected subtrees

l r

Split node

Page 37: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

37

Answering

Queries

• N

o

r

m

a

l

i

z

e

q

u

e

r

i

e

s

t

o

e

n

d

o

n

s

l

a

b

b

o

u

n

d

a

r

i

e

s

.

• Q

u

e

r

y

d

e

c

o

m

p

o

s

e

s

i

n

t

o

O

(

l

o

g

n

)

s

u

b

q

u

e

r

i

e

s

.

• E

v

e

r

y

s

u

b

q

u

e

r

y

i

s

a

1

-

d

i

m

e

n

s

i

o

n

a

l

r

a

n

g

e

q

u

e

r

y

o

n

y

-

c

o

o

r

d

i

n

a

t

e

s

o

f

a

l

l

p

o

i

n

t

s

i

n

t

h

e

s

l

a

b

o

f

t

h

e

c

o

r

r

e

s

p

o

n

d

i

n

g

n

o

d

e

.

(

x

-

c

o

o

r

d

i

n

a

t

e

s

d

o

n

o

t

m

a

t

t

e

r

!

)

Page 38: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

38

Answering

Queries

• N

o

r

m

a

l

i

z

e

q

u

e

r

i

e

s

t

o

e

n

d

o

n

s

l

a

b

b

o

u

n

d

a

r

i

e

s

.

• Q

u

e

r

y

d

e

c

o

m

p

o

s

e

s

i

n

t

o

O

(

l

g

n

)

s

u

b

q

u

e

r

i

e

s

.

• E

v

e

r

y

s

u

b

q

u

e

r

y

i

s

a

1

-

d

i

m

e

n

s

i

o

n

a

l

r

a

n

g

e

q

u

e

r

y

o

n

y

-

c

o

o

r

d

i

n

a

t

e

s

o

f

a

l

l

p

o

i

n

t

s

i

n

t

h

e

s

l

a

b

o

f

t

h

e

c

o

r

r

e

s

p

o

n

d

i

n

g

n

o

d

e

.

(

x

-

c

o

o

r

d

i

n

a

t

e

s

d

o

n

o

t

m

a

t

t

e

r

!

)

Page 39: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

39

Answering

Queries

• N

o

r

m

a

l

i

z

e

q

u

e

r

i

e

s

t

o

e

n

d

o

n

s

l

a

b

b

o

u

n

d

a

r

i

e

s

.

• Q

u

e

r

y

d

e

c

o

m

p

o

s

e

s

i

n

t

o

O

(

l

o

g

n

)

s

u

b

q

u

e

r

i

e

s

.

• E

v

e

r

y

s

u

b

q

u

e

r

y

i

s

a

1

-

d

i

m

e

n

s

i

o

n

a

l

r

a

n

g

e

q

u

e

r

y

o

n

y

-

c

o

o

r

d

i

n

a

t

e

s

o

f

a

l

l

p

o

i

n

t

s

i

n

t

h

e

s

l

a

b

o

f

t

h

e

c

o

r

r

e

s

p

o

n

d

i

n

g

n

o

d

e

.

(

x

-

c

o

o

r

d

i

n

a

t

e

s

d

o

n

o

t

m

a

t

t

e

r

!

)

Page 40: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

40

2-dim Range Tree

v

Tx

Ty(v)

Ix(v)

x

y

Page 41: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

41

2-dim Range Tree

A 2-dimensional range tree for storing a set P of n points in the x-y-plane is:

• A 1-dim-range tree Tx for the x-coordinates of points.

• Each node v of Tx has a pointer to a 1-dim-range-tree Ty(v) storing all points which

fall into the interval Ix(v). That is: Ty(v) is a 1-dim-range-tree based on the y-

coordinates of all points p P with p Ix(v).

Leaf-search-tree on x-coordinates of points

Leaf-search-tree ony-coordinates of poins

v

Page 42: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

42

2-dim Range Tree

A 2-dim range tree on a set of n points in the plane requires O(n log n) space.

p

p

pp

A point p is stored in all associated range trees Ty(v) for all nodes v on the search path to px in Tx.

Hence, for each depth d, each point p occurs in only one associated search structure Ty(v)for a node v of depth d in Tx.

The 2-dim range tree can be constructed in time O(n log n).(Presort the points on y-coordinates!)

Page 43: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

43

The 2-Di

mensional

Range

Tree

• P

r

i

m

a

r

y

s

t

r

u

c

t

u

r

e

:

L

e

a

f

-

s

e

a

r

c

h

t

r

e

e

o

n

x

-

c

o

o

r

d

i

n

a

t

e

s

o

f

p

o

i

n

t

s

• E

v

e

r

y

n

o

d

e

s

t

o

r

e

s

a

s

e

c

o

n

d

a

r

y

s

t

r

u

c

t

u

r

e

:

B

a

l

a

n

c

e

d

b

i

n

a

r

y

s

e

a

r

c

h

t

r

e

e

o

n

y

-

c

o

o

r

d

i

n

a

t

e

s

o

f

p

o

i

n

t

s

i

n

t

h

e

n

o

d

e

s

s

l

a

b

.

Every point is stored in secondary structures of O(log n) nodes.

Space: O(n log n)

Page 44: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

44

Answering

Queries

• E

v

e

r

y

2

-

d

i

m

e

n

s

i

o

n

a

l

r

a

n

g

e

q

u

e

r

y

d

e

c

o

m

p

o

s

e

s

i

n

t

o

O

(

l

o

g

n

)

1

-

d

i

m

e

n

s

i

o

n

a

l

r

a

n

g

e

q

u

e

r

i

e

s

• E

a

c

h

s

u

c

h

q

u

e

r

y

t

a

k

e

s

O

(

l

o

g

n

+

k

′)

t

i

m

e

• T

o

t

a

l

q

u

e

r

y

c

o

m

p

l

e

x

i

t

y

:

O

(

l

o

g2

n

+

k

)

Page 45: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

45

2-dim Range Query

Let P be a set of points in the plane stored in a 2-dim range tree and let a 2-dim range R defined by the two intervals [x, x‘], [y, y‘] be given. The all k points of P falling into the range R can be reported as follows:

1. Determine the O(log n) umbrella nodes for the range [x, x‘], i.e. determine the canonical subsets of P that together contain exactly the points with x-coordinates in the range [x, x‘]. (This is a 1-dim range query on the x-coordinates.)

2. For each umbrella node v obtained in 1, use the associated 1-dim range tree Ty(v)

in order to select the subset P(v) of points with y-coordinates in the range [y, y‘]. (This is a 1-dim range query for each of the O(log n) canonical subsets obtained in 1.)

Time to report all k points in the 2-dim range R: O(log2 n + k).

Query time can be reduced to O(log n +k) by a technique known as fractional cascading.

Page 46: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

46

The 3-Di

mensional

Range

Tree

• Primary structure:

S

e

a

r

c

h

t

r

e

e

o

n

x

-

c

o

o

r

d

i

n

a

t

e

s

o

f

p

o

i

n

t

s

• E

v

e

r

y

n

o

d

e

s

t

o

r

e

s

a

secondary structure:

2

-

d

i

m

e

n

s

i

o

n

a

l

r

a

n

g

e

t

r

e

e

o

n

p

o

i

n

t

s

i

n

t

h

e

n

o

d

e

s

s

l

a

b

.

Every point is stored in secondary structures of O(log n) nodes.

Space: O(n log2 n)

Page 47: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

47

Answering

Queries

• E

v

e

r

y

3

-

d

i

m

e

n

s

i

o

n

a

l

r

a

n

g

e

q

u

e

r

y

d

e

c

o

m

p

o

s

e

s

i

n

t

o

O

(

l

o

g

n

)

2

-

d

i

m

e

n

s

i

o

n

a

l

r

a

n

g

e

q

u

e

r

i

e

s

• E

a

c

h

s

u

c

h

q

u

e

r

y

t

a

k

e

s

O

(

l

o

g2

n

+

k

′)

t

i

m

e

• T

o

t

a

l

q

u

e

r

y

c

o

m

p

l

e

x

i

t

y

:

O(log3

n + k)

Page 48: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

48

d-Di

mensional

Range

Queries

• P

r

i

m

a

r

y

s

t

r

u

c

t

u

r

e

:

S

e

a

r

c

h

t

r

e

e

o

n

x

-

c

o

o

r

d

i

n

a

t

e

s• S

e

c

o

n

d

a

r

y

s

t

r

u

c

t

u

r

e

s

:

(

d

1

)

-

d

i

m

e

n

s

i

o

n

a

l

r

a

n

g

e

t

r

e

e

s• S

p

a

c

e

r

e

q

u

i

r

e

m

e

n

t

:

O

(

n

l

o

gd

 

1

n

)• Q

u

e

r

y

t

i

m

e

:

O

(

n

l

o

gd

 

1

n

)

Page 49: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

49

Updates are difficult!

Insertion or deletion of a point p in a 2-dim range tree requires:

1. Insertion or deletion of p into the primary range tree Tx according to the x-

coordinate of p

2. For each node v on the search path to the leaf storing p in Tx, insertion or deletion

of p in the associated secondary range tree Ty(v).

Maintaining the primary range tree balanced is difficult, except for the case d = 1!

Rotations in the primary tree may require to completely rebuild the associated range

trees along the search path!

Page 50: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

50

Range

Trees–Summary

Theorem: There exists a data structure to represent a static set S of n points in d dimensions with the following properties:

The data structure allows us to answer range

queries in

O(logd n + k) time. The data structure occupies

O(n logd – 1 n) space.

Note: The query complexity can be reduced to O(logd – 1 n + k), for d ≥ 2, using a very beautiful technique called fractional cascading.

Page 51: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

51

Examples for Augmenting DS

• Dynamic order statistics: Augmenting binary search trees by size information

• D-dimensional range trees: Recursive construction of (static) d-dim range trees

• Min-augmented dynamic range trees: Augmenting 1-dim range trees by min-

information

• Interval trees

• Priority search trees

Page 52: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

52

minXinRectangle Queries

Problem: Given a set P of points that changes under insertions and deletions,

construct a data structure to store P that can be updated in O(log n) time and that can

find the point with minimal x-coordinate in a given range below a given

threshold in O(log n) time.

l r

y0

minXinRectangle(l, r, y0)

Assumption: All points have pairwise different x-coordinates

Page 53: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

53

minXinRectangle Queries

lr

y0

minXinRectangle(l, r, y0)

Assumption: All points have pairwise different x-coordinates

Page 54: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

54

Min-augmented

Range

Tree

(2, 12) (3, 4) (4, 11)

(5, 3) (8, 5)

(11, 21)

(14, 7)

(21, 8)(15, 2) (17, 30)

2 4

3

11

8

5

14

17

15 21

2

2

2

3

4 3

3

3

Two data structures in one:

Leaf-search tree on x-coordinates of points

Min-tournament tree on y-coordinates of points

82

Page 55: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

55

minXinRectangle(l, r, y0)

l r

Split node

Search for the boundary values l, r.Find the leftmost umbrella node witha min-field ≤ y0.

Page 56: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

56

minXinRectangle(l, r, y0)

l r

Split node

Search for the boundary values l, r.Find the leftmost umbrella node witha min-field ≤ y0.

Proceed to the left son of the currentnode, if its min-field is ≤ y0, and tothe right son, otherwise. Return the point at the leaf.

minXinRectangle(l, r, y0) can be found in time O(height of tree).

Page 57: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

57

Updates

Insert operation

Insert node as into a standard binary leaf search tree.

Adjust min-fields of every ancestor of the new node by playing a min tournament for

each node and its sibling along the search path.

Delete operation: Similar

Page 58: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

58

Maintaining

min-fields under

Rotations

s1

s2s3

s4 s5

s1

s3s5

s4

min{s5, s3}

Page 59: Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006

59

Min-augmented

Range

Trees–Summary

Theorem: There exists a data structure to represent a dynamic set S of n points in the plane with the following properties:

The data structure allows updates and to answer minXinRectangle(l, r, y 0) queries in

O(log n) time. The data structure occupies O (n) space.

Note: The data structure can be based on an arbitrary scheme of balanced binary leaf search trees.