117
Interval Tree + Priority Search Trees โ„Ž PST: min heap over the -coordinate (of left endpoint) while a BST over the -coordinate PST: max heap over the -coordinate (of right endpoint) while a BST over the -coordinate Query object: vertical segment ร— [ , โ€ฒ ] root Set: horizontal line segments Applicable to axis-parallel segments only. IT ( , ) ( , โ€ฒ )

Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Interval Tree + Priority Search Trees

๐‘ฃ

๐ผ๐‘™๐‘’๐‘“๐‘ก ๐ผ๐‘Ÿ๐‘–๐‘”โ„Ž๐‘ก

๐ผ๐‘š๐‘–๐‘‘ ๐ผ๐‘š๐‘–๐‘‘

PST: min heap over

the ๐‘ฅ-coordinate

(of left endpoint)

while a BST over

the ๐‘ฆ-coordinate

PST: max heap over

the ๐‘ฅ-coordinate

(of right endpoint)

while a BST over

the ๐‘ฆ-coordinate

๐‘ฅ๐‘š๐‘–๐‘‘

Query object: vertical segment ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ , ๐‘ž๐‘ฆโ€ฒ ]

rootSet: ๐‘› horizontal line segments

Applicable to axis-parallel segments only.

IT

๐‘ž

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ)

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )

Page 2: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

General Windowing Queries

Line segments can have arbitrary orientations.

๐‘ค

Page 3: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

General Windowing Queries

Line segments can have arbitrary orientations.

A bounding box approach?

๐‘ค

Page 4: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

General Windowing Queries

Line segments can have arbitrary orientations.

A bounding box approach?

โ€ข Replace each segment with its bounding box.

๐‘ค

Page 5: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

General Windowing Queries

Line segments can have arbitrary orientations.

A bounding box approach?

โ€ข Replace each segment with its bounding box.

โ€ข Find all bounding boxes intersected the query.

โ€ข Check the segments defining these boxes.

๐‘ค

Page 6: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

General Windowing Queries

Line segments can have arbitrary orientations.

A bounding box approach?

โ€ข Replace each segment with its bounding box.

โ€ข Find all bounding boxes intersected the query.

โ€ข Check the segments defining these boxes.

Works well generally in practice.

๐‘ค

Page 7: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

General Windowing Queries

Line segments can have arbitrary orientations.

A bounding box approach?

โ€ข Replace each segment with its bounding box.

โ€ข Find all bounding boxes intersected the query.

โ€ข Check the segments defining these boxes.

Works well generally in practice.

Worst case can be very bad.

No segment intersects

๐‘Š even though all the

bounding boxes do!

๐‘ค

๐‘ค

Page 8: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

General Windowing Queries

Line segments can have arbitrary orientations.

A bounding box approach?

โ€ข Replace each segment with its bounding box.

โ€ข Find all bounding boxes intersected the query.

โ€ข Check the segments defining these boxes.

Works well generally in practice.

Worst case can be very bad.

No segment intersects

๐‘Š even though all the

bounding boxes do!

๐‘ค

๐‘ค

No guarantee on a fast query time.

Page 9: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Thinking Top-Down

Segments with โ‰ฅ 1 endpoints in ๐‘Š.

Page 10: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Thinking Top-Down

Segments with โ‰ฅ 1 endpoints in ๐‘Š.

Range trees.

Page 11: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Thinking Top-Down

Segments with โ‰ฅ 1 endpoints in ๐‘Š.

Range trees.

Segments intersecting the window boundary.

One intersection query with each of the 4 boundary edges.

Page 12: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Thinking Top-Down

Segments with โ‰ฅ 1 endpoints in ๐‘Š.

Range trees.

Segments intersecting the window boundary.

One intersection query with each of the 4 boundary edges.

Focus on a vertical boundary edge.

Page 13: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Thinking Top-Down

Segments with โ‰ฅ 1 endpoints in ๐‘Š.

Range trees.

Segments intersecting the window boundary.

One intersection query with each of the 4 boundary edges.

Focus on a vertical boundary edge.

Query problem

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Page 14: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Thinking Top-Down

Segments with โ‰ฅ 1 endpoints in ๐‘Š.

Range trees.

Segments intersecting the window boundary.

One intersection query with each of the 4 boundary edges.

Focus on a vertical boundary edge.

Query problem

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Page 15: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Thinking Top-Down

Segments with โ‰ฅ 1 endpoints in ๐‘Š.

Range trees.

Segments intersecting the window boundary.

One intersection query with each of the 4 boundary edges.

Focus on a vertical boundary edge.

Query problem

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set: ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of ๐‘› segments

โ€ข arbitrarily oriented

โ€ข non-intersecting in the interior

โ€ข possibly sharing endpoints

Page 16: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

๐‘ 2

๐‘ 1

Inapplicability of an Interval Tree

An interval tree is not very helpful.

โˆ’โˆž,๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ฅ๐‘š๐‘–๐‘‘

๐‘ 3

๐‘ž

Page 17: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

๐‘ 2

๐‘ 1

Inapplicability of an Interval Tree

An interval tree is not very helpful.

Search with ๐‘ž๐‘ฅ

โˆ’โˆž,๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ฅ๐‘š๐‘–๐‘‘

๐‘ 3

๐‘ž

Page 18: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

๐‘ 2

๐‘ 1

Inapplicability of an Interval Tree

An interval tree is not very helpful.

Search with ๐‘ž๐‘ฅ ๐ผ ๐‘š๐‘–๐‘‘ ๐‘ฃ dat node ๐‘ฃat node ๐‘ฃ

โˆ’โˆž,๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ฅ๐‘š๐‘–๐‘‘

๐‘ 3

๐‘ž

Page 19: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

๐‘ 2

๐‘ 1

Inapplicability of an Interval Tree

An interval tree is not very helpful.

Search with ๐‘ž๐‘ฅ ๐ผ ๐‘š๐‘–๐‘‘ ๐‘ฃ dat node ๐‘ฃat node ๐‘ฃ

๐‘ฅ ๐‘š๐‘–๐‘‘ ๐‘ฃ > ๐‘ž ๐‘ฅ๐‘ฅ ๐‘ฅ ๐‘ฅ ๐‘š๐‘–๐‘‘ ๐‘‘ ๐‘š๐‘–๐‘‘ ๐‘ฃ > ๐‘ž๐‘ฅ

Checking if left endpoint โˆˆ (โˆ’โˆž, ๐‘ž๐‘ฅ] ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

(for a horizontal segment).

โˆ’โˆž,๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ฅ๐‘š๐‘–๐‘‘

๐‘ 3

๐‘ž

Page 20: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

๐‘ 2

๐‘ 1

Inapplicability of an Interval Tree

An interval tree is not very helpful.

Search with ๐‘ž๐‘ฅ ๐ผ ๐‘š๐‘–๐‘‘ ๐‘ฃ dat node ๐‘ฃat node ๐‘ฃ

๐‘ฅ ๐‘š๐‘–๐‘‘ ๐‘ฃ > ๐‘ž ๐‘ฅ๐‘ฅ ๐‘ฅ ๐‘ฅ ๐‘š๐‘–๐‘‘ ๐‘‘ ๐‘š๐‘–๐‘‘ ๐‘ฃ > ๐‘ž๐‘ฅ

Checking if left endpoint โˆˆ (โˆ’โˆž, ๐‘ž๐‘ฅ] ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

(for a horizontal segment).

No such reduction to range

query when the segment is

arbitrarily oriented

โˆ’โˆž,๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ฅ๐‘š๐‘–๐‘‘

๐‘ 3

๐‘ž

Page 21: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

๐‘ 2

๐‘ 1

Inapplicability of an Interval Tree

An interval tree is not very helpful.

Search with ๐‘ž๐‘ฅ ๐ผ ๐‘š๐‘–๐‘‘ ๐‘ฃ dat node ๐‘ฃat node ๐‘ฃ

๐‘ฅ ๐‘š๐‘–๐‘‘ ๐‘ฃ > ๐‘ž ๐‘ฅ๐‘ฅ ๐‘ฅ ๐‘ฅ ๐‘š๐‘–๐‘‘ ๐‘‘ ๐‘š๐‘–๐‘‘ ๐‘ฃ > ๐‘ž๐‘ฅ

Checking if left endpoint โˆˆ (โˆ’โˆž, ๐‘ž๐‘ฅ] ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

(for a horizontal segment).

No such reduction to range

query when the segment is

arbitrarily oriented

โˆ’โˆž,๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ฅ๐‘š๐‘–๐‘‘

๐‘ 3

๐‘ž

Page 22: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

๐‘ 2

๐‘ 1

