Simple Algorithm
Algorithms & Data Structures · Optimal · 28 min

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 mm accesses cost O((m+n)log⁡n)O((m+n)\log n) 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 ∑xlog⁡2s(x)\sum_x\log_2 s(x), 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 xx, it splays xx, 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 pp is xx's parent and gg its grandparent:

  • zig: pp is the root. Rotate xx over pp.
  • zig-zig: xx and pp are both left children, or both right children. Rotate pp over gg first, then xx over pp.
  • zig-zag: one is a left child and the other a right child. Rotate xx over pp, then xx over gg.
python
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 root

Bottom-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

DerivationProof: one inequality per step
  1. log⁡2a+log⁡2b ≤ 2log⁡2c−2\log_2 a+\log_2 b\ \le\ 2\log_2 c-2

    A concavity fact. If a,b>0a,b>0 and a+b≤ca+b\le c, then ab≤((a+b)/2)2≤c2/4ab\le\big((a+b)/2\big)^2\le c^2/4, so

  2. 2+ΔΦ = 2+r′(p)+r′(g)−r(x)−r(p) ≤ 2+r′(x)+r′(g)−2r(x)2+\Delta\Phi\ =\ 2+r'(p)+r'(g)-r(x)-r(p)\ \le\ 2+r'(x)+r'(g)-2r(x)

    Zig-zig, two rotations. Primes denote values after the step; only xx, pp and gg change rank, and r′(x)=r(g)r'(x)=r(g). Using r′(p)≤r′(x)r'(p)\le r'(x) and r(p)≥r(x)r(p)\ge r(x):

  3. 2+r′(x)+(2r′(x)−r(x)−2)−2r(x) = 3(r′(x)−r(x))2+r'(x)+\big(2r'(x)-r(x)-2\big)-2r(x)\ =\ 3\big(r'(x)-r(x)\big)

    After a zig-zig, xx's old subtree and gg's new subtree are disjoint parts of xx's new subtree: s(x)+s′(g)≤s′(x)s(x)+s'(g)\le s'(x). The concavity fact gives r′(g)≤2r′(x)−r(x)−2r'(g)\le2r'(x)-r(x)-2, so the step costs at most

  4. 2+r′(p)+r′(g)−r(x)−r(p) ≤ 2+(2r′(x)−2)−2r(x) = 2(r′(x)−r(x))2+r'(p)+r'(g)-r(x)-r(p)\ \le\ 2+\big(2r'(x)-2\big)-2r(x)\ =\ 2\big(r'(x)-r(x)\big)

    Zig-zag, two rotations. Now pp's and gg's new subtrees are disjoint parts of xx's new one, s′(p)+s′(g)≤s′(x)s'(p)+s'(g)\le s'(x), so r′(p)+r′(g)≤2r′(x)−2r'(p)+r'(g)\le2r'(x)-2, and with r(p)≥r(x)r(p)\ge r(x):

  5. amortized cost of the splay ≤ 3(r(t)−r(x))+1\text{amortized cost of the splay}\ \le\ 3\big(r(t)-r(x)\big)+1

    Zig, one rotation, at most once and only at the end. With r′(p)≤r(p)r'(p)\le r(p), it costs at most 1+(r′(x)−r(x))1+\big(r'(x)-r(x)\big). Summing over the steps, the rank differences telescope from r(x)r(x) to r(t)r(t), and only the zig adds a constant:

Predict before you look

In a long access sequence, key xx makes up 1/81/8 of all accesses. With weights w(i)w(i) 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 xx by 3log⁡2(s(t)/s(x))+13\log_2\big(s(t)/s(x)\big)+1. What is that bound in rotations?

Show the answer

10

s(t)=1s(t)=1 and s(x)≥w(x)=1/8s(x)\ge w(x)=1/8, so the bound is 3log⁡28+1=103\log_2 8+1=10. A key accessed a fraction qq of the time costs O(log⁡(1/q))O(\log(1/q)) 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 ∑ilog⁡2(W/w(i))\sum_i\log_2\big(W/w(i)\big), where WW is the total weight, so for mm accesses i1,…,imi_1,\dots,i_m to nn keys:

TheoremWeights in the analysisTotal cost of mm accesses
Balancew(i)=1w(i)=1O((m+n)log⁡n)O\big((m+n)\log n\big)
Static optimalityw(i)=qi/mw(i)=q_i/m, where key ii is accessed qi≥1q_i\ge1 timesO(m+∑iqilog⁡(m/qi))O\big(m+\sum_i q_i\log(m/q_i)\big)
Static finger (a fixed key ff)w(i)=1/(∣i−f∣+1)2w(i)=1/(\abs{i-f}+1)^2O(m+nlog⁡n+∑jlog⁡(∣ij−f∣+1))O\big(m+n\log n+\sum_j\log(\abs{i_j-f}+1)\big)
Working set1/k21/k^2 for the kk-th most recently accessed key, updated after each accessO(m+nlog⁡n+∑jlog⁡(tj+1))O\big(m+n\log n+\sum_j\log(t_j+1)\big), tjt_j = distinct keys accessed since the last access to iji_j
Sequential access (Tarjan, 1985)A separate argumentO(n)O(n) to access all keys in order
Dynamic finger (Cole, 2000)A separate, long proofO(m+n+∑jlog⁡(∣ij−ij−1∣+1))O\big(m+n+\sum_j\log(\abs{i_j-i_{j-1}}+1)\big)
The first four follow from the access lemma (Sleator & Tarjan, 1985). Static optimality matches, up to a constant factor, the cost of the best static BST built with the frequencies known in advance.

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 3log⁡2n+13\log_2 n+1 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

