An
interval tree is a
data structure that indexes
intervals so a query can find those that intersect a given window. A risk system can use one to check whether a proposed exposure window overlaps an existing position’s window as positions open and close.
What the tree stores
In the augmented balanced binary search tree version, each node holds one interval
[s,e), ordered by start
s. A record ID can break ties between equal starts. Each node also stores
max end, the largest end among its own interval and all its descendants. Recompute that value from the node’s own end and its children’s cached maxima. A rotation changes which intervals lie below the rotated nodes, so update the lower node before the new local root.
MIT OpenCourseWare on interval-tree augmentation
Use nonempty, half-open intervals, with the start included and the end excluded. A stored interval [s,e) overlaps query [a,b) exactly when
s<banda<e.
A subtree with max end at or before a can be skipped because every interval in it ends too soon. A node whose start is at or after b is too late, as is its entire right subtree. Its left subtree can still contain an earlier start.
Trace the query [4,9)
Four fictional exposure windows are measured in minutes from the start of a session. The root stores [3,8) and has max end 12 because its right child stores [9,12). The root overlaps the query. Its left subtree has max end 4, so the search skips [1,4) outright. On the right, [9,12) starts at the query’s excluded end. That node misses, but its left child [8,10) overlaps. The complete result is [3,8) and [8,10).
![Query [4, 9) overlaps [3, 8) and [8, 10). The tree prunes [1, 4) because its maximum end is 4. It skips [9, 12) because that node starts at 9, but checks its left child
8, 10).
The timeline shows where the two matches overlap the query. The tree rules out the left branch by its maximum end and skips the right child’s own interval by its start.
Finding one match and reporting every match
Find any overlap. Test the current node and stop on a hit. Otherwise go left if the left subtree’s max end is strictly greater than
a. If it is, some left interval ends after
a. That interval either overlaps or starts at or after
b, in which case every right interval also starts too late. If the left child is absent or its max end is at most
a, go right. Reaching a missing child means there is no match. On the same tree, a one-match search for
[8,9) goes from the root to
[9,12), then left to
[8,10).
One search takes
O(h) time for tree height
h. With a red-black tree and correctly maintained maxima, finding one overlap, inserting a record and deleting a record by its key each take
O(logn) worst-case time for
n stored intervals, using
O(n) space.
MIT OpenCourseWare on the search and its correctness
Report all overlaps. A depth-first traversal can use both pruning rules and visit both children when they might contain results. The traversal runs in
O(n) time in the worst case, and emitting
k matches takes
Ω(k) time. The one-path bound applies to finding a single match. For a different query, “which windows contain time
t?”, a static
centered interval tree groups intervals that contain each split point in endpoint-sorted lists. It can report all
k intervals containing
t in
O(logn+k) time by scanning only the relevant end of each list along a single path.
Mount’s lecture on interval trees and point stabbing
The SelfMadeQuant lesson shows how to maintain the cached values through rotations and works through a closed-interval search for one match. The preview is public and the full lesson requires membership.
Augmenting Data Structures
Sources
- Erik D. Demaine and Charles E. Leiserson (2005). Augmenting Data Structures, MIT OpenCourseWare, lecture 11.
- David M. Mount. Computational Geometry, lecture 24, “Interval Trees,” pp. 116–119.