7.7Splay Trees
Covers IGNOU MCS-208 §7.5, 7.5.1, 7.5.2
AVL and B-trees keep the tree balanced at all times. A splay tree takes a different approach: it does not keep any balance information, but every time a node is accessed, it is moved to the root. Frequently used keys therefore stay near the top, and over any long sequence of operations the average cost is still logarithmic.
The idea
Splaying steps
Let be the node being splayed, its parent and its grandparent. Splaying repeats one of three steps until is the root:
| Step | Situation | Rotations |
|---|---|---|
| Zig | is the root | One rotation at (the last step only) |
| Zig-zig | and are both left children (or both right children): a straight line | Rotate at first (moving up), then at (moving up) |
| Zig-zag | is a right child and a left child, or vice versa: a bend | Rotate above , then above (as in an AVL double rotation) |
In the study material’s words, “zig” means one step down to the left and “zag” one step down to the right; zig-zag and zag-zig are the two mirror-image bends.
The zig-zig step is what makes splay trees work: rotating the grandparent first roughly halves the depth of every node on the access path, while rotating up one level at a time would not.
Top-down splaying
The study material describes top-down splaying, which does the same work during the search instead of after it. Three trees are kept: the central tree (initially the whole tree), a left tree (keys smaller than the target) and a right tree (keys larger). Going down, each zig, zig-zig or zig-zag moves the passed nodes into the left or right tree; at the end the target is made the root, with the left tree as its left subtree and the right tree as its right subtree. It needs no parent pointers and no second pass.
Insertion and deletion
- Insert: splay the key; if it is not present, the new key becomes the root, with the splayed tree split on either side.
- Delete: splay the key to the root, remove the root, then splay the largest key of the left subtree to its top (it then has no right child) and attach the right subtree there.
Amortized analysis
A single splay-tree operation can take time (accessing the bottom of a chain), but any sequence of operations on a tree with at most nodes takes time, so the amortized cost is . Three standard methods prove such bounds:
| Method | Idea |
|---|---|
| Aggregate | Bound the total cost of operations; amortized cost , the same for every operation |
| Accounting | Overcharge cheap operations and store the extra as credit that pays for expensive ones later |
| Potential | Define a “potential energy” of the data structure; amortized cost = actual cost + change in potential |
Sleator and Tarjan proved the amortized bound for splay trees with the potential method.
Key takeaways
- Splay tree: BST + splaying every accessed node to the root; no balance fields.
- Zig (parent is root), zig-zig (straight line: rotate grandparent first), zig-zag (bend).
- Top-down splaying uses left, central and right trees during the search.
- Amortized per operation; a single operation may be .
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Explain zig, zig-zig and zig-zag with diagrams and state when each is used.
Show worked answer
Let be the node being splayed, its parent and its grandparent. Zig: used when is the root; one rotation at makes the root (e.g. ). Zig-zig: used when and are both left (or both right) children; first rotate at so that moves up, then rotate at so that moves up: . Zig-zag: used when is a right child and a left child (or the reverse); rotate above , then above : . Steps repeat until is the root.