Sub topic 12
The binary search tree property
This chapter assumes you can write a binary tree node class, you understand recursion, and you know that inorder traversal visits left subtree, node, right subtree.
The rule
A binary search tree (BST) is a binary tree in which every node obeys one ordering rule:
- Every key in the node's left subtree is smaller than the node's key.
- Every key in the node's right subtree is larger than the node's key.
- Both subtrees are themselves binary search trees.
The rule covers the whole subtree, not only the two children. This is the point people most often miss.
In this tree, 4 sits under 3 and also under 5. It must be smaller than 5 and larger than 3. It is, so the tree is valid.
Why checking children is not enough
Consider a tree with root 5, left child 3, and a right child 4 under 3 (so 3's right child is 4, and 4 has a right child 7). Every parent is compared correctly with its immediate children, but 7 is in the left subtree of 5 and is larger than 5. The tree breaks the rule.
To validate a tree, pass down the allowed range of keys. Each step narrows the range.
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def is_bst(node, low=None, high=None):
if node is None:
return True
if low is not None and node.value <= low:
return False
if high is not None and node.value >= high:
return False
return (is_bst(node.left, low, node.value) and
is_bst(node.right, node.value, high))
good = Node(5, Node(3, Node(1), Node(4)), Node(8))
bad = Node(5, Node(3, None, Node(7)), Node(8))
print(is_bst(good)) # True
print(is_bst(bad)) # FalseGoing left sets the upper bound to the current key. Going right sets the lower bound. The node holding 7 inherits the bound high = 5 from the root, so it fails.
What the property gives you
At each node, one comparison tells you which half of the remaining keys can contain your target. The other subtree can be skipped entirely. Search and insert therefore follow a single path from the root, and their cost is proportional to the height of the tree. The next chapter implements both operations.
Inorder traversal of a BST also visits keys in ascending order. This follows directly from the rule, because the traversal finishes the smaller left subtree, then the node, then the larger right subtree. A later chapter shows the proof.
Duplicates
The rule above uses strict inequalities, so it allows no duplicate keys. If you need duplicates, choose one convention and apply it everywhere, for example "equal keys go in the right subtree". Then change the checks in is_bst to match (< instead of <= for the low bound). Mixing conventions makes search results unpredictable.
Exercise
A tree has root 10, left child 5, and right child 15. Node 15 has a left child 8. Is this a valid BST? Explain which check fails or passes.
Show answer
It is not valid. Node 8 is in the right subtree of 10, so it must be larger than 10. Its parent 15 accepts it, because 8 < 15, but the lower bound of 10 inherited from the root rejects it.