Sub topic 9
Searching a Binary Search Tree
Assume you know recursion, object references, and Big-O notation. A binary search tree (BST) stores keys so that, for every node:
- Every key in the left subtree is smaller than the node's key.
- Every key in the right subtree is larger than the node's key.
- This chapter assumes distinct keys. A BST that allows duplicates must define an additional rule for equal keys.
Following one search path
To search for a key, compare it with the current node:
- If the node is empty, the key is absent.
- If the node's key equals the target, return the node.
- If the target is smaller, continue in the left subtree.
- If the target is larger, continue in the right subtree.
Each comparison eliminates the opposite subtree. Therefore, a search visits only one path from the root toward a leaf.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def search(root, target):
node = root
while node is not None:
if target == node.key:
return node
if target < node.key:
node = node.left
else:
node = node.right
return None
root = Node(8)
root.left = Node(3)
root.right = Node(10)
root.left.left = Node(1)
root.left.right = Node(6)
result = search(root, 6)
print(result.key if result else "not found")The loop stops when it finds the target or reaches an empty child reference. The iterative version uses constant auxiliary space, O(1), because it stores only the current node.
A recursive version is also direct:
def recursive_search(node, target):
if node is None or node.key == target:
return node
if target < node.key:
return recursive_search(node.left, target)
return recursive_search(node.right, target)The recursive version uses O(h) call-stack space in the worst case, where h is the tree height.
Minimum and maximum keys
The minimum key is found by repeatedly following left children. The maximum key is found by repeatedly following right children.
def minimum(root):
if root is None:
return None
node = root
while node.left is not None:
node = node.left
return node
def maximum(root):
if root is None:
return None
node = root
while node.right is not None:
node = node.right
return nodeIf a tree is empty, these functions return None. Otherwise, the returned node has no child in the direction being followed. A minimum node cannot have a left child, because that child would contain a smaller key. It may have a right child. The corresponding statement holds for a maximum node.
Complexity and common errors
Let h be the height measured as the number of edges on the longest root-to-leaf path. Search, minimum, and maximum each take O(h) time.
- In a perfectly balanced tree with n nodes, h is O(log n), so operations take O(log n).
- In the best case, the target is at the root, so search takes O(1).
- In a skewed tree, h is n - 1, so the worst-case cost is O(n).
Do not claim that every BST operation is O(log n). That bound requires a balanced shape. Inserting sorted keys such as 1, 2, 3, 4, and 5 can create a chain.
Exercise
In a BST, which direction should a search for 17 take at a node containing 23, and why?
Show answer
It should take the left child because 17 is smaller than 23. Every key in the right subtree is at least 23 under the distinct-key assumption.