interval tree + priority search treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfΒ Β·...

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 ( , ) ( , β€² )

Upload: others

Post on 03-Oct-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

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

ℬ 𝑣

(π‘žπ‘₯, π‘žπ‘¦) (π‘žπ‘₯, π‘žπ‘¦β€² )