Data Structures · Visualization
Interval Tree: Introduction
Interval Tree
An interval tree stores ranges like [1, 5] instead of single points, so you can ask 'which stored ranges overlap my query?' Each node keeps one interval plus the biggest endpoint in its subtree — that one number lets whole branches be skipped during a search. Time: O(log n + k) per query for k overlaps. Space: O(n).
Big-O: Insert O(log n); overlap query O(log n + k).
Interval Tree: Introduction
Interval TreeAn interval tree stores ranges like [1, 5] instead of single points, so you can ask 'which stored ranges overlap my query?' Each node keeps one interval plus the biggest endpoint in its subtree — that one number lets whole branches be skipped during a search.
How Interval Tree: Introduction Works
- Think of each booking, gene segment or calendar event as a [low, high] pair.
- Nodes are ordered by their low endpoint, like a binary search tree.
- Every node also stores maxHigh: the largest high in its subtree.
- Overlap search prunes any branch whose maxHigh is below the query's low.
Interval Tree: Introduction Worked Example
Input: Store [1, 3], [5, 8], [2, 6] then query [4, 7]
- Root [2, 6] overlaps [4, 7] → report it.
- Left child's maxHigh covers 4 → go left, find [1, 3] (no overlap).
- Right child [5, 8] overlaps [4, 7] → report it.
- Done: [2, 6] and [5, 8] overlap the query.
Output: Overlaps of [4, 7]: [2, 6], [5, 8]
Press ▶ Run to watch it step by step.
def insert(root, iv):
if not root:
return {'iv': iv, 'max': iv[1]}
if iv[0] < root['iv'][0]:
root['left'] = insert(root.get('left'), iv)
else:
root['right'] = insert(root.get('right'), iv)
root['max'] = max(root['iv'][1], ...) # repair up
return root
# example: introInterval Tree: Introduction Time & Space Complexity
When to Use Interval Tree: Introduction
- Calendar conflict detection and room-booking systems
- Genomic interval overlap queries over millions of segments
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.