Sub topic 5
Quick Review: Binary Search Trees
This chapter recaps the binary search tree (BST) ideas that interval trees build on. It assumes you know Big-O notation, basic recursion, and can read short Python. If BSTs are fresh in your mind, skim the code and go to the exercise.
The Ordering Rule
A BST is a binary tree where each node holds a key. For every node:
- All keys in its left subtree are smaller than the node's key.
- All keys in its right subtree are larger than (or equal to) the node's key.
This rule holds at every node, not just at the root. It lets you discard half of a subtree at each step, the same way binary search discards half of a sorted array.
Interval trees reuse this rule. The key for an interval is its low endpoint, so intervals with smaller low endpoints go left.
Insertion and Search
Insertion walks down from the root, going left or right by comparing keys, and attaches a new leaf where the walk falls off the tree. Search follows the same path.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def insert(node, key):
if node is None:
return Node(key)
if key < node.key:
node.left = insert(node.left, key)
else:
node.right = insert(node.right, key)
return node
def contains(node, key):
while node is not None:
if key == node.key:
return True
node = node.left if key < node.key else node.right
return False
root = None
for k in [15, 10, 20, 5, 12, 30]:
root = insert(root, k)
print(contains(root, 12)) # True
print(contains(root, 13)) # FalseNote that insert returns the node and the caller reassigns the child: node.left = insert(node.left, key). The interval tree's insertion uses this same pattern. It gives you a natural place to update extra data on the way back up the recursion.
In-Order Traversal
Visiting the left subtree, then the node, then the right subtree yields keys in sorted order. This is a quick way to check that your tree obeys the ordering rule.
def in_order(node, out):
if node is None:
return
in_order(node.left, out)
out.append(node.key)
in_order(node.right, out)
keys = []
in_order(root, keys)
print(keys) # [5, 10, 12, 15, 20, 30]Height Controls the Cost
Search and insertion each follow one root-to-leaf path, so they cost O(h), where h is the tree's height. The shape of the tree depends on insertion order.
| Tree shape | Height | Search cost |
|---|---|---|
| Balanced | about log n | O(log n) |
| Inserted in sorted order | n | O(n) |
Inserting 1, 2, 3, 4, 5 in that order produces a chain that behaves like a linked list. Later chapters on balancing address this. For now, remember that every O(log n) claim about interval trees assumes the height stays near log n.
What Comes Next
A plain BST can find a key quickly. It cannot tell you which stored intervals overlap a query, because ordering by low endpoint says nothing about high endpoints. The next chapters fix this by storing extra data in each node.
Exercise
You insert the keys 8, 3, 10, 1, 6 into an empty BST in that order. What does an in-order traversal print, and what is the height of the tree, counting the root as height 1?
Show answer
The traversal prints [1, 3, 6, 8, 10], which is sorted order. The tree has 8 at the root, 3 and 10 as its children, and 1 and 6 as children of 3, so its height is 3.