Trees
Nievergelt & Hinrichs · Ch. 5 · Algorithms and Data Structures
A binary search tree keeps keys ordered: left child < parent < right child. Unbalanced, it degrades to a linked list. AVL trees restore balance after every insertion. B-trees generalize to disk-friendly multi-way branching.
Binary search tree
Insert, search, and delete all follow the same path from root to leaf. Average depth O(log n) for random insertions. Worst case O(n) for sorted input.
Scheme
Tree traversals
Inorder (left, root, right) gives sorted output. Preorder (root, left, right) gives a serialization that can reconstruct the tree. Postorder (left, right, root) evaluates expressions bottom-up.
Scheme
Neighbors
Cross-references
- 🐱 Milewski Ch.19 — trees as initial algebras: fold over a tree is a catamorphism