Sub topic 15
An Alternative Design: The Centered Interval Tree
This chapter assumes you have read the augmented-BST chapters. You should know the closed-interval overlap test, the max-endpoint invariant, and the O(log n) height of a balanced tree. You should also be comfortable with recursion and short Python programs.
The augmented BST keeps intervals in one ordered tree and stores a max field in each node. The centered interval tree takes a different approach. It picks a center point, stores every interval that crosses that point at the node, and sends the intervals that lie entirely to one side into subtrees.
The Core Idea
Each node holds a center value center. Every interval in the set falls into one of three groups relative to that node:
- Left: the interval ends before the center (
hi < center). It goes into the left subtree. - Right: the interval starts after the center (
lo > center). It goes into the right subtree. - Middle: the interval contains the center (
lo <= center <= hi). It stays at this node.
The node stores its middle intervals in two sorted lists:
by_low, sorted by low endpoint ascending.by_high, sorted by high endpoint descending.
The two lists hold the same intervals in different orders. Each order lets a query stop scanning early.
Why Two Sorted Lists Help
Every middle interval contains center. Take a query interval [lo, hi] that lies entirely left of the center, so hi < center. Each middle interval has a high endpoint of at least center, which is above hi. So the high endpoint cannot decide whether it overlaps. Only the low endpoint matters: a middle interval overlaps the query exactly when its low endpoint is at most hi.
Scanning by_low from the smallest low endpoint, you report intervals until you reach one with lo' > hi. Every later interval has an even larger low endpoint, so you stop. The scan costs the number of reported intervals plus one failed comparison. It never touches an interval that does not overlap.
A query entirely right of the center works the same way with by_high. You scan from the largest high endpoint and stop at the first one below lo.
An Example Tree
Take the intervals (15, 20), (10, 30), (17, 19), (5, 20), (12, 15) and (30, 40). Choose the root center as the median of all endpoints, which is 19.
Four intervals contain 19, so they stay at the root. (12, 15) ends before 19, so it goes left. (30, 40) starts after 19, so it goes right.
Building the Tree
The build picks the median endpoint as the center. It partitions the intervals into the three groups and recurses on the left and right groups.
class CNode:
def __init__(self, center, mid, left, right):
self.center = center
self.by_low = sorted(mid) # ascending low
self.by_high = sorted(mid, key=lambda iv: iv[1], reverse=True) # descending high
self.left = left
self.right = right
def build(intervals):
if not intervals:
return None
endpoints = sorted(p for iv in intervals for p in iv)
center = endpoints[len(endpoints) // 2]
left, right, mid = [], [], []
for lo, hi in intervals:
if hi < center:
left.append((lo, hi))
elif lo > center:
right.append((lo, hi))
else:
mid.append((lo, hi))
return CNode(center, mid, build(left), build(right))The recursion always terminates. The center is an endpoint of some interval, so that interval contains the center and lands in mid. Each recursive call therefore receives strictly fewer intervals than it was given.
Choosing the median of the endpoints puts at most half of the endpoints in each side, so the depth is O(log n). Each level sorts its middle lists, so the full build costs O(n log n).
Querying for Overlaps
The query takes an interval [lo, hi]. For a stabbing query at a point q, pass lo = hi = q. There are three cases at each node:
- The query lies left of the center (
hi < center). Scanby_lowwhilelo' <= hi, then recurse left only. - The query lies right of the center (
lo > center). Scanby_highwhilehi' >= lo, then recurse right only. - The query contains the center. Every middle interval overlaps, so report all of them and recurse into both children.
def query(node, lo, hi, out):
# Appends every stored interval overlapping [lo, hi] to out.
if node is None:
return
if hi < node.center:
for iv in node.by_low:
if iv[0] > hi:
break
out.append(iv)
query(node.left, lo, hi, out)
elif lo > node.center:
for iv in node.by_high:
if iv[1] < lo:
break
out.append(iv)
query(node.right, lo, hi, out)
else:
out.extend(node.by_low)
query(node.left, lo, hi, out)
query(node.right, lo, hi, out)
root = build([(15, 20), (10, 30), (17, 19), (5, 20), (12, 15), (30, 40)])
result = []
query(root, 14, 16, result)
print(sorted(result)) # [(5, 20), (10, 30), (12, 15), (15, 20)]
result = []
query(root, 25, 25, result)
print(result) # [(10, 30)]
result = []
query(root, 45, 50, result)
print(result) # []The first query matches the output of the augmented-BST find_all on the same data. That gives you a cross-check between the two designs.
Why the Query Is Correct
In case 1, a right-subtree interval has lo' > center > hi, so it cannot overlap the query. A left-subtree interval might. A middle interval has hi' >= center > hi, so it overlaps exactly when lo' <= hi. That is the condition the scan tests.
Case 2 mirrors case 1. In case 3 the query contains the center and every middle interval contains the center, so they all share that point with the query. Both subtrees may hold overlaps, so both are searched.
Complexity
For a balanced build, the cost has two parts:
- Walking down: the query visits O(log n) nodes. Only case 3 branches, and once the query contains a center, the two child searches each run a one-sided query. A one-sided query follows a single path.
- Scanning lists: each scan reports only overlapping intervals plus at most one failed comparison per visited node.
The total is O(log n + k), where k is the number of reported intervals. Space is O(n), because each interval is stored at exactly one node, in two lists.
Comparison with the Augmented BST
| Property | Centered interval tree | Augmented BST |
|---|---|---|
| Build from n intervals | O(n log n) | O(n log n) by repeated insertion |
| Find all k overlaps | O(log n + k) | O(k log n) |
| Insert or delete | Not supported cheaply | O(log n) with balancing |
| Extra data per node | Center, two sorted lists | One max value |
| Best fit | Static or rarely changing sets | Sets that change often |
The query time is the centered tree's advantage. The augmented BST's find_all can revisit many internal nodes for each reported interval, which gives the O(k log n) bound. The centered tree pays for that speed with a rigid structure.
Limitations: Updates
Inserting an interval means finding the node where it first crosses a center, then inserting into two sorted lists. Python lists make that O(m) for a node with m middle intervals. Replacing the lists with balanced trees fixes the cost, but the structure becomes complicated.
A new interval can also force a new node. That node's center may unbalance the tree, and the tree has no simple rotation to fix it, because the center values are tied to how intervals were partitioned. Most implementations treat the centered tree as static. They rebuild it periodically or when enough updates accumulate.
Exercise
A centered tree node has center 50 and middle intervals (40, 55), (45, 90), (50, 60). A query asks for overlaps with [10, 42]. Which interval does the scan of by_low report, and where does the scan stop?
Show answer
The query lies left of the center (42 < 50), so the node scans by_low, which is (40, 55), (45, 90), (50, 60). It reports (40, 55) because 40 <= 42. It stops at (45, 90) because 45 > 42, and by sorted order no later interval can overlap either. Only (40, 55) is reported, and the search continues into the left subtree.