Interval Trees: Efficient Range and Overlap Queries
An **Interval Tree** is an augmented data structure designed to hold intervals and efficiently answer queries about which intervals overlap with a given point or another interval. In computational geometry and windowing systems, they are the primary tool for managing dynamic sets of ranges.
1. Augmented Interval Tree (BST-based)
The most common implementation is an augmentation of a self-balancing binary search tree (like a Red-Black Tree). Each node $x$ contains:
* **Interval:** $[low[x], high[x]]$
* **Key:** $low[x]$ (the tree is ordered by the start of the interval).
* **Max:** The maximum value of any interval endpoint in the subtree rooted at $x$.
Complexity Analysis
| Operation | Complexity |
| :--- | :--- |
| **Space** | $O(n)$ |
| **Insertion / Deletion** | $O(\log n)$ |
| **Overlap Search** | $O(\log n)$ |
2. Augmented Interval Tree vs. Segment Tree
While both handle intervals, they excel in different scenarios:
| Feature | Augmented Interval Tree | Segment Tree |
| :--- | :--- | :--- |
| **Best For** | Overlap queries in dynamic sets. | Point-in-interval and range aggregate (min/max/sum) queries. |
| **Space** | $O(n)$ — highly memory efficient. | $O(n \log n)$ or $O(n)$ with coordinate compression. |
| **Dynamic Updates** | Native support via BST rotations. | Difficult; often requires rebuilding or lazy propagation. |
3. Real-World Applications
* **Virtual Memory Management:** Linux kernels use interval trees (via `rb_tree`) to track virtual memory areas (VMAs).
* **Collision Detection:** In game engines, finding all bounding boxes that overlap with a projectile's path.
* **Calendar and Scheduling:** Finding all meetings that occur during a specific time block.
4. Implementation Example (Pseudocode)
```python
def interval_search(root, i):
i is the query interval [low, high]
current = root
while current is not None and not overlaps(current.interval, i):
if current.left is not None and current.left.max >= i.low:
current = current.left
else:
current = current.right
return current
```
For more foundational structures, refer to the [Data Structures Hub](DataStructuresHub).