Trees & Binary Search
A tree is nodes and pointers arranged as a hierarchy: one root, branching children, no cycles. File systems, the DOM, JSON, org charts, database indexes — hierarchies are everywhere, and trees are how programs hold them.
Tree vocabulary in one breath: the root is the top node; children hang off parents; leaves have no children; height is the longest root-to-leaf path. A binary tree limits each node to two children, left and right. Since each subtree is itself a tree, tree code is naturally recursive — the base case is the empty tree (None).
The star of the family is the binary search tree (BST): every node's left subtree holds smaller values, right subtree holds bigger ones. That single rule means search never explores both sides — compare, go left or right, half the tree eliminated per step. On a balanced tree that's O(log n): a million items found in ~20 comparisons. Databases index columns with tree variants (B-trees) for exactly this reason — it's why indexed queries are fast (see the SQL track).
Same halving idea works on a plain sorted array — binary search, no tree needed. Check the middle; too small, discard the left half; too big, discard the right. O(log n) with three lines of state. It's the most implemented-slightly-wrong algorithm in history (off-by-one errors), so learn this canonical form.
BSTs are only O(log n) while balanced. Insert already-sorted data into a naive BST and it degenerates into a linked list — O(n) again. Real implementations (red-black trees, AVL, B-trees) rebalance automatically; that's what your language's sorted containers and every database index use.