Inapplicability of an Interval Tree

An interval tree is not very helpful.

Search with ๐‘ž๐‘ฅ ๐ผ ๐‘š๐‘–๐‘‘ ๐‘ฃ dat node ๐‘ฃat node ๐‘ฃ

๐‘ฅ ๐‘š๐‘–๐‘‘ ๐‘ฃ > ๐‘ž ๐‘ฅ๐‘ฅ ๐‘ฅ ๐‘ฅ ๐‘š๐‘–๐‘‘ ๐‘‘ ๐‘š๐‘–๐‘‘ ๐‘ฃ > ๐‘ž๐‘ฅ

Checking if left endpoint โˆˆ (โˆ’โˆž, ๐‘ž๐‘ฅ] ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

(for a horizontal segment).

No such reduction to range

query when the segment is

arbitrarily oriented

โˆ’โˆž,๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ฅ๐‘š๐‘–๐‘‘

๐‘ 3

๐‘ž

Segment ๐‘ 1 โˆˆ ๐ผ๐‘š๐‘–๐‘‘(๐‘ฃ) intersects ๐‘žbut its left endpoint โˆ‰ (โˆ’โˆž, ๐‘ž๐‘ฅ] ร— [๐‘ž๐‘ฆ , ๐‘ž๐‘ฆ

โ€ฒ ].

Page 23: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

๐‘ 2

๐‘ 1

Inapplicability of an Interval Tree

An interval tree is not very helpful.

Search with ๐‘ž๐‘ฅ ๐ผ ๐‘š๐‘–๐‘‘ ๐‘ฃ dat node ๐‘ฃat node ๐‘ฃ

๐‘ฅ ๐‘š๐‘–๐‘‘ ๐‘ฃ > ๐‘ž ๐‘ฅ๐‘ฅ ๐‘ฅ ๐‘ฅ ๐‘š๐‘–๐‘‘ ๐‘‘ ๐‘š๐‘–๐‘‘ ๐‘ฃ > ๐‘ž๐‘ฅ

Checking if left endpoint โˆˆ (โˆ’โˆž, ๐‘ž๐‘ฅ] ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

(for a horizontal segment).

No such reduction to range

query when the segment is

arbitrarily oriented

โˆ’โˆž,๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ฅ๐‘š๐‘–๐‘‘

๐‘ 3

Segment ๐‘ 3 โˆˆ ๐ผ๐‘š๐‘–๐‘‘(๐‘ฃ) and

its left endpoint โˆˆ (โˆ’โˆž, ๐‘ž๐‘ฅ] ร— [๐‘ž๐‘ฆ , ๐‘ž๐‘ฆโ€ฒ ].

But it has no intersection with ๐‘ž.

๐‘ž

Segment ๐‘ 1 โˆˆ ๐ผ๐‘š๐‘–๐‘‘(๐‘ฃ) intersects ๐‘žbut its left endpoint โˆ‰ (โˆ’โˆž, ๐‘ž๐‘ฅ] ร— [๐‘ž๐‘ฆ , ๐‘ž๐‘ฆ

โ€ฒ ].

Page 24: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Locus Approach

Window ๐‘Š: ๐‘ž๐‘ฅ, ๐‘ž๐‘ฅโ€ฒ ร— [๐‘ž๐‘ฆ , ๐‘ž๐‘ฆ

โ€ฒ ] defined by four parameters.

Idea: Partition the parameter space into regions such that queries in

the region have the same answer.

Page 25: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Locus Approach

Window ๐‘Š: ๐‘ž๐‘ฅ, ๐‘ž๐‘ฅโ€ฒ ร— [๐‘ž๐‘ฆ , ๐‘ž๐‘ฆ

โ€ฒ ] defined by four parameters.

Idea: Partition the parameter space into regions such that queries in

the region have the same answer.

Input: interval set: ๐ผ = { ๐‘ฅ1, ๐‘ฅ1โ€ฒ , ๐‘ฅ2, ๐‘ฅ2

โ€ฒ , โ€ฆ , ๐‘ฅ๐‘›, ๐‘ฅ๐‘›โ€ฒ }

1D Query

point ๐‘ž๐‘ฅ

Output: all intervals containing ๐‘ž๐‘ฅ

Page 26: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Locus Approach

Window ๐‘Š: ๐‘ž๐‘ฅ, ๐‘ž๐‘ฅโ€ฒ ร— [๐‘ž๐‘ฆ , ๐‘ž๐‘ฆ

โ€ฒ ] defined by four parameters.

Idea: Partition the parameter space into regions such that queries in

the region have the same answer.

Input: interval set: ๐ผ = { ๐‘ฅ1, ๐‘ฅ1โ€ฒ , ๐‘ฅ2, ๐‘ฅ2

โ€ฒ , โ€ฆ , ๐‘ฅ๐‘›, ๐‘ฅ๐‘›โ€ฒ }

1D Query

point ๐‘ž๐‘ฅ

Output: all intervals containing ๐‘ž๐‘ฅ

๐‘1, ๐‘2, โ€ฆ , ๐‘๐‘š: distinct interval endpoints in the increasing order.

๐‘๐‘šโˆ’1๐‘1 ๐‘2 ๐‘๐‘š

Page 27: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Locus Approach

Window ๐‘Š: ๐‘ž๐‘ฅ, ๐‘ž๐‘ฅโ€ฒ ร— [๐‘ž๐‘ฆ , ๐‘ž๐‘ฆ

โ€ฒ ] defined by four parameters.

Idea: Partition the parameter space into regions such that queries in

the region have the same answer.

Input: interval set: ๐ผ = { ๐‘ฅ1, ๐‘ฅ1โ€ฒ , ๐‘ฅ2, ๐‘ฅ2

โ€ฒ , โ€ฆ , ๐‘ฅ๐‘›, ๐‘ฅ๐‘›โ€ฒ }

1D Query

point ๐‘ž๐‘ฅ

Output: all intervals containing ๐‘ž๐‘ฅ

๐‘1, ๐‘2, โ€ฆ , ๐‘๐‘š: distinct interval endpoints in the increasing order.

๐‘๐‘šโˆ’1๐‘1 ๐‘2 ๐‘๐‘š

The parameter space โˆ’โˆž,โˆž is partitioned into elementary intervals.

โˆ’โˆž, ๐‘1 , ๐‘1, ๐‘1 , ๐‘1, ๐‘2 , ๐‘2, ๐‘2 ,โ€ฆ , ๐‘๐‘šโˆ’1, ๐‘๐‘š , ๐‘๐‘š, ๐‘๐‘š , ๐‘๐‘š, โˆž

Page 28: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Locus Approach

Window ๐‘Š: ๐‘ž๐‘ฅ, ๐‘ž๐‘ฅโ€ฒ ร— [๐‘ž๐‘ฆ , ๐‘ž๐‘ฆ

โ€ฒ ] defined by four parameters.

Idea: Partition the parameter space into regions such that queries in

the region have the same answer.

Input: interval set: ๐ผ = { ๐‘ฅ1, ๐‘ฅ1โ€ฒ , ๐‘ฅ2, ๐‘ฅ2

โ€ฒ , โ€ฆ , ๐‘ฅ๐‘›, ๐‘ฅ๐‘›โ€ฒ }

1D Query

point ๐‘ž๐‘ฅ

Output: all intervals containing ๐‘ž๐‘ฅ

๐‘1, ๐‘2, โ€ฆ , ๐‘๐‘š: distinct interval endpoints in the increasing order.

๐‘๐‘šโˆ’1๐‘1 ๐‘2 ๐‘๐‘š

The parameter space โˆ’โˆž,โˆž is partitioned into elementary intervals.

โˆ’โˆž, ๐‘1 , ๐‘1, ๐‘1 , ๐‘1, ๐‘2 , ๐‘2, ๐‘2 ,โ€ฆ , ๐‘๐‘šโˆ’1, ๐‘๐‘š , ๐‘๐‘š, ๐‘๐‘š , ๐‘๐‘š, โˆž

Every open interval has two consecutive endpoints.

Every closed interval consists of a single endpoint.

Open intervals alternate with closed intervals.

Page 29: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Using a Binary Search Tree?

๐œ‡

โˆ’โˆž, ๐‘1 ๐‘1, ๐‘1 โ€ฆ ๐‘๐‘– , ๐‘๐‘–+1 โ€ฆ ๐‘๐‘š, ๐‘๐‘š ๐‘๐‘š, โˆž

Int ๐œ‡ : elementary interval

corresponding to ๐œ‡.

๐œ‡ : a leaf

Page 30: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Using a Binary Search Tree?

Store at the leaf ๐œ‡ all the intervals

