Sub topic 3
Why the Search Never Misses an Overlap
Assume you know binary search trees, recursion, and Big-O notation. In an augmented interval tree, each node stores max, the largest high endpoint in its subtree.
Why the Search Never Misses an Overlap
For a query interval [q_low, q_high], first test the current node. If it does not overlap, use the tree order and the max value:
- If the left subtree exists and
left.max >= q_low, search left. - Otherwise, search right.
This choice is safe. If left.max < q_low, every interval in the left subtree ends before the query begins, so the left subtree cannot help. If left.max >= q_low, the left subtree contains an interval that ends late enough to overlap in principle. When the current node starts no later than q_high, that interval also starts no later than q_high, so an overlap exists there. If the current node starts after q_high, the right subtree is impossible because all its intervals start even later.
Reporting Every Overlapping Interval
To report all matches, traverse both subtrees when they might contain results. Prune a subtree when its max < q_low. Also skip the right subtree when the current node's low endpoint is greater than q_high. The running time is O(h + k), where h is tree height and k is the number of reported intervals.
Deleting Intervals
Deletion follows ordinary BST deletion. After removing or replacing a node, recompute max while returning up the path:
max = max(node.hi, left.max, right.max)
A stale value can cause the search to prune a subtree that contains a valid match.
Keeping the Tree Balanced
An unbalanced tree can have height O(n). An AVL tree or red-black tree keeps height O(log n). Rotations preserve the low-endpoint ordering, but every rotated node must have its max value recomputed from its new children.
Time and Space Complexity
| Operation | Balanced augmented BST |
|---|---|
| Insert, delete, or find one overlap | `O(log n)` |
| Report `k` overlaps | `O(log n + k)` |
| Storage | `O(n)` |
An Alternative Design: The Centered Interval Tree
A centered interval tree chooses a center value and stores intervals crossing that center at the node. Intervals entirely to the left or right go into child trees. This can make the organization natural for static data, but updates and balancing are less uniform than in an augmented BST.
Interval Trees Versus Segment Trees and Other Structures
Use an augmented BST for dynamic intervals and arbitrary overlap queries. A segment tree is useful when endpoints come from a fixed discrete coordinate range and queries target points or ranges. It often uses more memory. Sorted endpoint arrays are simpler for static data but usually require rebuilding after updates.
Where Interval Trees Are Used
Common uses include calendar conflict detection, reservation systems, text-editor ranges, genomic coordinates, and collision windows.
Exercise
Why can a search prune a subtree whose maximum high endpoint is below q_low?
Show answer
Every interval in that subtree ends before the query begins, so none can overlap the query.