Sub topic 14
Time and Space Complexity
This chapter assumes you know Big-O notation, tree height, and the augmented-BST interval tree from the earlier chapters. Each node stores an interval and a max field, and the tree is ordered by low endpoint. Here n is the number of stored intervals, h is the tree height, and k is the number of intervals a query reports.
Everything Depends on Height
Every operation walks one root-to-leaf path and does constant work at each node. The cost is therefore O(h), and the real question is how large h is.
| Operation | Balanced tree | Unbalanced worst case |
|---|---|---|
| Insert | O(log n) | O(n) |
| Delete | O(log n) | O(n) |
| Find one overlap | O(log n) | O(n) |
| Report all overlaps | O(min(n, k log n)) | O(n) |
| Build from n intervals | O(n log n) | O(n²) |
| Space | O(n) | O(n) |
Why Each Bound Holds
- Insertion visits one node per level and updates
maxon the way back up. The update is one comparison per node, so the cost is O(h). - Finding one overlap moves down one child per step and never backtracks. The previous chapter showed that this is safe, so the cost is O(h).
- Deletion is a BST deletion plus
maxrecomputation along the affected path. That is also O(h). - Rotations change the subtree structure only locally. After a rotation you recompute
maxfor the two nodes involved, lower node first, from their children and their ownhi. This is O(1) per rotation, so a self-balancing scheme such as a red-black tree or AVL tree keeps every operation at O(log n). - Space is one node per interval, each holding two endpoints, a
max, and two child references. That is O(n) with a small constant.
Reporting All Overlaps
The find_all function from the earlier chapter prunes a subtree only when its max is below the query's low endpoint. A subtree that is visited can still contain no overlap, so it is not a plain O(k + log n) traversal.
The standard bound is O(min(n, k log n)). Each reported interval costs at most one extra path of length O(log n), and the traversal can never visit more than n nodes. When k is small, the cost stays close to a single search. When k approaches n, the cost approaches a full traversal.
Seeing the Height Problem
The tree in this chapter has no balancing code. Insertion order therefore decides the height.
import random
class Node:
def __init__(self, lo, hi):
self.lo, self.hi, self.max = lo, hi, hi
self.left = 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 height(node):
if node is None:
return 0
return 1 + max(height(node.left), height(node.right))
n = 500
sorted_root = None
for i in range(n):
sorted_root = insert(sorted_root, i, i + 1)
keys = list(range(n))
random.seed(1)
random.shuffle(keys)
shuffled_root = None
for i in keys:
shuffled_root = insert(shuffled_root, i, i + 1)
print(height(sorted_root)) # 500, the tree is a linked list
print(height(shuffled_root)) # far smaller, a small multiple of log2(500) ≈ 9Inserting intervals in increasing order of low endpoint turns the tree into a linked list, and every query becomes a linear scan. Real systems use calendars, event logs, and sorted imports that often arrive in sorted order. Balancing is therefore a requirement for the bounds above to hold in practice.
Exercise
A balanced interval tree holds 1,000,000 intervals. A query overlaps 3 of them. Roughly how many nodes does find_all visit, and why is the cost not simply 3?
Show answer
About 3 × log2(1,000,000) ≈ 60 nodes at most, since the bound is O(k log n) when k is small. The traversal reaches each of the 3 matches along a root-to-node path of about 20 nodes. It may also descend into subtrees whose max is at least the query's low endpoint but that contain no overlap. Pruning by max is therefore not exact, and the cost is not exactly k.