Splay tree analysis [00U3]
- November 25, 2026
Theorem Amortized time for splay [00U4]
- November 25, 2026
The \mathsf {Splay} operation takes amortized \Theta (\lg n) time.
Remark Splay bounds other BST operations [00U5]
- November 25, 2026
We note first that this implies all the other methods (in particular \mathsf {Insert}, \mathsf {Lookup}, \mathsf {Delete}) will take amortized \Theta (\lg n) time as well. For \mathsf {Insert} and \mathsf {Lookup}, this is because they have exactly the same cost as splaying: when we lookup or insert, we travel down the tree, paying a cost proportional to the depth of the node we find or insert; then splaying that node to the root has exactly the same cost, since we travel back up exactly the same path, doing one rotation per level of depth. For \mathsf {Delete}, this is simply because it works by doing two \mathsf {Splay} operations.
Splay runtime proof [00U6]
- November 25, 2026
To prove that \mathsf {Splay} takes \Theta (\lg n) amortized time, we will use the potential method.
Definition Subtree size [00U7]
- November 25, 2026
If r is some node in a binary search tree, let S(r) be the size of the subtree rooted at r, that is, the number of nodes which have r as their ancestor (including r itself).
Definition Rank [00U8]
- November 25, 2026
The rank of a node r is defined by R(r) = \log _2(S(r)).
Definition Potential function = rank sum [00U9]
- November 25, 2026
Finally, let \Phi (T) be the sum of the ranks of all the nodes in the tree, that is, \Phi = \sum _{r \in T} R(r).
Rank sum examples [00UA]
- November 25, 2026
Note this is kind of weird and definitely not an integer! So thinking of this in terms of money would probably not work very well. For example, consider this tree:
Its three leaves all have size 1 and hence rank 0 (since \log _2 1 = 0); the internal node has size 3, and the root has size 5, so \Phi for this tree is \log _2 3 + \log _2 5. On the other hand, this tree: has a \Phi value of \log _2 5 + \log _2 4 + \log _2 3 + \log _2 2 + \log _2 1 = 3 + \log _2 5 + \log _2 3. In general, \Phi will be higher for more unbalanced trees.Remark Rank sum is a valid potential function [00UB]
- November 25, 2026
We note that \Phi is a valid potential function: \Phi for an empty tree is 0, and by definition it is clearly always positive. So the amortized time for an operation, that is, the sum of the actual cost and the change in potential, a = c + \Delta \Phi , will be a valid upper bound on the average cost of a single operation.
Before starting in on the proof proper, we will need a couple observations and lemmas about properties of the \lg (log base 2) function.
Observation \lg is increasing [00UC]
- November 25, 2026
\lg is an increasing function. So, if S(x) > S(y) then R(x) > R(y). In particular this means that R(x) > R(y) whenever x is an ancestor of y.
Lemma Maximum of \lg x + \lg (1 - x) [00UD]
- November 25, 2026
On the interval 0 < x < 1, the function \lg x + \lg (1-x) attains a maximum value of -2 (namely, when x = 1/2).
Proof.
- November 25, 2026
Remark [00UE]
- November 25, 2026
To get a more intuitive idea of why this is true, recall what the graph of \lg x looks like on the interval 0 < x < 1: it lies below the x-axis, passing through the points (1/2, -1) and (1,0), with a vertical asymptote as it diverges to -\infty as x \to 0. We are adding up the \lg of two values which are symmetric about the point x = 1/2. If we pick x = 1-x = 1/2 then the sum is -2. As we move the two points farther away from each other, the one moving left moves much faster in the negative y direction than the one moving to the right moves in the positive y direction, so the sum gets more negative.
Notation for node ranks [00UF]
- November 25, 2026
We want to talk about the rank of nodes before and after applying the splay rules. In general, for a node x we will use R(x) to refer to the rank of x before applying a rule, and R'(x) to refer to its new rank after applying the rule.
Important note: to understand the following proofs you really have to be looking at the pictures for the different splay rules!
Lemma Amortized cost of ZIG [00UG]
- November 25, 2026
The amortized cost of a ZIG operation is \leq 3(R'(x) - R(x)) + 1.
The amortized cost is defined as the actual cost plus the change in potential. In this case the actual cost of ZIG is 1 rotation. As for the change in potential, the ranks of x and p may change, but note that the ranks of all nodes in the subtrees A, B, and C do not change. Note also that the old rank of p is the same as the new rank of x (since p used to be the root of the whole tree, but afterwards x is, so the new x has the same number of nodes under it as the old p did). So the amortized cost is
Lemma ZIG-ZIG bound [00UK]
- November 25, 2026
In the context of the ZIG-ZIG operation, 2 \leq 2R'(x) - R(x) - R'(g).
Proof.
- November 25, 2026
For the final step, note that S(x) + S'(g) \leq S'(x) (since the original tree rooted at x and the new tree rooted at g are completely disjoint, and all the elements of both have x as an ancestor in the new tree), so \frac {S(x)}{S'(x)} + \frac {S'(g)}{S'(x)} = \frac {S(x) + S'(g)}{S'(x)} \leq 1. Therefore, the bound of -2 follows from the previous lemma.
The lemma as stated now follows by negating both sides of the inequality (and flipping the inequality appropriately).
Lemma Amortized cost of ZIG-ZIG [00UL]
- November 25, 2026
The amortized cost of a ZIG-ZIG operation is \leq 3(R'(x) - R(x)).
Proof.
- November 25, 2026
Lemma Amortized cost of ZIG-ZAG [00UM]
- November 25, 2026
The amortized cost of a ZIG-ZAG operation is \leq 3(R'(x) - R(x)).
Proof.
- November 25, 2026
Main proof for splay runtime [00UN]
- November 25, 2026
Now we have finally built the tools we need to prove the original theorem:
Theorem Amortized time for splay [00U4]
- November 25, 2026
The \mathsf {Splay} operation takes amortized \Theta (\lg n) time.
Proof.
- November 25, 2026
The amortized cost of a \mathsf {Splay} operation is the sum of the amortized costs of each of the rule applications as x makes its way up the tree; we have shown that each of these costs at most 3(R'(x) - R(x)), except possibly one final ZIG application which takes at most 1 + 3(R'(x) - R(x)). Note that in each case, the R'(x) from one step becomes the R(x) for the next step, and so the sum telescopes, with all of the intermediate R values cancelling, leaving at most 3(R(t) - R(x_0)) + 1, where t denotes the root of the entire tree (where x ultimately ends up) and x_0 denotes the starting location for x. The size of the tree t is of course n, so R(t) = \lg n. Also, in the worst case, x had to start all the way down at a leaf, which has R(x_0) = \lg 1 = 0. Therefore the amortized cost is bounded by 3\log _2 n + 1, which is O(\log n). Of course, it is clear that the cost has to be at least \Omega (\log n) as well, since the height of a tree of size n is at least \lg n. Therefore \mathsf {Splay} takes amortized \Theta (\lg n) time.