Trees Algorithms
Branching structures that grow in hierarchy. Learn the 4 topics below step by step with interactive visualizations.
Ways to visit every node of a tree exactly once. Depending on when you visit, it splits into preorder (root first), inorder (left → root → right, sorted order in a BST), postorder (root last), and level order.
O(n)A self-balancing binary search tree that keeps the height difference of every node's left and right subtrees at most 1. After an insert or delete it restores balance with rotations, guaranteeing O(log n) at all times.
O(log n)A tree for quickly computing range sums, minimums, and the like. Each node owns one interval, handling both point updates and range queries in O(log n) — covering the update weakness of a prefix-sum array.
Query/Update O(log n)A prefix tree that stores strings letter by letter along its branches. By sharing common prefixes, it handles autocomplete, dictionaries, and prefix search in O(m) where m is the string length.
O(m) (string length)