Sub topic 2
The ordering property that defines a BST
This chapter assumes you can read Python classes, follow recursion, and use introductory Big-O notation. You should also know what a binary tree is: each node has at most a left child and a right child.
The rule
A binary search tree (BST) is a binary tree where every node obeys one 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.
The rule applies to the whole subtree, not only to the two children. This is the source of most mistakes, and the next section shows why.
The tree below is a valid BST.
Every key under 30 is below 50, and every key under 70 is above 50. The same holds at 30 and 70 for their own subtrees.
The common mistake: checking only the children
A tempting test is "each node is larger than its left child and smaller than its right child." That test is too weak. Look at this tree:
Each parent-child pair looks fine: 30 is less than 50, and 60 is greater than 30. But 60 sits in the left subtree of 50 and is larger than 50, so the tree is not a BST. The rule must hold for every ancestor, not just the parent.
Checking validity with bounds
To check the full rule, pass down a range of allowed keys. The root may hold any key. When you go left, the current key becomes the new upper bound. When you go right, it becomes the new lower bound.
class Node:
def __init__(self, key, left=None, right=None):
self.key = key
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.key <= low:
return False
if high is not None and node.key >= high:
return False
return (is_bst(node.left, low, node.key) and
is_bst(node.right, node.key, high))
good = Node(50, Node(30, Node(20), Node(40)), Node(70))
bad = Node(50, Node(30, None, Node(60)))
print(is_bst(good)) # True
print(is_bst(bad)) # FalseFor bad, the call on node 60 receives low=30 and high=50. Since 60 is not below 50, the check fails. The function visits each node once, so it runs in O(n) time.
The comparisons here are strict (<= and >= reject), so this version treats equal keys as invalid. A later chapter covers how to handle duplicates.
Why the rule is useful
The rule gives you a decision at every node. If the target key is smaller than the current key, the whole right subtree cannot contain it, so you skip it. If it is larger, you skip the left subtree. Each comparison discards a whole branch.
A second consequence is that reading the keys left subtree, then node, then right subtree gives sorted order. The traversal chapter explains why this works.
Summary of checks
| Question | Answer for a valid BST |
|---|---|
| Must every left descendant be smaller than a node? | Yes, at any depth |
| Must every right descendant be larger than a node? | Yes, at any depth |
| Is comparing only parent and child enough to validate? | No |
| Does the same key set have one valid shape? | No, shape depends on insertion order |
Exercise
A tree has root 10, a left child 5, and a right child 15. Node 5 has a right child 12. Is this a valid BST? Explain using the bounds from is_bst.
Show answer
No. When is_bst reaches node 12, it is in the right subtree of 5 and the left subtree of 10, so low=5 and high=10. The key 12 is not below 10, so the check fails. Every parent-child pair looks ordered, but 12 is larger than the root 10 and sits in its left subtree.
Exercise
Why does the rule have to apply to whole subtrees rather than only to a node's immediate children?
Show answer
Search skips an entire subtree after one comparison. That is only safe if every key in the skipped subtree is on the correct side of the current key. A children-only rule would allow a misplaced key deeper down, and search could miss it.