Sub topic 16
Interval Trees Versus Segment Trees and Other Structures
This chapter assumes you know the augmented-BST interval tree from the earlier chapters, including the max-endpoint invariant, the O(log n) search for one overlap, and the cost of reporting all overlaps. It also assumes you know what a centered interval tree is. Here you decide when each structure is the right choice.
The Candidates
- Augmented BST interval tree. This is the structure you built. It is ordered by low endpoint, and each node stores the subtree max.
- Centered interval tree. Each node picks a center point. Intervals containing the center stay at that node, in two sorted lists. The rest go to the left or right child.
- Segment tree. It is built over the sorted distinct endpoints. Each interval is split into O(log n) canonical pieces, and each piece is stored at one node.
- Plain sorted array or list. This is the baseline from the linear-scan chapter.
Comparison
Here n is the number of intervals and k is the number of reported results.
| Structure | Space | Build or insert | Find one overlap | Report all |
|---|---|---|---|---|
| Linear scan | O(n) | O(1) per insert | O(n) | O(n) |
| Augmented BST (balanced) | O(n) | O(log n) insert and delete | O(log n) | O(min(n, k log n)) with the simple traversal |
| Centered interval tree | O(n) | O(n log n) build | O(log n) | O(log n + k) |
| Segment tree | O(n log n) | O(n log n) build | O(log n) | O(log n + k) |
The centered tree and segment tree have better reporting bounds. The augmented BST wins on flexibility. It is the only one in the table that supports cheap insertion and deletion of arbitrary intervals, with no setup beyond a balanced BST.
Gotchas
- Segment tree has two meanings. In competitive programming, a "segment tree" usually means a tree over array indices that answers range sums or minimums. That structure does not store intervals. The interval-storing version described above is the one that competes with interval trees.
- Segment trees want known coordinates. The tree shape depends on the set of endpoints. If new intervals arrive with unseen endpoints, you must rebuild the tree or use a dynamic variant. An augmented BST has no such limit.
- Centered trees are mostly static. Inserting an interval can change which center it belongs to. Dynamic versions exist, but they are more complex than a balanced augmented BST.
- Segment trees cost more memory. Storing each interval in up to O(log n) nodes gives O(n log n) space.
- Point-stabbing versus general overlap. All three handle both queries. A stabbing query is an overlap query with lo equal to hi.
Choosing
If the stored intervals never overlap each other, none of these is needed. A BST keyed by low endpoint, with a predecessor lookup, answers a stabbing query directly. For rectangles or higher dimensions, intervals no longer apply. Look at structures such as R-trees or k-d trees.
Exercise
Your service stores booking intervals that are added and cancelled all day, and it must answer "is this time slot already taken?" Which structure fits best, and why?
Show answer
An augmented BST interval tree, kept balanced. Insertions and deletions cost O(log n), and the "is it taken?" question only needs find-one-overlap, which is O(log n). A centered tree or segment tree would need rebuilding or a more complex dynamic variant.