in ๐ผ that contain Int ๐œ‡ .

๐œ‡

โˆ’โˆž, ๐‘1 ๐‘1, ๐‘1 โ€ฆ ๐‘๐‘– , ๐‘๐‘–+1 โ€ฆ ๐‘๐‘š, ๐‘๐‘š ๐‘๐‘š, โˆž

Int ๐œ‡ : elementary interval

corresponding to ๐œ‡.

๐œ‡ : a leaf

Page 31: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Using a Binary Search Tree?

Store at the leaf ๐œ‡ all the intervals

in ๐ผ that contain Int ๐œ‡ .

๐œ‡

โˆ’โˆž, ๐‘1 ๐‘1, ๐‘1 โ€ฆ ๐‘๐‘– , ๐‘๐‘–+1 โ€ฆ ๐‘๐‘š, ๐‘๐‘š ๐‘๐‘š, โˆž

Int ๐œ‡ : elementary interval

corresponding to ๐œ‡.

๐œ‡ : a leaf

Query time

๐‘‚(log ๐‘› + ๐‘˜)

Page 32: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Using a Binary Search Tree?

Store at the leaf ๐œ‡ all the intervals

in ๐ผ that contain Int ๐œ‡ .

๐œ‡

โˆ’โˆž, ๐‘1 ๐‘1, ๐‘1 โ€ฆ ๐‘๐‘– , ๐‘๐‘–+1 โ€ฆ ๐‘๐‘š, ๐‘๐‘š ๐‘๐‘š, โˆž

Int ๐œ‡ : elementary interval

corresponding to ๐œ‡.

๐œ‡ : a leaf

Query time

๐‘‚(log ๐‘› + ๐‘˜)

BST search #reported

intervals

Page 33: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Using a Binary Search Tree?

Store at the leaf ๐œ‡ all the intervals

in ๐ผ that contain Int ๐œ‡ .

๐œ‡

โˆ’โˆž, ๐‘1 ๐‘1, ๐‘1 โ€ฆ ๐‘๐‘– , ๐‘๐‘–+1 โ€ฆ ๐‘๐‘š, ๐‘๐‘š ๐‘๐‘š, โˆž

Int ๐œ‡ : elementary interval

corresponding to ๐œ‡.

๐œ‡ : a leaf

Query time

๐‘‚(log ๐‘› + ๐‘˜)

BST search #reported

intervals

High storage if the intervals overlap

a lot.

Page 34: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Using a Binary Search Tree?

Store at the leaf ๐œ‡ all the intervals

in ๐ผ that contain Int ๐œ‡ .

๐œ‡

โˆ’โˆž, ๐‘1 ๐‘1, ๐‘1 โ€ฆ ๐‘๐‘– , ๐‘๐‘–+1 โ€ฆ ๐‘๐‘š, ๐‘๐‘š ๐‘๐‘š, โˆž

Int ๐œ‡ : elementary interval

corresponding to ๐œ‡.

๐œ‡ : a leaf

Query time

๐‘‚(log ๐‘› + ๐‘˜)

BST search #reported

intervals

High storage if the intervals overlap

a lot.

๐‘‚ ๐‘›2 possible!

Page 35: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Store an Interval As High as Possible

๐œ‡2๐œ‡1 ๐œ‡3 ๐œ‡4 ๐œ‡5

๐‘ฃ

๐‘ 

Interval ๐‘  is stored five times at

๐œ‡1, ๐œ‡2, ๐œ‡3 , ๐œ‡4, ๐œ‡5.

Page 36: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Store an Interval As High as Possible

๐œ‡2๐œ‡1 ๐œ‡3 ๐œ‡4 ๐œ‡5

๐‘ฃ

๐‘ 

Interval ๐‘  is stored five times at

๐œ‡1, ๐œ‡2, ๐œ‡3 , ๐œ‡4, ๐œ‡5.

Page 37: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Store an Interval As High as Possible

๐œ‡2๐œ‡1 ๐œ‡3 ๐œ‡4 ๐œ‡5

๐‘ฃ

๐‘ 

Interval ๐‘  is stored five times at

๐œ‡1, ๐œ‡2, ๐œ‡3 , ๐œ‡4, ๐œ‡5.

Observation A search path ends at

๐œ‡1, ๐œ‡2, ๐œ‡3 , ๐œ‡4 if and only if

it passes through ๐‘ฃ.

Page 38: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Store an Interval As High as Possible

๐œ‡2๐œ‡1 ๐œ‡3 ๐œ‡4 ๐œ‡5

๐‘ฃ

๐‘ 

Interval ๐‘  is stored five times at

๐œ‡1, ๐œ‡2, ๐œ‡3 , ๐œ‡4, ๐œ‡5.

Why not store ๐‘  only two times

at ๐‘ฃ and ๐œ‡5?

Observation A search path ends at

๐œ‡1, ๐œ‡2, ๐œ‡3 , ๐œ‡4 if and only if

it passes through ๐‘ฃ.

Page 39: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Store an Interval As High as Possible

๐œ‡2๐œ‡1 ๐œ‡3 ๐œ‡4 ๐œ‡5

๐‘ฃ

๐‘ 

Interval ๐‘  is stored five times at

๐œ‡1, ๐œ‡2, ๐œ‡3 , ๐œ‡4, ๐œ‡5.

Why not store ๐‘  only two times

at ๐‘ฃ and ๐œ‡5?

Observation A search path ends at

๐œ‡1, ๐œ‡2, ๐œ‡3 , ๐œ‡4 if and only if

it passes through ๐‘ฃ.

Idea: Store a segment ๐‘  at the fewest nodes whose corresponding intervals

form a partitioning of ๐‘ .

Page 40: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Store an Interval As High as Possible

๐œ‡2๐œ‡1 ๐œ‡3 ๐œ‡4 ๐œ‡5

๐‘ฃ

๐‘ 

Interval ๐‘  is stored five times at

๐œ‡1, ๐œ‡2, ๐œ‡3 , ๐œ‡4, ๐œ‡5.

Why not store ๐‘  only two times

at ๐‘ฃ and ๐œ‡5?

Observation A search path ends at

๐œ‡1, ๐œ‡2, ๐œ‡3 , ๐œ‡4 if and only if

it passes through ๐‘ฃ.

Idea: Store a segment ๐‘  at the fewest nodes whose corresponding intervals

form a partitioning of ๐‘ .

These nodes must be as high as possible in the tree.

Page 41: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree

(โˆ’โˆž,โˆž)

(โˆ’โˆž, ๐‘4]

[๐‘1, ๐‘1] (๐‘2, ๐‘3)(โˆ’โˆž, ๐‘1) [๐‘2, ๐‘2] [๐‘3, ๐‘3] [๐‘4, ๐‘4] [๐‘5, ๐‘5] [๐‘6, ๐‘6] [๐‘7, ๐‘7](๐‘3, ๐‘4) (๐‘4, ๐‘5) (๐‘5, ๐‘6) (๐‘7, โˆž)(๐‘6, ๐‘7)(๐‘1, ๐‘2)

(โˆ’โˆž, ๐‘1] (๐‘1, ๐‘2] (๐‘2, ๐‘3] (๐‘3, ๐‘4] (๐‘4, ๐‘5] (๐‘5, ๐‘6] (๐‘6, ๐‘7]

(โˆ’โˆž, ๐‘2] (๐‘2, ๐‘4] (๐‘4, ๐‘6] (๐‘6, โˆž)

(๐‘4, โˆž)

๐‘1 ๐‘2 ๐‘7๐‘6๐‘5๐‘4๐‘3๐‘ 1 ๐‘ 3

๐‘ 2 ๐‘ 4

๐‘ 5

Page 42: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree

(โˆ’โˆž,โˆž)

(โˆ’โˆž, ๐‘4]

[๐‘1, ๐‘1] (๐‘2, ๐‘3)(โˆ’โˆž, ๐‘1) [๐‘2, ๐‘2] [๐‘3, ๐‘3] [๐‘4, ๐‘4] [๐‘5, ๐‘5] [๐‘6, ๐‘6] [๐‘7, ๐‘7](๐‘3, ๐‘4) (๐‘4, ๐‘5) (๐‘5, ๐‘6) (๐‘7, โˆž)(๐‘6, ๐‘7)(๐‘1, ๐‘2)

(โˆ’โˆž, ๐‘1] (๐‘1, ๐‘2] (๐‘2, ๐‘3] (๐‘3, ๐‘4] (๐‘4, ๐‘5] (๐‘5, ๐‘6] (๐‘6, ๐‘7]

(โˆ’โˆž, ๐‘2] (๐‘2, ๐‘4] (๐‘4, ๐‘6] (๐‘6, โˆž)

