Sub topic 8
The Binary Search Tree Property
Assume you know recursion, object references, and the meaning of Big-O notation. A binary search tree (BST) is a binary tree that keeps keys in an order that makes searching possible.
The ordering rule
For every node in a BST:
- Every key in its left subtree is smaller than the node's key.
- Every key in its right subtree is larger than the node's key.
- The same rule must hold recursively for every descendant.
The rule applies to entire subtrees, not only to immediate children. In this tree, 12 is correctly placed because it is greater than 10 and smaller than 15.
10
/ \
5 15
/
12A common error is to check only that each left child is smaller and each right child is larger. This test is insufficient:
10
/ \
5 15
/
8The node 8 is smaller than 15, so it fits under 15. However, it is in the right subtree of 10, where every key must be greater than 10. Therefore, the tree is not a BST.
A useful interval model
During a recursive check, each node has an allowed interval. The root may contain any key. After visiting a node with key k:
- Its left child must be in the interval below
k. - Its right child must be in the interval above
k.
For distinct keys, use strict bounds. The following implementation passes bounds down the tree.
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)
)
root = Node(10, Node(5), Node(15, Node(12)))
print(is_bst(root)) # TrueThe low and high values describe exclusive limits. For example, the node 12 receives the interval (10, 15). This catches violations that are several levels below the root.
Duplicate keys
A BST definition must specify how it handles equal keys. Common policies are:
| Policy | Left subtree | Right subtree |
|---|---|---|
| Distinct keys | `< node.key` | `> node.key` |
| Duplicates go left | `<= node.key` | `> node.key` |
| Duplicates go right | `< node.key` | `>= node.key` |
The strict version is common in mathematical definitions and GATE questions unless the question states another policy. A validation algorithm and an insertion algorithm must use the same policy.
Why inorder order matters
An inorder traversal visits the left subtree, then the node, then the right subtree. Applying the BST rule repeatedly means the keys appear in increasing order. Thus, a BST with distinct keys has a strictly increasing inorder sequence.
The converse is also useful: if an inorder traversal of a binary tree is strictly increasing, the tree satisfies the BST ordering rule.
Exercise
Is this tree a BST under the distinct-key rule? Explain: root 20, left child 10, right child 30, and left child of 30 equal to 15.
Show answer
No. The node 15 lies in the right subtree of 20, so it must be greater than 20, but 15 is smaller than 20.