What does a splay operation cost, and what does "$O(\log n)$ on average" mean for a splay tree?
Splaying a node at depth $h$ costs $O(h)$: about one rotation per level, each $O(1)$. A single operation can therefore cost $O(n)$ when the tree is a chain, but over any sequence of operations the average is $O(\log n)$ per operation (amortised), and frequently used nodes are faster still because they stay near the root.
| Cost | |
|---|---|
| one rotation | $O(1)$ |
| splaying a node at depth $h$ | $O(h)$, about $h$ rotations |
| one operation, worst case | $O(n)$: the tree can be a chain |
| any sequence of $m$ operations | $O(m \log n)$ in total, so $O(\log n)$ each, amortised |
"Average" here is not about random inputs. It is an amortised guarantee: whatever the sequence of operations, even an adversarial one, the total is $O(m \log n)$. A few operations can be expensive, but each expensive one reshapes the tree so that the following ones are cheap.
Compared with an AVL tree, the guarantee is weaker for single operations and stronger for skewed access patterns. If 90 % of the lookups hit a handful of keys, those keys sit at or near the root, and their lookups cost far less than $\log n$.
Go deeper:
Amortized analysis — what an amortised bound promises, with the splay tree as one of the standard examples.