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
    7

  • 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

ℬ 𝑣

(𝑞𝑥, 𝑞𝑦) (𝑞𝑥, 𝑞𝑦′ )