Sub topic 17
Where Interval Trees Are Used
This chapter assumes you know the augmented tree from the earlier chapters: the max-endpoint invariant, insert, and find_overlap. It shows where that structure pays off and how to recognize the problems it fits.
The Pattern to Look For
An interval tree fits when two things are true:
- Your data are ranges, such as time spans, address ranges, or coordinate ranges.
- You repeatedly ask which stored ranges overlap a given range, while ranges are added and removed over time.
If the set never changes, sorting plus a one-time build may be enough. If you only ask about points, a simpler structure may work. The tree earns its keep when the set is dynamic and queries are frequent.
Common Applications
| Domain | What the intervals are | Query |
|---|---|---|
| Calendars and scheduling | Meeting start and end times | Does a new meeting conflict with an existing one? |
| Genomics | Gene or feature positions on a chromosome | Which features overlap this region? |
| Networking | IP address ranges in firewall or routing rules | Which rules cover this range? |
| Databases | Valid-time ranges of rows | Which rows were valid during this period? |
| Graphics and games | Extents of objects along one axis | Which objects might collide on this axis? |
| Memory management | Allocated address ranges | Does a new allocation overlap an old one? |
Worked Example: Conflict-Free Booking
A booking system should accept a meeting only if it overlaps nothing already booked. We store times as minutes since midnight. Because our intervals are closed, a meeting from 9:00 to 9:59 is stored as (540, 599). This lets a meeting that starts at 10:00 follow it without a conflict.
class Node:
def __init__(self, lo, hi):
self.lo = lo
self.hi = hi
self.max = hi
self.left = None
self.right = None
def insert(node, lo, hi):
if node is None:
return Node(lo, hi)
if lo < node.lo:
node.left = insert(node.left, lo, hi)
else:
node.right = insert(node.right, lo, hi)
node.max = max(node.max, hi)
return node
def find_overlap(node, lo, hi):
while node is not None:
if node.lo <= hi and lo <= node.hi:
return (node.lo, node.hi)
if node.left is not None and node.left.max >= lo:
node = node.left
else:
node = node.right
return None
def book(root, lo, hi):
# Returns (new_root, accepted).
if find_overlap(root, lo, hi) is not None:
return root, False
return insert(root, lo, hi), True
root = None
for lo, hi in [(540, 599), (600, 659), (590, 610), (660, 720)]:
root, ok = book(root, lo, hi)
print((lo, hi), "booked" if ok else "conflict")The output is:
(540, 599) booked
(600, 659) booked
(590, 610) conflict
(660, 720) bookedThe check before each insert is one find_overlap call. In a balanced tree it costs O(log n), where a list scan would cost O(n) per booking.
Choosing Between Designs in Practice
- For "is there any conflict?" checks, as above,
find_overlapis enough. - For "list everything in this region", as in genomics or rule lookup, use
find_all. Its cost is O(log n + k) in a balanced tree, where k is the number of results. - Plain insertion degrades to O(n) on sorted input. Bookings often arrive in time order, so production code needs a balanced variant such as a red-black or AVL tree with max maintained in rotations.
- Many libraries already provide this. Examples include the
intervaltreepackage for Python and the interval-tree-based maps in genomics toolkits. Check for one before writing your own.
Exercise
A scheduling service receives meetings in strictly increasing start-time order and uses the unbalanced tree from this chapter. Why will it slow down over time, and what change fixes it?
Show answer
Inserting in sorted order always goes right, so the tree becomes a chain with height n, and each insert and overlap search costs O(n). Using a self-balancing tree (such as red-black or AVL) that updates the max field during rotations keeps the height at O(log n).