A hash table can find an exact key quickly, but it cannot easily answer “which stored key comes next?” A binary search tree keeps keys in an order that supports both lookup and ordered questions. At each node, keys in the left subtree are smaller and keys in the right subtree are larger, under a stated rule for duplicates.
Background for this page. A subtree contains a node and all its descendants. The search-tree ordering rule applies to every key in each subtree, not just immediate children. Related explanations: Arrays, linked lists, stacks, and queues · Red-black trees: balance, colors, and rotations.
Suppose the root is 8. Its left child is 3, and its right child is 12. To find 10, compare it with 8 and go right. Compare it with 12 and go left. The path ends at 10 if that node exists, or at an empty child if it does not. At each step, the ordering rule lets us discard an entire subtree.
The cost is tied to height, the number of links on the longest root-to-leaf path. Search, insertion, and deletion take O(h) time for a tree of height h. A well-shaped tree has h = O(log n). A tree formed by inserting sorted keys without balancing can become a chain with h = Θ(n). The same data structure can therefore behave very differently depending on its shape.
# Walk down until a key matches or a child is empty.
while current node exists:
if key equals current.key: return current.value
if key is smaller: move to the left child
otherwise: move to the right child
return not found
To insert, follow the same search path and attach a new node at the empty position. To visit keys in sorted order, use an inorder traversal: visit the left subtree, then the node, then the right subtree. That works because every left key comes before the node and every right key comes after it.
Deletion has three cases. A leaf can be removed. A node with one child can be replaced by that child. A node with two children can be replaced by its successor, the smallest key in its right subtree, while preserving the ordering rule. The details are easy to get wrong because parent and child links must all agree afterward.
The duplicate-key rule must be explicit. One design stores a count or list of values at one key. Another puts equal keys consistently on one side. Whatever rule is chosen, search, insertion, and traversal must all use the same one.
Delete a node with two children
In the illustrated tree, delete root 8 by replacing its key with 10, the smallest key in its right subtree, then removing the original 10 node. Every left key remains smaller, and every remaining right key is larger in this distinct-key example. Choosing an arbitrary replacement such as 14 could leave smaller values on its right. A leaf can be removed directly; a node with one child can be replaced by that child. Each operation follows paths whose cost depends on tree height.
Practice. Insert 8, 3, 12, 10, and 14 into an empty unbalanced binary search tree. What path does a search for 10 take? What order does an inorder traversal produce?
Answer. Search visits 8, 12, then 10. Inorder traversal produces 3, 8, 10, 12, 14.
Try another case. Can 10 occur anywhere below the left child of root 8?
Reasoning. No, under this distinct-key rule. Every key in the entire left subtree must be less than 8.