Splay Trees & Dynamic Optimality
Self-adjusting trees, the access lemma and a 40-year-old conjecture.
A splay tree keeps no balance information at all, and a single access can take linear time. Yet any accesses cost in total, and on skewed, local or sequential access patterns the tree adapts by itself, often beating every static tree. Whether it is within a constant factor of the best possible binary search tree on every access sequence has been an open question since 1985.
What you’ll learn
- Define splaying with zig, zig-zig and zig-zag steps, and explain why a zig-zig rotates the parent first.
- Prove the access lemma with the potential , and derive the balance, static-optimality and working-set theorems from it.
- State the dynamic optimality conjecture precisely, with Wilber's lower bounds and the best known competitive ratios.
- Decide when splay trees win in practice (skew, locality, sequential scans) and when they lose (worst-case latency, concurrent reads).
Splaying
A splay tree (Sleator & Tarjan, 1985) is a plain BST that restructures itself on every access: after reaching a node , it splays , rotating it all the way to the root. Searches splay the node found (or the last node reached when the key is absent), insertions splay the new node, and deletions splay the doomed node to the root before removing it. Splaying climbs two levels at a time, in one of three steps, where is 's parent and its grandparent:
- zig: is the root. Rotate over .
- zig-zig: and are both left children, or both right children. Rotate over first, then over .
- zig-zag: one is a left child and the other a right child. Rotate over , then over .
class Node:
def __init__(self, key, parent=None):
self.key, self.parent, self.left, self.right = key, parent, None, None
def rotate(x): # lift x above its parent; inorder is unchanged
p, g = x.parent, x.parent.parent
if x is p.left:
p.left, x.right = x.right, p
if p.left: p.left.parent = p
else:
p.right, x.left = x.left, p
if p.right: p.right.parent = p
p.parent, x.parent = x, g
if g:
if g.left is p: g.left = x
else: g.right = x
def splay(x): # rotate x up to the root, two levels at a time
while x.parent:
p, g = x.parent, x.parent.parent
if g is None:
rotate(x) # zig
elif (g.left is p) == (p.left is x):
rotate(p); rotate(x) # zig-zig: the parent first
else:
rotate(x); rotate(x) # zig-zag: x twice
return x # x is the new rootBottom-up splaying with parent pointers. Tested by random insertions and searches against a sorted list, checking the parent pointers and that the accessed key ends at the root.
The access lemma
A concavity fact. If and , then , so
Zig-zig, two rotations. Primes denote values after the step; only , and change rank, and . Using and :
After a zig-zig, 's old subtree and 's new subtree are disjoint parts of 's new subtree: . The concavity fact gives , so the step costs at most
Zig-zag, two rotations. Now 's and 's new subtrees are disjoint parts of 's new one, , so , and with :
Zig, one rotation, at most once and only at the end. With , it costs at most . Summing over the steps, the rank differences telescope from to , and only the zig adds a constant:
In a long access sequence, key makes up of all accesses. With weights equal to each key's share of the accesses, so that the total weight is 1, the access lemma bounds the amortized cost of splaying by . What is that bound in rotations?
Show the answer
10
and , so the bound is . A key accessed a fraction of the time costs amortized, which is the entropy bound behind static optimality, and the tree achieves it without knowing the frequencies.
What the lemma buys
The algorithm never changes; only the weights in the analysis do, and each choice of weights is a theorem. Over the whole sequence the potential can drop by at most , where is the total weight, so for accesses to keys:
| Theorem | Weights in the analysis | Total cost of accesses |
|---|---|---|
| Balance | ||
| Static optimality | , where key is accessed times | |
| Static finger (a fixed key ) | ||
| Working set | for the -th most recently accessed key, updated after each access | , = distinct keys accessed since the last access to |
| Sequential access (Tarjan, 1985) | A separate argument | to access all keys in order |
| Dynamic finger (Cole, 2000) | A separate, long proof |
All of these bounds hold at once, so a splay tree is fast on any sequence with some exploitable structure: a few hot keys, keys near a fixed position, sweeps, or small jumps. In a quick experiment with the code above on 2,000 keys and 50,000 accesses, uniformly random accesses cost about 12.9 rotations each (the bound is 33.9), accesses of which 90% hit 10 hot keys cost about 3.9, and a sequential pass about 2.5.
The dynamic optimality conjecture
Progress has come through lower bounds on . Wilber (1989) gave two. The interleave (alternation) bound fixes a static reference tree and counts, at each of its nodes, how often consecutive accesses below that node switch between its left and right sides; the funnel bound looks at which earlier keys are "visible" from each access. Lecomte and Weinstein (2020) proved that the funnel bound dominates the interleave bound on every sequence and can exceed it by a factor.
The interleave bound powers the best competitive ratios known as of 2025:
| Algorithm | Competitive ratio | Idea |
|---|---|---|
| Splay trees (1985) | , from the balance theorem | Splay every accessed node to the root |
| Tango trees (Demaine, Harmon, Iacono & Pătraşcu, 2004) | Store preferred paths of a reference tree in auxiliary red-black trees, and charge each change of preferred child to the interleave bound | |
| Multi-splay trees (Wang, Derryberry & Sleator, 2006) | , while also amortized per access | Preferred paths kept in splay trees |
| Greedy (Lucas, 1988; Munro, 2000) | Conjectured | At each access, touch only what the geometric view forces |
In the geometric view (Demaine, Harmon, Iacono, Kane & Pătraşcu, 2009), an execution is a set of points (key, time) that is arborally satisfied: any two points not on a common horizontal or vertical line span a rectangle containing a third point. is equivalent to the smallest arborally satisfied superset of the access points, and Greedy, an offline idea of Lucas and Munro, becomes an online algorithm that adds, time step by time step, only the points the current access forces.
Which statement is true?
- Splay trees are known to be -competitive
- Some online BST algorithms are known to be -competitive
- Computing for an access sequence is known to be NP-hard
- Every BST algorithm must spend time on some single access
Show the answer
B. Some online BST algorithms are known to be -competitive
Tango trees and multi-splay trees are -competitive. The complexity of computing exactly is open; NP-completeness is known for the geometric problem on general point sets, where a time step may hold several points.
Key takeaways
- Splaying moves every accessed node to the root with zig, zig-zig (parent first) and zig-zag steps. No balance information is stored, and a single access can cost .
- The access lemma, amortized cost at most with of the subtree weight, yields the balance, static-optimality, static-finger and working-set theorems by choosing weights in the analysis.
- Sequential access costs in total (Tarjan) and the dynamic-finger theorem holds (Cole); the deque conjecture is proved up to an factor (Pettie).
- Dynamic optimality, being within a constant factor of the best offline BST on every sequence, has been open since 1985. Tango and multi-splay trees are -competitive, and 2026 preprints claim the first ratios for splay trees and Greedy.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
In a zig-zig step, is the left child of , which is the left child of . Which rotations happen, in which order?
- Rotate over , then over
- Rotate over , then over
- Rotate over only
- Rotate directly over
Show the answer
A. Rotate over , then over
The parent goes first. Rotating twice is the zig-zag order; applied to a zig-zig configuration it is plain rotate-to-root, which can cost per access on a path.
What does the balance theorem (unit weights) guarantee for accesses in an -node splay tree?
- Every access takes time in the worst case
- time in total
- time in total
- time, but only for random access sequences
Show the answer
B. time in total
With , each splay costs at most amortized, and the potential, at most , can drop by at most that much over the whole sequence. Total: , for every sequence.
What does the static optimality theorem say?
- The splay tree converges to the shape of the optimal static tree
- On every long enough access sequence, splaying costs at most a constant factor more than the best static BST for that sequence
- Splay trees are within a constant factor of the best dynamic BST
- Every access costs in the worst case
Show the answer
B. On every long enough access sequence, splaying costs at most a constant factor more than the best static BST for that sequence
If key is accessed times out of , splaying costs (every key accessed at least once), and no static BST can beat that entropy bound by more than a constant factor. The tree never needs to know the frequencies.
What was known about the dynamic optimality conjecture as of 2025?
- Cole proved it in 2000
- It was open; some BSTs (Tango trees, multi-splay trees) were known to be -competitive, but no ratio was known for splay trees
- It had been disproved by a sequence on which splay trees are times slower than optimal
- It was proved for Greedy, but not for splay trees
Show the answer
B. It was open; some BSTs (Tango trees, multi-splay trees) were known to be -competitive, but no ratio was known for splay trees
Splay trees were only known to be -competitive, which is immediate from the balance theorem. Tango trees (2004) and multi-splay trees (2006) are -competitive, using Wilber's lower bound. In 2026, preprints claimed the first ratios for splay trees and for Greedy.
Further reading
- D. D. Sleator & R. E. Tarjan, "Self-adjusting binary search trees," Journal of the ACM 32(3) (1985), 652–686.
- R. Wilber, "Lower bounds for accessing binary search trees with rotations," SIAM Journal on Computing 18(1) (1989), 56–67.
- E. D. Demaine, D. Harmon, J. Iacono & M. Pătraşcu, "Dynamic optimality—almost," SIAM Journal on Computing 37(1) (2007), 240–251.
- E. D. Demaine, D. Harmon, J. Iacono, D. Kane & M. Pătraşcu, "The geometry of binary search trees," Proceedings of the 20th ACM-SIAM Symposium on Discrete Algorithms (SODA) (2009), 496–505.
- P. Chmel, B. Haeupler, R. Hladík, M. Koucký, A. Roeyskoe, V. Rozhoň, O. Sladký & R. E. Tarjan, "Splay trees are almost dynamically optimal," preprint, arXiv:2607.18498 (2026).