disambiguation:
A segment tree is a DS that let’s you do things like query for range sums and range maxes in an array (dynamically). This is not at all related to what I talk about today.
An interval tree is a DS that stores some intervals, and on a query point returns the set of intervals containing that point.
Interval tree:
def build_interval_tree(intervals):
= median
middlept = [interval I if I is completely to the left of middlept]
left_intervals = [ditto but right]
right_intervals = [intervals that touch the middlept]
center_intervals
store build_interval_tree(left_intervals)
store build_interval_tree(right_intervals)sorted list of center_intervals (keyed by left endpoint and by right endpoint).
store
def handle_query(interval_tree, point):
wlog let point be to the left of center
recursively handle_query(left_interval_tree, point)list all the center_intervals with start coord before point.
maybe it’s more complicated to make this dynamic?