(๐‘4, โˆž)

๐‘1 ๐‘2 ๐‘7๐‘6๐‘5๐‘4๐‘3๐‘ 1 ๐‘ 3

๐‘ 2 ๐‘ 4

๐‘ 5

{๐‘ 1} {๐‘ 1}{๐‘ 3} {๐‘ 3, ๐‘ 4}

{๐‘ 2, ๐‘ 5} {๐‘ 5}

Page 43: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Tree Structure

Leaves โ†” elementary intervals (left-to-right)

Page 44: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Tree Structure

Leaves โ†” elementary intervals (left-to-right)

๐‘ฃ

Page 45: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Tree Structure

Leaves โ†” elementary intervals (left-to-right)

Internal node ๐‘ฃ โ†” union Int(๐‘ฃ) of elementary intervals at the leaves in the

subtree ๐’ฏ(๐‘ฃ) rooted at ๐‘ฃ.

๐‘ฃ

Page 46: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Tree Structure

Leaves โ†” elementary intervals (left-to-right)

Internal node ๐‘ฃ โ†” union Int(๐‘ฃ) of elementary intervals at the leaves in the

subtree ๐’ฏ(๐‘ฃ) rooted at ๐‘ฃ.

๐‘ฃ

At ๐‘ฃ stores Int(๐‘ฃ) and the canonical subset (as a linked list) defined below:

Page 47: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Tree Structure

Leaves โ†” elementary intervals (left-to-right)

Internal node ๐‘ฃ โ†” union Int(๐‘ฃ) of elementary intervals at the leaves in the

subtree ๐’ฏ(๐‘ฃ) rooted at ๐‘ฃ.

๐‘ฃ

At ๐‘ฃ stores Int(๐‘ฃ) and the canonical subset (as a linked list) defined below:

๐ถ ๐‘ฃ = ๐‘ฅ, ๐‘ฅโ€ฒ โˆˆ ๐ผ Int ๐‘ฃ โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]and Int(parent ๐‘ฃ) โŠˆ [๐‘ฅ, ๐‘ฅโ€ฒ]}

Page 48: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Tree Structure

Leaves โ†” elementary intervals (left-to-right)

Internal node ๐‘ฃ โ†” union Int(๐‘ฃ) of elementary intervals at the leaves in the

subtree ๐’ฏ(๐‘ฃ) rooted at ๐‘ฃ.

๐‘ฃ

At ๐‘ฃ stores Int(๐‘ฃ) and the canonical subset (as a linked list) defined below:

๐ถ ๐‘ฃ = ๐‘ฅ, ๐‘ฅโ€ฒ โˆˆ ๐ผ Int ๐‘ฃ โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]and Int(parent ๐‘ฃ) โŠˆ [๐‘ฅ, ๐‘ฅโ€ฒ]}

Store intervals at nodes

as high as possible.

Page 49: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

A Closer Look at Storage

๐ถ ๐‘ฃ = ๐‘ฅ, ๐‘ฅโ€ฒ โˆˆ ๐ผ Int ๐‘ฃ โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ] and Int(parent(๐‘ฃ)) โŠˆ [๐‘ฅ, ๐‘ฅโ€ฒ]}

๐‘ข

๐‘ฃ

Int ๐‘ข โŠˆ [๐‘ฅ, ๐‘ฅโ€ฒ]

Int ๐‘ฃ โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]๐ถ(๐‘ฃ):(๐‘ฅ, ๐‘ฅโ€ฒ)

Linked list

Page 50: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Number of Leaves

๐‘› = ๐ผ : #segments

๐‘๐‘– , ๐‘๐‘–+1 ๐‘šโˆ’ 1

[๐‘๐‘– , ๐‘๐‘–] ๐‘š

(โˆ’โˆž, ๐‘1) 1

(๐‘๐‘š, โˆž) 1

๐‘š โ‰ค 2๐‘›: #distinct endpoints

Page 51: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Number of Leaves

โ€ข 2๐‘š + 1 โ‰ค 4๐‘› + 1 leaves

๐‘› = ๐ผ : #segments

๐‘๐‘– , ๐‘๐‘–+1 ๐‘šโˆ’ 1

[๐‘๐‘– , ๐‘๐‘–] ๐‘š

(โˆ’โˆž, ๐‘1) 1

(๐‘๐‘š, โˆž) 1

๐‘š โ‰ค 2๐‘›: #distinct endpoints

Page 52: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of an Interval

Claim Each interval [๐‘ฅ, ๐‘ฅโ€ฒ] โˆˆ ๐ผ is stored at โ‰ค 2 nodes at any depth.

Page 53: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of an Interval

Claim Each interval [๐‘ฅ, ๐‘ฅโ€ฒ] โˆˆ ๐ผ is stored at โ‰ค 2 nodes at any depth.

Proof Suppose the interval is stored at the nodes ๐‘ฃ1, ๐‘ฃ2, โ€ฆ , ๐‘ฃ๐‘˜, ๐‘˜ โ‰ฅ 3, at

the same depth in the left to right order.

๐‘ฃ1 ๐‘ฃ2 ๐‘ฃ3

parent(๐‘ฃ2)

Page 54: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of an Interval

Claim Each interval [๐‘ฅ, ๐‘ฅโ€ฒ] โˆˆ ๐ผ is stored at โ‰ค 2 nodes at any depth.

Proof Suppose the interval is stored at the nodes ๐‘ฃ1, ๐‘ฃ2, โ€ฆ , ๐‘ฃ๐‘˜, ๐‘˜ โ‰ฅ 3, at

the same depth in the left to right order.

Then ๐‘ฃ2 must be a sibling of either ๐‘ฃ1 or ๐‘ฃ3.

๐‘ฃ1 ๐‘ฃ2 ๐‘ฃ3

parent(๐‘ฃ2)

Page 55: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of an Interval

Claim Each interval [๐‘ฅ, ๐‘ฅโ€ฒ] โˆˆ ๐ผ is stored at โ‰ค 2 nodes at any depth.

Proof Suppose the interval is stored at the nodes ๐‘ฃ1, ๐‘ฃ2, โ€ฆ , ๐‘ฃ๐‘˜, ๐‘˜ โ‰ฅ 3, at

the same depth in the left to right order.

Then ๐‘ฃ2 must be a sibling of either ๐‘ฃ1 or ๐‘ฃ3.

๐‘ฃ1 ๐‘ฃ2 ๐‘ฃ3

parent(๐‘ฃ2)Suppose its sibling is ๐‘ฃ1 without loss of generality.

Page 56: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of an Interval

Claim Each interval [๐‘ฅ, ๐‘ฅโ€ฒ] โˆˆ ๐ผ is stored at โ‰ค 2 nodes at any depth.

Proof Suppose the interval is stored at the nodes ๐‘ฃ1, ๐‘ฃ2, โ€ฆ , ๐‘ฃ๐‘˜, ๐‘˜ โ‰ฅ 3, at

the same depth in the left to right order.

Then ๐‘ฃ2 must be a sibling of either ๐‘ฃ1 or ๐‘ฃ3.

๐‘ฃ1 ๐‘ฃ2 ๐‘ฃ3

parent(๐‘ฃ2)Suppose its sibling is ๐‘ฃ1 without loss of generality.

Int ๐‘ฃ1 โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]

Int ๐‘ฃ2 โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]

Page 57: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of an Interval

Claim Each interval [๐‘ฅ, ๐‘ฅโ€ฒ] โˆˆ ๐ผ is stored at โ‰ค 2 nodes at any depth.

Proof Suppose the interval is stored at the nodes ๐‘ฃ1, ๐‘ฃ2, โ€ฆ , ๐‘ฃ๐‘˜, ๐‘˜ โ‰ฅ 3, at

the same depth in the left to right order.

Then ๐‘ฃ2 must be a sibling of either ๐‘ฃ1 or ๐‘ฃ3.

๐‘ฃ1 ๐‘ฃ2 ๐‘ฃ3

parent(๐‘ฃ2)Suppose its sibling is ๐‘ฃ1 without loss of generality.

Int ๐‘ฃ1 โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]

Int ๐‘ฃ2 โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]

Int(parent(๐‘ฃ2)) = Int(๐‘ฃ1) โˆช Int(๐‘ฃ2) โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]

Page 58: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of an Interval

Claim Each interval [๐‘ฅ, ๐‘ฅโ€ฒ] โˆˆ ๐ผ is stored at โ‰ค 2 nodes at any depth.

Proof Suppose the interval is stored at the nodes ๐‘ฃ1, ๐‘ฃ2, โ€ฆ , ๐‘ฃ๐‘˜, ๐‘˜ โ‰ฅ 3, at

