Splay trees
A search tree with one habit: whatever you look up is rotated to the top. Three small moves (zig, zig-zig, zig-zag) keep it fast without any bookkeeping.
In 30 seconds
A splay tree is a binary search tree that reshapes itself every time it is used. After you look up or insert a key, that node is moved to the root by a series of rotations called splaying.
The moves come in three kinds, depending on where the node sits below its parent and grandparent: zig, zig-zig and zig-zag.
One lookup can be slow, but slow lookups improve the tree, so the average over many is O(log n). Keys used often stay near the top, which suits repeated access.
Splay trees, step by step
Only left-handed cases are drawn; each has a mirror image. The triangles A to D stand for whole subtrees, which may be empty. Lookup costs count nodes visited, not the rotations.
- Step 1: A tree that rearranges itself. A binary search tree keeps smaller keys to the left and larger to the right, so finding 30 means walking down from the root: four nodes here. A splay tree adds one rule: after every access, that node is rotated all the way up to become the new root.
- Step 2: Zig: one rotation. A rotation lifts a node above its parent without breaking the left-smaller, right-larger order: the parent drops to the other side, and the subtree between them (B) changes hands. When x is a child of the root, this single rotation, called zig, finishes the job.
- Step 3: Zig-zig: a straight line. If x and its parent are both left children (or both right children), the three nodes form a straight line. Rotate the parent above the grandparent first, then x above the parent. Doing the upper rotation first is the detail that makes splay trees work: it roughly halves long chains.
- Step 4: Zig-zag: a bend. If x is a left child and its parent a right child (or the other way round), the path bends. Then x is rotated twice: first above its parent, then above its grandparent. It ends up on top with the other two as its children, and the bend is straightened into a balanced shape.
- Step 5: Splay 30 to the root. Back to the example. 30 is a left child and its parent 40 a right child: a bend, so zig-zag lifts 30 above 40 and 20. Now 30 is a child of the root 50, so one zig finishes. Splaying always works in pairs of levels like this, with a single zig at the end if needed.
- Step 6: Repeated lookups get cheap. Now ask for 30, 30, 40, 30, 40. In a tree that never changes, each lookup walks the same long path. In the splay tree the first lookup pays full price, then recently used keys stay near the top. Real workloads often lean on a few hot keys, which is where splay trees shine; rarely used keys sink lower.
- Step 7: Amortised O(log n). A splay tree can be badly shaped: here seven keys form a chain, and finding 1 visits all seven. But the splay that follows (three zig-zigs) leaves the tree shallower, and on long chains roughly halves the depth. Slow lookups repair the tree, so over any long run the average cost per operation is O(log n): an amortised bound.
Try it yourself
Learn this first
Spotted a mistake on this page?