splay(X) = O(OPT(X)+n)for every access sequence X\mathrm{splay}(X)\ =\ O\big(\mathrm{OPT}(X)+n\big)\qquad\text{for every access sequence } X
The dynamic optimality conjecture (Sleator & Tarjan, 1985): splay trees are O(1)O(1)-competitive with the best offline BST algorithm. It is open.

Progress has come through lower bounds on OPT\mathrm{OPT}. 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 Θ(log⁡log⁡n)\Theta(\log\log n) factor.

The interleave bound powers the best competitive ratios known as of 2025:

AlgorithmCompetitive ratioIdea
Splay trees (1985)O(log⁡n)O(\log n), from the balance theoremSplay every accessed node to the root
Tango trees (Demaine, Harmon, Iacono & Pătraşcu, 2004)O(log⁡log⁡n)O(\log\log n)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)O(log⁡log⁡n)O(\log\log n), while also O(log⁡n)O(\log n) amortized per accessPreferred paths kept in splay trees
Greedy (Lucas, 1988; Munro, 2000)Conjectured O(1)O(1)At each access, touch only what the geometric view forces
Online BST algorithms and what was known about them as of 2025.

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. OPT(X)\mathrm{OPT}(X) 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.

Check your understanding

Which statement is true?

  1. Splay trees are known to be O(1)O(1)-competitive
  2. Some online BST algorithms are known to be O(log⁡log⁡n)O(\log\log n)-competitive
  3. Computing OPT(X)\mathrm{OPT}(X) for an access sequence is known to be NP-hard
  4. Every BST algorithm must spend Θ(n)\Theta(n) time on some single access
Show the answer

B. Some online BST algorithms are known to be O(log⁡log⁡n)O(\log\log n)-competitive

Tango trees and multi-splay trees are O(log⁡log⁡n)O(\log\log n)-competitive. The complexity of computing OPT(X)\mathrm{OPT}(X) 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 Θ(n)\Theta(n).
  • The access lemma, amortized cost at most 3(r(t)−r(x))+13\big(r(t)-r(x)\big)+1 with r=log⁡2r=\log_2 of the subtree weight, yields the balance, static-optimality, static-finger and working-set theorems by choosing weights in the analysis.
  • Sequential access costs O(n)O(n) in total (Tarjan) and the dynamic-finger theorem holds (Cole); the deque conjecture is proved up to an α∗(n)\alpha^*(n) 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 O(log⁡log⁡n)O(\log\log n)-competitive, and 2026 preprints claim the first o(log⁡n)o(\log n) 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.

Question 1

In a zig-zig step, xx is the left child of pp, which is the left child of gg. Which rotations happen, in which order?

  1. Rotate pp over gg, then xx over pp
  2. Rotate xx over pp, then xx over gg
  3. Rotate pp over gg only
  4. Rotate xx directly over gg
Show the answer

A. Rotate pp over gg, then xx over pp

The parent goes first. Rotating xx twice is the zig-zag order; applied to a zig-zig configuration it is plain rotate-to-root, which can cost Θ(n)\Theta(n) per access on a path.

Question 2

What does the balance theorem (unit weights) guarantee for mm accesses in an nn-node splay tree?

  1. Every access takes O(log⁡n)O(\log n) time in the worst case
  2. O((m+n)log⁡n)O((m+n)\log n) time in total
  3. O(m+n)O(m+n) time in total
  4. O(mlog⁡n)O(m\log n) time, but only for random access sequences
Show the answer

B. O((m+n)log⁡n)O((m+n)\log n) time in total

With w≡1w\equiv1, each splay costs at most 3log⁡2n+13\log_2 n+1 amortized, and the potential, at most nlog⁡2nn\log_2 n, can drop by at most that much over the whole sequence. Total: m(3log⁡2n+1)+nlog⁡2nm(3\log_2 n+1)+n\log_2 n, for every sequence.

Question 3

What does the static optimality theorem say?

  1. The splay tree converges to the shape of the optimal static tree
  2. On every long enough access sequence, splaying costs at most a constant factor more than the best static BST for that sequence
  3. Splay trees are within a constant factor of the best dynamic BST
  4. Every access costs O(log⁡n)O(\log n) 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 ii is accessed qiq_i times out of mm, splaying costs O(m+∑iqilog⁡(m/qi))O\big(m+\sum_i q_i\log(m/q_i)\big) (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.

Question 4

What was known about the dynamic optimality conjecture as of 2025?

  1. Cole proved it in 2000
  2. It was open; some BSTs (Tango trees, multi-splay trees) were known to be O(log⁡log⁡n)O(\log\log n)-competitive, but no o(log⁡n)o(\log n) ratio was known for splay trees
  3. It had been disproved by a sequence on which splay trees are Θ(log⁡n)\Theta(\log n) times slower than optimal
  4. 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 O(log⁡log⁡n)O(\log\log n)-competitive, but no o(log⁡n)o(\log n) ratio was known for splay trees

Splay trees were only known to be O(log⁡n)O(\log n)-competitive, which is immediate from the balance theorem. Tango trees (2004) and multi-splay trees (2006) are O(log⁡log⁡n)O(\log\log n)-competitive, using Wilber's lower bound. In 2026, preprints claimed the first o(log⁡n)o(\log n) 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).