the same depth in the left to right order.

Then ๐‘ฃ2 must be a sibling of either ๐‘ฃ1 or ๐‘ฃ3.

๐‘ฃ1 ๐‘ฃ2 ๐‘ฃ3

parent(๐‘ฃ2)Suppose its sibling is ๐‘ฃ1 without loss of generality.

Int ๐‘ฃ1 โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]

Int ๐‘ฃ2 โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]

Int(parent(๐‘ฃ2)) = Int(๐‘ฃ1) โˆช Int(๐‘ฃ2) โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]

[๐‘ฅ, ๐‘ฅโ€ฒ] should be stored at parent(๐‘ฃ2) or above instead of at ๐‘ฃ2. Contradiction.

Page 59: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of an Interval

Claim Each interval [๐‘ฅ, ๐‘ฅโ€ฒ] โˆˆ ๐ผ is stored at โ‰ค 2 nodes at any depth.

Proof Suppose the interval is stored at the nodes ๐‘ฃ1, ๐‘ฃ2, โ€ฆ , ๐‘ฃ๐‘˜, ๐‘˜ โ‰ฅ 3, at

the same depth in the left to right order.

Then ๐‘ฃ2 must be a sibling of either ๐‘ฃ1 or ๐‘ฃ3.

๐‘ฃ1 ๐‘ฃ2 ๐‘ฃ3

parent(๐‘ฃ2)Suppose its sibling is ๐‘ฃ1 without loss of generality.

Int ๐‘ฃ1 โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]

Int ๐‘ฃ2 โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]

Int(parent(๐‘ฃ2)) = Int(๐‘ฃ1) โˆช Int(๐‘ฃ2) โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ]

[๐‘ฅ, ๐‘ฅโ€ฒ] should be stored at parent(๐‘ฃ2) or above instead of at ๐‘ฃ2. Contradiction.

Page 60: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Total Storage

Following the claim, any interval is stored at most twice at any

given length.

Page 61: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Total Storage

Following the claim, any interval is stored at most twice at any

given length.

The required storage at each depth is ๐‘‚(๐‘›).

Page 62: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Total Storage

Following the claim, any interval is stored at most twice at any

given length.

The required storage at each depth is ๐‘‚(๐‘›).

Maximum tree depth (i.e., height) is ๐‘‚(log ๐‘›).

Page 63: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Total Storage

Following the claim, any interval is stored at most twice at any

given length.

The required storage at each depth is ๐‘‚(๐‘›).

Maximum tree depth (i.e., height) is ๐‘‚(log ๐‘›).

๐‘‚(๐‘› log ๐‘›)

Page 64: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

QuerySegmentTree(๐‘ฃ, ๐‘ž๐‘ฅ)

1. report all the intervals in ๐ถ(๐‘ฃ) // canonical subset2. if ๐‘ฃ is not a leaf3. then if ๐‘ž๐‘ฅ โˆˆ Int(๐‘™๐‘(๐‘ฃ))4. then QuerySegmentTree(๐‘™๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)5. else QuerySegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)

root

Page 65: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

QuerySegmentTree(๐‘ฃ, ๐‘ž๐‘ฅ)

1. report all the intervals in ๐ถ(๐‘ฃ) // canonical subset2. if ๐‘ฃ is not a leaf3. then if ๐‘ž๐‘ฅ โˆˆ Int(๐‘™๐‘(๐‘ฃ))4. then QuerySegmentTree(๐‘™๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)5. else QuerySegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)

๐‘ž๐‘ฅ

Example: ๐‘ž๐‘ฅ

root

Page 66: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

QuerySegmentTree(๐‘ฃ, ๐‘ž๐‘ฅ)

1. report all the intervals in ๐ถ(๐‘ฃ) // canonical subset2. if ๐‘ฃ is not a leaf3. then if ๐‘ž๐‘ฅ โˆˆ Int(๐‘™๐‘(๐‘ฃ))4. then QuerySegmentTree(๐‘™๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)5. else QuerySegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)

๐‘ž๐‘ฅ

Example: ๐‘ž๐‘ฅQuerySegmentTree(root, ๐‘ž๐‘ฅ)

root

Page 67: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

QuerySegmentTree(๐‘ฃ, ๐‘ž๐‘ฅ)

1. report all the intervals in ๐ถ(๐‘ฃ) // canonical subset2. if ๐‘ฃ is not a leaf3. then if ๐‘ž๐‘ฅ โˆˆ Int(๐‘™๐‘(๐‘ฃ))4. then QuerySegmentTree(๐‘™๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)5. else QuerySegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)

๐‘ž๐‘ฅ

Example: ๐‘ž๐‘ฅQuerySegmentTree(root, ๐‘ž๐‘ฅ)

root

Page 68: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

QuerySegmentTree(๐‘ฃ, ๐‘ž๐‘ฅ)

1. report all the intervals in ๐ถ(๐‘ฃ) // canonical subset2. if ๐‘ฃ is not a leaf3. then if ๐‘ž๐‘ฅ โˆˆ Int(๐‘™๐‘(๐‘ฃ))4. then QuerySegmentTree(๐‘™๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)5. else QuerySegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)

๐‘ž๐‘ฅ

Example: ๐‘ž๐‘ฅ

Output in order:

๐‘ 2, ๐‘ 5, ๐‘ 1

QuerySegmentTree(root, ๐‘ž๐‘ฅ)

root

Page 69: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

QuerySegmentTree(๐‘ฃ, ๐‘ž๐‘ฅ)

1. report all the intervals in ๐ถ(๐‘ฃ) // canonical subset2. if ๐‘ฃ is not a leaf3. then if ๐‘ž๐‘ฅ โˆˆ Int(๐‘™๐‘(๐‘ฃ))4. then QuerySegmentTree(๐‘™๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)5. else QuerySegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)

๐‘ž๐‘ฅ

Example: ๐‘ž๐‘ฅ

Output in order:

๐‘ 2, ๐‘ 5, ๐‘ 1

Query time:

๐‘‚(log ๐‘› + ๐‘˜)

QuerySegmentTree(root, ๐‘ž๐‘ฅ)

root

Page 70: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

QuerySegmentTree(๐‘ฃ, ๐‘ž๐‘ฅ)

1. report all the intervals in ๐ถ(๐‘ฃ) // canonical subset2. if ๐‘ฃ is not a leaf3. then if ๐‘ž๐‘ฅ โˆˆ Int(๐‘™๐‘(๐‘ฃ))4. then QuerySegmentTree(๐‘™๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)5. else QuerySegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , ๐‘ž๐‘ฅ)

๐‘ž๐‘ฅ

Example: ๐‘ž๐‘ฅ

Output in order:

๐‘ 2, ๐‘ 5, ๐‘ 1

Query time:

๐‘‚(log ๐‘› + ๐‘˜)

#reported intervals

QuerySegmentTree(root, ๐‘ž๐‘ฅ)

root

Page 71: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Construction

Sort endpoints from ๐ผ to yield elementary intervals.

Page 72: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Construction

Sort endpoints from ๐ผ to yield elementary intervals.

๐‘‚(๐‘› log ๐‘›)

Page 73: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Construction

Sort endpoints from ๐ผ to yield elementary intervals.

Construct a balanced BST in a bottom-up way.

๐‘‚(๐‘› log ๐‘›)

Page 74: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Construction

Sort endpoints from ๐ผ to yield elementary intervals.

Construct a balanced BST in a bottom-up way.

Determine interval ๐ผ(๐‘ฃ) for each node ๐‘ฃ.

๐‘‚(๐‘› log ๐‘›)

Page 75: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Construction

Sort endpoints from ๐ผ to yield elementary intervals.

Construct a balanced BST in a bottom-up way.

Determine interval ๐ผ(๐‘ฃ) for each node ๐‘ฃ.

๐‘‚(๐‘› log ๐‘›)

๐‘‚(๐‘›) for all the nodes together

Page 76: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Construction

Sort endpoints from ๐ผ to yield elementary intervals.

Construct a balanced BST in a bottom-up way.

Determine interval ๐ผ(๐‘ฃ) for each node ๐‘ฃ.

Compute canonical subsets by inserting original intervals [๐‘ฅ, ๐‘ฅโ€ฒ]from ๐ผ one by one.

๐‘‚(๐‘› log ๐‘›)

๐‘‚(๐‘›) for all the nodes together

Page 77: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Insertion

InsertSegmentTree(๐‘ฃ, [๐‘ฅ, ๐‘ฅโ€ฒ])

