Conceptual
Login

Finding the Minimum and Maximum Element in a Binary Search Tree in C++

The binary search tree ordering invariant — every key in a node's left subtree is lesser and every key in its right subtree is greater — implies that the minimum key of a tree is the leftmost node, reached by following left links until none remains, and symmetrically the maximum key is the rightmost node. Both extrema can therefore be located by a single root-to-leaf descent, expressible either iteratively with a moving pointer or recursively by reducing the problem to finding the extremum of the appropriate subtree, with the base case being an absent child in the chosen direction. This belongs to data structures and algorithms, and its cost is proportional to tree height rather than to node count, which distinguishes ordered search trees from unordered collections where finding an extremum requires inspecting every element.