Ordered binary tree operations: insert, search, validate, k-th smallest, and lowest common ancestor.
5 algorithms3 problems
5 algorithms. Each one builds on the last.
A binary search tree keeps smaller values to the left of each node and larger ones to the right, at every level. That one rule lets a lookup, an insert or a rank question follow a single path instead of visiting the whole tree. Search trees sit behind ordered maps and sets such as Java's TreeMap and C++'s std::set, and they are a favourite in interviews.
At each node, one comparison tells you which side can still hold what you want, and the other side is dropped for good. So the cost follows the height H of the tree, not its size: about log N steps when it is balanced, and N when sorted inserts have turned it into a chain. Validation is the one page that checks the rule; the rest trust it.
Search, insert and LCA each follow one path down, so they are fast exactly when the tree is short. Validation visits every node of a valid tree, and k-th smallest visits k nodes after the left edge. If your values might arrive sorted, the plain tree on these pages becomes a list, and a self-balancing tree such as AVL or red-black is the fix.