1. if Int(๐‘ฃ)โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ] // Int(parent(๐‘ฃ))โŠˆ [๐‘ฅ, ๐‘ฅโ€ฒ] holds2. then store [๐‘ฅ, ๐‘ฅโ€ฒ] at ๐‘ฃ3. else if Int(๐‘™๐‘ ๐‘ฃ )โˆฉ [๐‘ฅ, ๐‘ฅโ€ฒ] โ‰  โˆ…4. then InsertSegmentTree(๐‘™๐‘ ๐‘ฃ , [๐‘ฅ, ๐‘ฅโ€ฒ])

5. if Int(๐‘Ÿ๐‘ ๐‘ฃ )โˆฉ [๐‘ฅ, ๐‘ฅโ€ฒ] โ‰  โˆ…6. then InsertSegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , [๐‘ฅ, ๐‘ฅโ€ฒ])

Page 78: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Insertion

InsertSegmentTree(๐‘ฃ, [๐‘ฅ, ๐‘ฅโ€ฒ])

1. if Int(๐‘ฃ)โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ] // Int(parent(๐‘ฃ))โŠˆ [๐‘ฅ, ๐‘ฅโ€ฒ] holds2. then store [๐‘ฅ, ๐‘ฅโ€ฒ] at ๐‘ฃ3. else if Int(๐‘™๐‘ ๐‘ฃ )โˆฉ [๐‘ฅ, ๐‘ฅโ€ฒ] โ‰  โˆ…4. then InsertSegmentTree(๐‘™๐‘ ๐‘ฃ , [๐‘ฅ, ๐‘ฅโ€ฒ])

5. if Int(๐‘Ÿ๐‘ ๐‘ฃ )โˆฉ [๐‘ฅ, ๐‘ฅโ€ฒ] โ‰  โˆ…6. then InsertSegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , [๐‘ฅ, ๐‘ฅโ€ฒ])

โ€ข At each visited node ๐‘ฃ, either [๐‘ฅ, ๐‘ฅโ€ฒ] is stored or Int(๐‘ฃ) contains an endpoint

of [๐‘ฅ, ๐‘ฅโ€ฒ].

Page 79: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Insertion

InsertSegmentTree(๐‘ฃ, [๐‘ฅ, ๐‘ฅโ€ฒ])

1. if Int(๐‘ฃ)โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ] // Int(parent(๐‘ฃ))โŠˆ [๐‘ฅ, ๐‘ฅโ€ฒ] holds2. then store [๐‘ฅ, ๐‘ฅโ€ฒ] at ๐‘ฃ3. else if Int(๐‘™๐‘ ๐‘ฃ )โˆฉ [๐‘ฅ, ๐‘ฅโ€ฒ] โ‰  โˆ…4. then InsertSegmentTree(๐‘™๐‘ ๐‘ฃ , [๐‘ฅ, ๐‘ฅโ€ฒ])

5. if Int(๐‘Ÿ๐‘ ๐‘ฃ )โˆฉ [๐‘ฅ, ๐‘ฅโ€ฒ] โ‰  โˆ…6. then InsertSegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , [๐‘ฅ, ๐‘ฅโ€ฒ])

โ€ข At each visited node ๐‘ฃ, either [๐‘ฅ, ๐‘ฅโ€ฒ] is stored or Int(๐‘ฃ) contains an endpoint

of [๐‘ฅ, ๐‘ฅโ€ฒ].

An interval is stored โ‰ค 2 times at each level.

Page 80: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Insertion

InsertSegmentTree(๐‘ฃ, [๐‘ฅ, ๐‘ฅโ€ฒ])

1. if Int(๐‘ฃ)โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ] // Int(parent(๐‘ฃ))โŠˆ [๐‘ฅ, ๐‘ฅโ€ฒ] holds2. then store [๐‘ฅ, ๐‘ฅโ€ฒ] at ๐‘ฃ3. else if Int(๐‘™๐‘ ๐‘ฃ )โˆฉ [๐‘ฅ, ๐‘ฅโ€ฒ] โ‰  โˆ…4. then InsertSegmentTree(๐‘™๐‘ ๐‘ฃ , [๐‘ฅ, ๐‘ฅโ€ฒ])

5. if Int(๐‘Ÿ๐‘ ๐‘ฃ )โˆฉ [๐‘ฅ, ๐‘ฅโ€ฒ] โ‰  โˆ…6. then InsertSegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , [๐‘ฅ, ๐‘ฅโ€ฒ])

โ€ข At each visited node ๐‘ฃ, either [๐‘ฅ, ๐‘ฅโ€ฒ] is stored or Int(๐‘ฃ) contains an endpoint

of [๐‘ฅ, ๐‘ฅโ€ฒ].

An interval is stored โ‰ค 2 times at each level.

At each depth, there exist

Page 81: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Insertion

InsertSegmentTree(๐‘ฃ, [๐‘ฅ, ๐‘ฅโ€ฒ])

1. if Int(๐‘ฃ)โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ] // Int(parent(๐‘ฃ))โŠˆ [๐‘ฅ, ๐‘ฅโ€ฒ] holds2. then store [๐‘ฅ, ๐‘ฅโ€ฒ] at ๐‘ฃ3. else if Int(๐‘™๐‘ ๐‘ฃ )โˆฉ [๐‘ฅ, ๐‘ฅโ€ฒ] โ‰  โˆ…4. then InsertSegmentTree(๐‘™๐‘ ๐‘ฃ , [๐‘ฅ, ๐‘ฅโ€ฒ])

5. if Int(๐‘Ÿ๐‘ ๐‘ฃ )โˆฉ [๐‘ฅ, ๐‘ฅโ€ฒ] โ‰  โˆ…6. then InsertSegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , [๐‘ฅ, ๐‘ฅโ€ฒ])

โ€ข At each visited node ๐‘ฃ, either [๐‘ฅ, ๐‘ฅโ€ฒ] is stored or Int(๐‘ฃ) contains an endpoint

of [๐‘ฅ, ๐‘ฅโ€ฒ].

An interval is stored โ‰ค 2 times at each level.

At each depth, there exist

โ‰ค 1 node ๐‘ข such that ๐‘ฅ โˆˆ Int(๐‘ข)

Page 82: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Insertion

InsertSegmentTree(๐‘ฃ, [๐‘ฅ, ๐‘ฅโ€ฒ])

1. if Int(๐‘ฃ)โŠ† [๐‘ฅ, ๐‘ฅโ€ฒ] // Int(parent(๐‘ฃ))โŠˆ [๐‘ฅ, ๐‘ฅโ€ฒ] holds2. then store [๐‘ฅ, ๐‘ฅโ€ฒ] at ๐‘ฃ3. else if Int(๐‘™๐‘ ๐‘ฃ )โˆฉ [๐‘ฅ, ๐‘ฅโ€ฒ] โ‰  โˆ…4. then InsertSegmentTree(๐‘™๐‘ ๐‘ฃ , [๐‘ฅ, ๐‘ฅโ€ฒ])

5. if Int(๐‘Ÿ๐‘ ๐‘ฃ )โˆฉ [๐‘ฅ, ๐‘ฅโ€ฒ] โ‰  โˆ…6. then InsertSegmentTree(๐‘Ÿ๐‘ ๐‘ฃ , [๐‘ฅ, ๐‘ฅโ€ฒ])

โ€ข At each visited node ๐‘ฃ, either [๐‘ฅ, ๐‘ฅโ€ฒ] is stored or Int(๐‘ฃ) contains an endpoint

of [๐‘ฅ, ๐‘ฅโ€ฒ].

An interval is stored โ‰ค 2 times at each level.

At each depth, there exist

โ‰ค 1 node ๐‘ข such that ๐‘ฅ โˆˆ Int(๐‘ข)

โ‰ค 1 node ๐‘ขโ€ฒ such that ๐‘ฅโ€ฒ โˆˆ Int(๐‘ขโ€ฒ)

Page 83: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Construction Time

โ€ข โ‰ค 2 storage actions + โ‰ค 2 containments

Page 84: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Construction Time

โ€ข โ‰ค 2 storage actions + โ‰ค 2 containments

โ‰ค 4 nodes visited per level.

Page 85: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Construction Time

โ€ข โ‰ค 2 storage actions + โ‰ค 2 containments

โ‰ค 4 nodes visited per level.

๐‘‚ log ๐‘› time to insert an interval.me to insert an interval.

Page 86: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Construction Time

โ€ข โ‰ค 2 storage actions + โ‰ค 2 containments

โ‰ค 4 nodes visited per level.

๐‘‚ log ๐‘› time to insert an interval.me to insert an interval.

๐‘‚ ๐‘› log ๐‘› time for segment tree construction.

Page 87: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Construction Time

