Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfΒ Β·...
Transcript of Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfΒ Β·...
![Page 1: Interval Tree + Priority Search Treesweb.cs.iastate.edu/~cs518/handouts/28. segment trees.pdfΒ Β· Interval Tree + Priority Search Trees πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/1.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/2.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/3.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/4.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/5.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/6.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/7.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/8.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/9.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/10.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/11.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/12.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/13.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/14.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/15.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/16.jpg)
π 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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/17.jpg)
π 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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/18.jpg)
π 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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/19.jpg)
π 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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/20.jpg)
π 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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/21.jpg)
π 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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/22.jpg)
π 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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/23.jpg)
π 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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/24.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/25.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/26.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/27.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/28.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/29.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/30.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/31.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/32.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/33.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/34.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/35.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/36.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/37.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/38.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/39.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/40.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/41.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/42.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/43.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/44.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/45.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/46.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/47.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/48.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/49.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/50.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/51.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/52.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/53.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/54.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/55.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/56.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/57.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/58.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/59.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/60.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/61.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/62.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/63.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/64.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/65.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/66.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/67.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/68.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/69.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/70.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/71.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/72.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/73.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/74.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/75.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/76.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/77.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/78.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/79.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/80.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/81.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/82.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/83.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/84.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/85.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/86.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/87.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/88.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/89.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/90.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/91.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/92.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/93.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/94.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/95.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/96.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/97.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/98.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/99.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/100.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/101.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/102.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/103.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/104.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/105.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/106.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/107.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/108.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/109.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/110.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/111.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/112.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/113.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/114.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/115.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/116.jpg)
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 πΌ π‘ πΌππ βπ‘ πΌπ πΌ](https://reader034.fdocuments.us/reader034/viewer/2022052011/6026f193f39ef91648727de6/html5/thumbnails/117.jpg)
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
β¬ π£
(ππ₯, ππ¦) (ππ₯, ππ¦β² )