Sub topic 13
Cost of BST Operations and the Role of Height
Assume you know recursion, object references, linked lists, and Big-O notation. Also assume the binary search tree property: keys smaller than a node are in its left subtree, and larger keys are in its right subtree.
Why height controls the cost
Let the height h of a tree be the number of edges on its longest root-to-leaf path. A tree with one node has height 0. An empty tree has height -1 under this definition.
A search, insertion, or deletion follows one path from the root. At each node, it compares the target key with the current key and chooses at most one child. Therefore, it does not inspect every node in the tree. Its running time depends on the path length, which is bounded by h.
The usual cost for these operations is:
\[ O(h) \]
The number of comparisons is at most h + 1, so writing O(h) ignores only a constant difference.
| Tree shape | Height for n nodes | Typical operation cost |
|---|---|---|
| Perfect or well balanced | Θ(log n) | Θ(log n) |
| Completely skewed | n - 1 | Θ(n) |
| One-node tree | 0 | Θ(1) |
Best, average, and worst cases
For search, the best case is Θ(1): the target is at the root. The worst case is Θ(h), when the target is at the deepest relevant node or is absent after reaching a leaf.
Insertion also follows one root-to-leaf path. If the new key belongs immediately below the root, the cost is Θ(1). In the worst case, insertion reaches depth h, so it costs Θ(h).
Deletion first searches for the key. If the node has two children, it then finds its inorder successor, which is the smallest node in the right subtree. Both paths together are still bounded by a constant multiple of h. Thus deletion is Θ(h).
“Average case” needs an assumption about the tree. If distinct keys are inserted in a reasonably random order, the expected height is Θ(log n), and the expected cost of the operations is Θ(log n). If keys arrive in sorted order, the tree becomes skewed and the cost becomes Θ(n). Average-case analysis is not automatically logarithmic.
Seeing the two shapes
A balanced tree splits its keys across both sides at many levels. A skewed tree has almost one child per node, so it behaves like a linked list.
Measuring the cost in code
The following function counts comparisons during a search. It returns both the matching node and the number of comparisons.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def search_with_count(root, key):
comparisons = 0
node = root
while node is not None:
comparisons += 1
if key == node.key:
return node, comparisons
node = node.left if key < node.key else node.right
return None, comparisons
root = Node(8)
root.left = Node(4)
root.right = Node(12)
root.left.left = Node(2)
root.left.right = Node(6)
found, comparisons = search_with_count(root, 6)
print(found.key if found else None)
print(comparisons)The function performs one loop iteration per visited node. If the deepest visited node has depth h, the comparison count is at most h + 1.
Exercise
A BST has height 5. What is the asymptotic worst-case cost of search, insertion, and deletion, and what is the maximum number of node comparisons during a search?
Show answer
Each operation costs O(h), which is O(5) and therefore constant for this fixed tree. A search makes at most h + 1 = 6 comparisons.