โ€ข โ‰ค 2 storage actions + โ‰ค 2 containments

โ‰ค 4 nodes visited per level.

๐‘‚ log ๐‘› time to insert an interval.me to insert an interval.

๐‘‚ ๐‘› log ๐‘› time for segment tree construction.

Compared with an interval tree, a segment tree has

the same query time ๐‘‚(log ๐‘› + ๐‘˜)

a larger storage ๐‘‚(๐‘› log ๐‘›) than ๐‘‚ ๐‘› ,

Page 88: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Construction Time

โ€ข โ‰ค 2 storage actions + โ‰ค 2 containments

โ‰ค 4 nodes visited per level.

๐‘‚ log ๐‘› time to insert an interval.me to insert an interval.

๐‘‚ ๐‘› log ๐‘› time for segment tree construction.

Compared with an interval tree, a segment tree has

the same query time ๐‘‚(log ๐‘› + ๐‘˜)

a larger storage ๐‘‚(๐‘› log ๐‘›) than ๐‘‚ ๐‘› ,

but it allows to answer more complicated queries.

Page 89: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Back to Windowing

Query segment: ๐‘ž = ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ž

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ)

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )

๐‘†(๐‘ฃ2) ๐‘†(๐‘ฃ3)

๐‘†(๐‘ฃ1)

๐‘ 1

๐‘ 3

๐‘ 2

๐‘ 4

๐‘ 5

๐‘ 6

๐‘ 7

๐‘ฃ1

๐‘ฃ2 ๐‘ฃ3

๐ถ ๐‘ฃ1 = {๐‘ 3}

๐ถ ๐‘ฃ3 = {๐‘ 4, ๐‘ 6}๐ถ ๐‘ฃ2 = {๐‘ 1, ๐‘ 2}

Over ๐‘› arbitrarily oriented segments

Page 90: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Back to Windowing

Query segment: ๐‘ž = ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ž

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ)

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )

Construct a segment tree ๐’ฏ.

๐‘†(๐‘ฃ2) ๐‘†(๐‘ฃ3)

๐‘†(๐‘ฃ1)

๐‘ 1

๐‘ 3

๐‘ 2

๐‘ 4

๐‘ 5

๐‘ 6

๐‘ 7

๐‘ฃ1

๐‘ฃ2 ๐‘ฃ3

๐ถ ๐‘ฃ1 = {๐‘ 3}

๐ถ ๐‘ฃ3 = {๐‘ 4, ๐‘ 6}๐ถ ๐‘ฃ2 = {๐‘ 1, ๐‘ 2}

Over ๐‘› arbitrarily oriented segments

Page 91: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Back to Windowing

Query segment: ๐‘ž = ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ž

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ)

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )

Construct a segment tree ๐’ฏ.

On the ๐‘ฅ-intervals of the segments in ๐‘†.

Canonical subset ๐ถ(๐‘ฃ) at a vertex ๐‘ฃ store segments rather than their ๐‘ฅ-intervals.

๐‘†(๐‘ฃ2) ๐‘†(๐‘ฃ3)

๐‘†(๐‘ฃ1)

๐‘ 1

๐‘ 3

๐‘ 2

๐‘ 4

๐‘ 5

๐‘ 6

๐‘ 7

๐‘ฃ1

๐‘ฃ2 ๐‘ฃ3

๐ถ ๐‘ฃ1 = {๐‘ 3}

๐ถ ๐‘ฃ3 = {๐‘ 4, ๐‘ 6}๐ถ ๐‘ฃ2 = {๐‘ 1, ๐‘ 2}

Over ๐‘› arbitrarily oriented segments

Page 92: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Back to Windowing

Query segment: ๐‘ž = ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ž

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ)

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )

Construct a segment tree ๐’ฏ.

Segment ๐‘  โˆˆ ๐ถ(๐‘ฃ) if it crosses the slab ๐‘† ๐‘ฃ : Int(๐‘ฃ) ร— (โˆ’โˆž,โˆž)but does not cross its parentโ€™s slab.

On the ๐‘ฅ-intervals of the segments in ๐‘†.

Canonical subset ๐ถ(๐‘ฃ) at a vertex ๐‘ฃ store segments rather than their ๐‘ฅ-intervals.

๐‘†(๐‘ฃ2) ๐‘†(๐‘ฃ3)

๐‘†(๐‘ฃ1)

๐‘ 1

๐‘ 3

๐‘ 2

๐‘ 4

๐‘ 5

๐‘ 6

๐‘ 7

๐‘ฃ1

๐‘ฃ2 ๐‘ฃ3

๐ถ ๐‘ฃ1 = {๐‘ 3}

๐ถ ๐‘ฃ3 = {๐‘ 4, ๐‘ 6}๐ถ ๐‘ฃ2 = {๐‘ 1, ๐‘ 2}

Over ๐‘› arbitrarily oriented segments

Page 93: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Back to Windowing

Query segment: ๐‘ž = ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

๐‘ž

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ)

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )

Construct a segment tree ๐’ฏ.

Segment ๐‘  โˆˆ ๐ถ(๐‘ฃ) if it crosses the slab ๐‘† ๐‘ฃ : Int(๐‘ฃ) ร— (โˆ’โˆž,โˆž)but does not cross its parentโ€™s slab.

On the ๐‘ฅ-intervals of the segments in ๐‘†.

Canonical subset ๐ถ(๐‘ฃ) at a vertex ๐‘ฃ store segments rather than their ๐‘ฅ-intervals.

๐‘†(๐‘ฃ2) ๐‘†(๐‘ฃ3)

๐‘†(๐‘ฃ1)

๐‘ 1

๐‘ 3

๐‘ 2

๐‘ 4

๐‘ 5

๐‘ 6

๐‘ 7

๐‘ฃ1

๐‘ฃ2 ๐‘ฃ3

๐ถ ๐‘ฃ1 = {๐‘ 3}

๐ถ ๐‘ฃ3 = {๐‘ 4, ๐‘ 6}๐ถ ๐‘ฃ2 = {๐‘ 1, ๐‘ 2}

Over ๐‘› arbitrarily oriented segments

Page 94: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segments on the Search Path

โ€ข Search with ๐‘ž๐‘ฅ in ๐’ฏ. (โˆ’โˆž,โˆž)

Segment

tree

Page 95: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segments on the Search Path

โ€ข Search with ๐‘ž๐‘ฅ in ๐’ฏ. (โˆ’โˆž,โˆž)

๐‘ฃ

Segment

tree

Page 96: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segments on the Search Path

โ€ข Search with ๐‘ž๐‘ฅ in ๐’ฏ. (โˆ’โˆž,โˆž)

๐‘ฃ

Segment

treeโ€ข ๐‘‚(log๐‘›) canonical sets together include

all the segments intersected by the

vertical line ๐‘ฅ = ๐‘ž๐‘ฅ.

Page 97: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segments on the Search Path

โ€ข Search with ๐‘ž๐‘ฅ in ๐’ฏ. (โˆ’โˆž,โˆž)

๐‘ฃ

Segment

tree

๐ถ(๐‘ฃ)

โ€ข ๐‘ฃ is a node on the search path.

โ€ข ๐‘‚(log๐‘›) canonical sets together include

all the segments intersected by the

vertical line ๐‘ฅ = ๐‘ž๐‘ฅ.

Page 98: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segments on the Search Path

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ)

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )

๐‘ 

๐‘ž

โ€ข Search with ๐‘ž๐‘ฅ in ๐’ฏ. (โˆ’โˆž,โˆž)

๐‘ฃ

Segment

tree

๐ถ(๐‘ฃ)

โ€ข Segment ๐‘  โˆˆ ๐ถ(๐‘ฃ) is intersected by ๐‘ž iff

โ€ข ๐‘ฃ is a node on the search path.

โ€ข ๐‘‚(log๐‘›) canonical sets together include

all the segments intersected by the

vertical line ๐‘ฅ = ๐‘ž๐‘ฅ.

Page 99: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segments on the Search Path

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ)

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )

๐‘ 

๐‘ž

โ€ข Search with ๐‘ž๐‘ฅ in ๐’ฏ. (โˆ’โˆž,โˆž)

๐‘ฃ

Segment

tree

๐ถ(๐‘ฃ)

โ€ข Segment ๐‘  โˆˆ ๐ถ(๐‘ฃ) is intersected by ๐‘ž iff

โ€ข ๐‘ฃ is a node on the search path.

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ) below ๐‘  and (๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ ) above ๐‘ 

โ€ข ๐‘‚(log๐‘›) canonical sets together include

all the segments intersected by the

vertical line ๐‘ฅ = ๐‘ž๐‘ฅ.

