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 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.

How Interval Tree: Introduction Works

  1. Think of each booking, gene segment or calendar event as a [low, high] pair.
  2. Nodes are ordered by their low endpoint, like a binary search tree.
  3. Every node also stores maxHigh: the largest high in its subtree.
  4. 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]

  1. Root [2, 6] overlaps [4, 7] → report it.
  2. Left child's maxHigh covers 4 → go left, find [1, 3] (no overlap).
  3. Right child [5, 8] overlaps [4, 7] → report it.
  4. Done: [2, 6] and [5, 8] overlap the query.

Output: Overlaps of [4, 7]: [2, 6], [5, 8]

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
Interval Tree: Introduction
Ranges with a maxHigh shortcut per subtree for overlap pruning.
[1, 3]
[5, 8]
[2, 6]
Visiting Overlaps query
Interval Tree: Introduction
Press ▶ Run to watch it step by step.
Python · Interval Tree in Code
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: intro

Interval Tree: Introduction Time & Space Complexity

TimeO(log n + k) per query for k overlaps
SpaceO(n)

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.