Short answer: Tarjan’s offline LCA answers a batch of lowest-common-ancestor queries during one depth-first search, using union-find. When the search finishes a node, the node is united with its parent and the parent’s set is labeled with the parent. A query between the finished node and a node already…
Category: Trees
How binary lifting computes a lowest common ancestor
Short answer: Binary lifting precomputes the 2^k-th ancestor of every node in a rooted tree. The table up[k][v] is the parent of up[k−1][v] taken 2^{k−1} more steps, and it fills in O(n log n). The lowest common ancestor of two nodes is found by lifting the deeper node up…
How an AVL tree restores height balance
Short answer: An AVL tree is a binary search tree whose balance factor, left height minus right height, stays in −1, 0, +1 at every node. An insertion or deletion that pushes a factor to ±2 is repaired by a single or a double rotation. The height is at…
How a van Emde Boas tree searches in log log universe
Short answer: A van Emde Boas tree stores a set of integers from a universe 0, 1, …, u−1 and supports insert, delete, member, successor, and predecessor in O(log log u) time. It splits the universe into √u clusters of √u values, keeps a summary structure on which clusters…
How a trie stores keys on a path from the root
Short answer: A trie stores a set of strings so that each edge is a character and each root-to-node path is a prefix. Lookup of a string of length L examines O(L) edges, independent of how many other keys exist. A node that ends a key is marked terminal.…
How a treap uses random priorities
Short answer: A treap is a binary search tree on keys and a heap on independently random priorities. Each node stores a key and a priority. The in-order walk is the key order, and every parent has a better priority than its children. Insert and delete are rotations that…
How a splay tree moves the accessed node to the root
Short answer: A splay tree is a binary search tree that splays the accessed node to the root by zig-zag and zig-zig rotations. It stores no balance bit and no priority. A single operation can take O(n) time, and a sequence of m operations on a tree of size…
How a sparse table answers a static range minimum
Short answer: A sparse table answers a static range minimum, or any other idempotent range query, in O(1) time after O(n log n) preprocessing. Cell (k, i) stores the minimum of the 2^k elements that start at i. A query of length L uses the two overlapping blocks of…
How a skip list stacks random linked lists
Short answer: A skip list is an ordered dictionary built from linked lists stacked in levels. Every key appears in the bottom list. A key is copied into the next level up with probability 1/2, independently, so the expected number of levels is O(log n) and the expected search…
How a segment tree answers a range query
Short answer: A segment tree partitions an array into segments of length a power of two, stored in a heap-indexed array of about 2n cells. A range query visits O(log n) disjoint nodes whose segments cover the query exactly. A point update walks one root path. A range update…
How a red-black tree fixes insert with colors
Short answer: A red-black tree is a binary search tree with red and black nodes. The root is black, every red node has two black children, and every path from a node to a leaf sentinel carries the same number of black nodes. Those rules keep the height at…
How a Fenwick tree sums a prefix
Short answer: A Fenwick tree, or binary indexed tree, stores partial sums so that the prefix sum through index i, and a point update at i, both run in O(log n). The index trick is the least significant set bit: i & −i in two’s complement. That bit names…
How a B+ tree stores keys in a linked leaf level
Short answer: A B+ tree keeps every record in the leaves and copies separator keys into internal nodes that act only as an index. Leaves are linked in key order, so a range scan walks that chain and never returns to the index. Occupancy rules match a B-tree, and…
How a B-tree keeps every leaf at one depth
Short answer: A B-tree of minimum degree t stores between t−1 and 2t−1 keys in every node except the root, and every leaf sits at the same depth. Search follows one child pointer per node, so a tree of n keys has height O(log_t n). Insert splits a full…
