Sub topic 7
The Max-Endpoint Invariant
This chapter assumes you can read a binary search tree (BST) insertion and know the closed-interval overlap test from earlier chapters. Here you will learn the one extra field that makes interval trees fast.
The Idea
An interval tree is a BST keyed by the low endpoint of each interval. Ordering by low endpoint alone cannot answer overlap queries quickly. A node with a small low endpoint may have a very large high endpoint, so any subtree might hide an overlap.
The fix is to store one more value in every node. The max field is the largest high endpoint of any interval in that node's subtree, including the node itself.
The Invariant
For every node n, the following must always hold:
n.max = the maximum of n.hi, n.left.max (if the left child exists), n.right.max (if the right child exists)This is an invariant, a condition every operation must preserve. It is local, because a node's max depends only on its own hi and its two children's max values. That makes it cheap to repair after a change.
A Worked Example
Insert (15,20), (10,30), (17,19), (5,20), (12,15), (30,40) in that order. The tree below shows each node's interval and its max.
Take the node [17,19]. Its own hi is 19 and its right child's max is 40, so its max is 40. The root takes the largest of 20, 30 and 40, which is 40.
Checking the Invariant in Code
A checker like this is useful when you debug insertion, deletion, or rotations. It recomputes each node's expected max from its children and compares.
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 check_max(node):
if node is None:
return True
expected = node.hi
for child in (node.left, node.right):
if child is not None:
expected = max(expected, child.max)
return (node.max == expected
and check_max(node.left)
and check_max(node.right))
root = None
for lo, hi in [(15, 20), (10, 30), (17, 19), (5, 20), (12, 15), (30, 40)]:
root = insert(root, lo, hi)
print(root.max) # 40
print(check_max(root)) # TrueWhy Insertion Keeps It Valid
insert updates node.max on the way back up the recursion. Every node on the insertion path gets max(node.max, hi). Nodes off the path have unchanged subtrees, so their max values stay correct. The cost is O(h), the same as an ordinary BST insertion, where h is the tree height.
Common Errors
- Initializing
maxto 0 orloinstead ofhi. A fresh node's max is its ownhi. - Updating
maxonly at the root. Every node on the path must update. - Keying the tree by
hiwhile storing max oflo. Choose one convention: key bylo, augment with maxhi.
Exercise
A node has interval (8, 12). Its left subtree contains intervals with high endpoints 9 and 14, and its right subtree is empty. What is the node's max?
Show answer
14. The max is the largest of the node's own hi (12) and its children's max values (14 from the left subtree, and nothing from the empty right subtree).