Page 100: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of a Canonical Set

โ€ข Segments in ๐ถ(๐‘ฃ) do not intersect each other.(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

โ€ข Each segment is over an ๐‘ฅ-interval containing

Int ๐‘ฃ .

Int(๐‘ฃ)

๐‘ 1

๐‘ 2

๐‘ 3

๐‘ 4

๐‘ 5

Slab

Page 101: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of a Canonical Set

โ€ข Segments in ๐ถ(๐‘ฃ) do not intersect each other.(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

โ€ข These segments span the slab Int ๐‘ฃ ร— [โˆ’โˆž,โˆž].

โ€ข Each segment is over an ๐‘ฅ-interval containing

Int ๐‘ฃ .

Int(๐‘ฃ)

๐‘ 1

๐‘ 2

๐‘ 3

๐‘ 4

๐‘ 5

โ€ข They do not intersect each other in the interior.Slab

Page 102: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of a Canonical Set

โ€ข Segments in ๐ถ(๐‘ฃ) do not intersect each other.(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

โ€ข These segments span the slab Int ๐‘ฃ ร— [โˆ’โˆž,โˆž].

โ€ข Each segment is over an ๐‘ฅ-interval containing

Int ๐‘ฃ .

Int(๐‘ฃ)

๐‘ 1

๐‘ 2

๐‘ 3

๐‘ 4

๐‘ 5

โ€ข They do not intersect each other in the interior.

โ€ข Store the segments in a balanced BST โ„ฌ ๐‘ฃ in

the vertical order.

Slab

Page 103: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of a Canonical Set

โ€ข Segments in ๐ถ(๐‘ฃ) do not intersect each other.(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

โ€ข These segments span the slab Int ๐‘ฃ ร— [โˆ’โˆž,โˆž].

โ€ข Each segment is over an ๐‘ฅ-interval containing

Int ๐‘ฃ .

Int(๐‘ฃ)

๐‘ 1

๐‘ 2

๐‘ 3

๐‘ 4

๐‘ 5

โ€ข They do not intersect each other in the interior.

โ€ข Store the segments in a balanced BST โ„ฌ ๐‘ฃ in

the vertical order.

๐‘ 3

๐‘ 5 ๐‘ 2

๐‘ 1๐‘ 4

Slab

associate structure โ„ฌ ๐‘ฃ

Page 104: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Storage of a Canonical Set

โ€ข Segments in ๐ถ(๐‘ฃ) do not intersect each other.(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

โ€ข These segments span the slab Int ๐‘ฃ ร— [โˆ’โˆž,โˆž].

โ€ข Each segment is over an ๐‘ฅ-interval containing

Int ๐‘ฃ .

Int(๐‘ฃ)

๐‘ 1

๐‘ 2

๐‘ 3

๐‘ 4

๐‘ 5

โ€ข They do not intersect each other in the interior.

โ€ข Store the segments in a balanced BST โ„ฌ ๐‘ฃ in

the vertical order.

๐‘ 3

๐‘ 5 ๐‘ 2

๐‘ 1๐‘ 4

๐‘‚(log ๐‘› + ๐‘˜๐‘ฃ) query time

within โ„ฌ ๐‘ฃ

Slab

#intersected

segments in ๐ถ(๐‘ฃ)

associate structure โ„ฌ ๐‘ฃ

Page 105: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Segment Tree Storage Summary

The set ๐‘† of segments is stored in a segment tree ๐’ฏ based on

their ๐‘ฅ-intervals.

The canonical subset ๐ถ(๐‘ฃ) of every internal node ๐‘ฃ in ๐’ฏ is stored in

a BST โ„ฌ(๐‘ฃ) based on

the vertical order within the slab Int ๐‘ฃ ร— [โˆ’โˆž,โˆž].

Total storage: ๐‘‚(๐‘› log ๐‘›)

Construction time: ๐‘‚(๐‘› log ๐‘›)

Page 106: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of arbitrarily oriented segments

Page 107: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

Search with ๐‘ž๐‘ฅ in a segment tree ๐’ฏ.

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of arbitrarily oriented segments

Page 108: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

Search with ๐‘ž๐‘ฅ in a segment tree ๐’ฏ.

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of arbitrarily oriented segments

(โˆ’โˆž,โˆž)

Page 109: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

Search with ๐‘ž๐‘ฅ in a segment tree ๐’ฏ.

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of arbitrarily oriented segments

(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

Page 110: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

Search with ๐‘ž๐‘ฅ in a segment tree ๐’ฏ.

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of arbitrarily oriented segments

At every node ๐‘ฃ on the search path, search with the two endpoints

of ๐‘ž to report segments in ๐ถ(๐‘ฃ) intersected by ๐‘ž.

(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

Page 111: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

Search with ๐‘ž๐‘ฅ in a segment tree ๐’ฏ.

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of arbitrarily oriented segments

At every node ๐‘ฃ on the search path, search with the two endpoints

of ๐‘ž to report segments in ๐ถ(๐‘ฃ) intersected by ๐‘ž.

(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

โ„ฌ ๐‘ฃ

Page 112: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

Search with ๐‘ž๐‘ฅ in a segment tree ๐’ฏ.

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of arbitrarily oriented segments

At every node ๐‘ฃ on the search path, search with the two endpoints

of ๐‘ž to report segments in ๐ถ(๐‘ฃ) intersected by ๐‘ž.

(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

โ„ฌ ๐‘ฃ

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ)

Page 113: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

Search with ๐‘ž๐‘ฅ in a segment tree ๐’ฏ.

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of arbitrarily oriented segments

At every node ๐‘ฃ on the search path, search with the two endpoints

of ๐‘ž to report segments in ๐ถ(๐‘ฃ) intersected by ๐‘ž.

(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

โ„ฌ ๐‘ฃ

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ) (๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )

Page 114: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

Search with ๐‘ž๐‘ฅ in a segment tree ๐’ฏ.

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of arbitrarily oriented segments

At every node ๐‘ฃ on the search path, search with the two endpoints

of ๐‘ž to report segments in ๐ถ(๐‘ฃ) intersected by ๐‘ž.

(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

โ€ฆ

โ„ฌ ๐‘ฃ

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ) (๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )

Page 115: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

Search with ๐‘ž๐‘ฅ in a segment tree ๐’ฏ.

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of arbitrarily oriented segments

At every node ๐‘ฃ on the search path, search with the two endpoints

of ๐‘ž to report segments in ๐ถ(๐‘ฃ) intersected by ๐‘ž.

(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

โ€ฆ

segments satisfying the query

โ„ฌ ๐‘ฃ

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ) (๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )

Page 116: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

Search with ๐‘ž๐‘ฅ in a segment tree ๐’ฏ.

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of arbitrarily oriented segments

At every node ๐‘ฃ on the search path, search with the two endpoints

of ๐‘ž to report segments in ๐ถ(๐‘ฃ) intersected by ๐‘ž.

(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

โ€ข ๐‘‚(log ๐‘›) nodes on the search path, each requiring

such a search.

โ€ข The search takes ๐‘‚(log ๐‘› + ๐‘˜๐‘ฃ) time.

โ€ฆ

segments satisfying the query

โ„ฌ ๐‘ฃ

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ) (๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )

Page 117: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfย ยท Interval Tree + Priority Search Trees ๐ผ ๐‘ก ๐ผ๐‘Ÿ๐‘– โ„Ž๐‘ก ๐ผ๐‘– ๐ผ

Query Algorithm

Search with ๐‘ž๐‘ฅ in a segment tree ๐’ฏ.

Query object: a vertical line segment ๐‘ž: ๐‘ž๐‘ฅ ร— [๐‘ž๐‘ฆ, ๐‘ž๐‘ฆโ€ฒ ]

Set ๐‘† = ๐‘ 1, ๐‘ 2, โ€ฆ , ๐‘ ๐‘› of arbitrarily oriented segments

At every node ๐‘ฃ on the search path, search with the two endpoints

of ๐‘ž to report segments in ๐ถ(๐‘ฃ) intersected by ๐‘ž.

(โˆ’โˆž,โˆž)

๐‘ฃ๐ถ(๐‘ฃ)

โ€ข ๐‘‚(log ๐‘›) nodes on the search path, each requiring

such a search.

โ€ข The search takes ๐‘‚(log ๐‘› + ๐‘˜๐‘ฃ) time.

Query time: ๐‘‚(log2๐‘› + ๐‘˜)โ€ฆ

segments satisfying the query

โ„ฌ ๐‘ฃ

(๐‘ž๐‘ฅ, ๐‘ž๐‘ฆ) (๐‘ž๐‘ฅ, ๐‘ž๐‘ฆโ€ฒ )