Sub topic 12
Deleting Intervals
This chapter assumes you know ordinary BST deletion (leaf, one child, two children) and the max-endpoint invariant from earlier chapters. Deletion removes a node as usual. After that, you must repair the max field of every node whose subtree changed.
Why `max` needs repair
Each node's max is the largest high endpoint in its subtree. If you delete the interval that supplied that value, the stored max is too large. A stale, too-large max never causes a missed overlap. It makes the search descend into subtrees that cannot match, so it wastes time and breaks the O(log n) bound. The reverse is the dangerous case: a max that is too small makes the search skip real overlaps. Recomputing max from scratch avoids both problems.
The recomputation rule uses only a node and its two children:
node.max = max(node.hi, left.max, right.max)
Missing children are ignored.
The three deletion cases
The in-order successor is the node with the smallest lo in the right subtree. Copying its interval into the node keeps the BST ordering intact.
Implementation
This code reuses Node and insert from the insertion chapter. It identifies an interval by both endpoints, because two stored intervals can share the same lo. Insertion sends equal lo values to the right, so deletion does the same when lo matches but hi does not.
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 recompute(node):
node.max = node.hi
if node.left is not None:
node.max = max(node.max, node.left.max)
if node.right is not None:
node.max = max(node.max, node.right.max)
def min_node(node):
while node.left is not None:
node = node.left
return node
def delete(node, lo, hi):
# Removes one stored interval equal to (lo, hi); returns the new subtree root.
if node is None:
return None
if lo < node.lo:
node.left = delete(node.left, lo, hi)
elif lo > node.lo or hi != node.hi:
node.right = delete(node.right, lo, hi)
else:
if node.left is None:
return node.right
if node.right is None:
return node.left
succ = min_node(node.right)
node.lo, node.hi = succ.lo, succ.hi
node.right = delete(node.right, succ.lo, succ.hi)
recompute(node)
return node
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
root = delete(root, 30, 40)
print(root.max) # 30
root = delete(root, 10, 30)
print(root.left.lo, root.left.max, root.max) # 12 20 20Deleting (30, 40) removes a leaf. Its ancestors recompute, and the root's max drops from 40 to 30. Deleting (10, 30) has two children, so (12, 15) takes its place and the root's max drops again.
Cost and rotations
Deletion walks one root-to-node path, then possibly one more path down to the successor. Each step does constant work, so the cost is O(h), which is O(log n) in a balanced tree. A self-balancing tree also rotates after deletion. Each rotation changes the children of two nodes, so recompute max for those two nodes, lower node first. The next chapter covers this.
Exercise
After deleting the interval that supplied a node's max, you forget to recompute max on the ancestors. Does find_overlap return wrong answers, or just slow ones? Explain.
Show answer
It can return wrong answers or waste time, depending on direction. A max that stays too large only causes needless descents, because the search follows the left child when it should not. The search still checks each visited node with the overlap test, so it stays correct. The cost is that the O(log n) guarantee is lost. A max that is too small is the real danger, and it happens if you recompute incorrectly, for example by ignoring a child. Then the search skips a subtree that holds an overlap.