★ Splay trees [00TY]
- November 25, 2026
Splay tree motivation [00TZ]
- November 25, 2026
As a fascinating application of the potential method we will study splay trees, a sort of “self-balancing” variant of binary search trees. Recall that binary search trees give us \Theta (\lg n) insert, lookup, and delete for ordered data, as long as the trees remain balanced. But if the tree becomes unbalanced these turn into O(n) instead. In some applications it does not matter; if a binary search tree is built randomly it is very likely to be balanced. However, in many applications the data may have characteristics that lead to an unbalanced tree (for example, the data may be already sorted or close to sorted, in which case inserting it sequentially into a naive BST creates an unbalanced tree). There are various ways to solve this problem. What most people think of are sophisticated BST variants, such as red-black trees and AVL trees, which work by storing extra information in the trees and then using this extra information to make sure the trees remain balanced. Practically speaking, these are some of the best BST implementations, but they are complex, and require reimplementing all the BST methods to ensure that the extra information is kept up-to-date and that the tree is rebalanced as necessary.
Splay trees, intuitively [00U0]
- November 25, 2026
Splay trees are an intriguing alternative: a splay tree is just a normal binary search tree, with no extra information stored anywhere. The twist is that we implement an extra splay operation which works to make the tree a little more balanced, and we use this operation in conjunction with other operations such as lookup and insert, in such a way that the tree tends to stay balanced.
In particular, the \mathsf {Splay} operation works by bringing a particular element to the root of the BST (while keeping all the same elements in the tree and preserving the BST properties). It uses a particular algorithm that we will explore shortly. However, given such an operation that brings a chosen node to the root of a BST, let’s see how we update the other BST operations to use it:
- \mathsf {Insert}: Whenever we insert a new value into the splay tree (which works just like normal BST insertion), we then splay the newly inserted element to the root.
- \mathsf {Lookup}: Similarly, lookup works just like in a normal BST; but after doing a lookup we splay the looked up element (or the closest element, in the case that the element being searched for is not in the tree).
- \mathsf {Join}: If all the elements in T_1 are less than all the elements in T_2, we can join them into a single BST by first splaying the largest element in T_1 to the root, then adding T_2 as its right child (if the largest element is at the root of a BST, by definition it has no right child, so there is a clear space to add T_2).
- \mathsf {Split}: To split a BST into two BSTs which contain all the elements less and greater than a particular target element, respectively, splay the target element to the root and then return its two children.
- \mathsf {Delete}: Splay the item to be deleted to the root, then \mathsf {join} the two subtrees. (This is only a minor point, but notice how much nicer this is than the usual BST delete implementation!)
The splay operation [00U1]
- November 25, 2026
Let’s see how \mathsf {Splay} actually works. We find the element x in the tree and then keep moving it up the tree until it reaches the root, according to the following three rules. In general we will use p to denote the parent of x and g to denote its grandparent. There are three general rules we apply depending on where x is in the tree.
ZIG [00UH]
- November 25, 2026
If x is the child of the root of the whole tree, then just do one rotation to move it to the root.
A, B, and C denote arbitrary subtrees. We don’t do anything to them at all, so the rotation takes constant time; we just need to update a few references. In the diagram, x is the left child of the root, so we rotate right, but there is also a second entirely symmetric case when x is the right child of the root and we rotate left.
Note how doing a rotation moves x up while preserving the binary search tree properties: all the elements in the subtree A are still to the left of x; all the ements in B remain in between x and p; and C remains to the right of p.
ZIG-ZIG [00UI]
- November 25, 2026
If x and p are either both left children or both right children, then do two successive rotations: first rotate around g \to p then around p \to x.
Some things to note:
- This rule applies anywhere in the tree, unlike ZIG which only applies when x is at a depth of 1. After applying ZIG-ZIG, x has moved two levels closer to the root.
- Like ZIG, this takes constant time, since we just do two rotations.
- Again, in the picture x and p are illustrated as both being left children, but there is an analogous symmetric case when x and p are both right children.
ZIG-ZAG [00UJ]
- November 25, 2026
Splay tree examples [00U2]
- November 25, 2026
Let’s start with some interactive examples to see how splay trees work in practice, and to gain some intuition for what we are trying to prove. For example, if we start with the tree on the left below, the next tree shows what we get if we splay 5 to the root; the tree after that shows what we would get if instead splay 4 to the root (starting from the tree on the left again); and so on. You can see that as we splay an element from deep in the tree, the other elements tend to “clump up” in twos due to the way we iterate the ZIG-ZIG rule.
And here is what happens when we iterate the ZIG-ZAG rule: starting from the tree on the left, splaying different elements results in the trees on the right:
In this case you can see that zigzags tend to get “unzipped” into two subtrees, one with the small nodes and one with the large nodes.We think of unbalanced trees as being “tense”/having a high potential energy; the splay operation tends to “relax” them, i.e. use some of the stored potential energy to do its work and then leave the tree in a lower-energy state. Doing lots of random splay operations tends to result in a balanced-ish tree. For example, if we start with a (very unbalanced) linear chain of 100 nodes numbered 0 to 99, and do 500 random splay operations, here is (one possible) result:
In fact, it turns out this still works even if the splay operations aren’t random: even if we have an evil adversary who is trying to choose splay operations which are as bad as possible for us, the tree will still tend towards balance! Intuitively, splaying an item that is already near the top of the tree doesn’t take very long; on the other hand, splaying an item that is deep in the tree takes a while but tends to make the rest of the tree more balanced. So the evil adversary can’t win.
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.