Amortized analysis [00SV]
- November 17, 2026
Amortized analysis introduction and motivation [00SW]
- November 17, 2026
Binary counter incrementing [00SX]
- November 17, 2026
Consider the following problem.
Input: an array B[0\dots ] representing a binary number n. (Each B[i] is a single bit representing the coefficient of 2^i. Assume B is sufficiently large so we don’t have to worry about problems with overflow.)
Output: Increment the binary number, that is, modify B so that it represents the number n+1.
Algorithm Incrementing a binary counter [00SY]
- November 17, 2026
Here is an algorithm to solve the binary counter problem.
Binary counter increment analysis [00SZ]
- November 17, 2026
How long does this algorithm take?
- The best case is \Theta (1): if the last bit of n is 0 then all we have to do is flip it. The while loop never executes at all.
- The worst case is when n is of the form 2^k - 1, that is, all 1 bits, and to increment it we have to flip all the 1’s to 0s and then set the next 0 bit to 1. Since n requires \Theta (\lg n) bits to represent it, this means that incrementing n takes \Theta (\lg n) in the worst case.
Therefore, a sequence of n increment operations takes \Omega (n) time in the best case, and O(n \lg n) in the worst case. However, we don't typically do a bunch of random increment operations; more realistically, we start a counter at 0 and repeatedly increment up to n. We can observe that many such operations will be best cases, and that the worst case happens only infrequently. Can we say something more precise about how long it takes to perform a n increment operations (and thus how long a single increment operation takes on average)?
Amortized analysis in general [00T0]
- November 17, 2026
This is a common situation: we have some operation that we want to do repeatedly, and the operation does not always take the same amount of time. We can easily give an upper bound and a lower bound, but we want to be able to say something more precise about how long the entire sequence of operations will take. For this we turn to amortized analysis. The idea of “amortization” is to spread out big one-time costs more evenly (e.g. repaying a car loan or a mortgage in monthly installments).
Approaching increment cost via a table [00T1]
- November 17, 2026
The first step is almost always to just try small examples, play around, make a table, and notice patterns to come up with a guess. Let’s try it for incrementing a binary counter. We’ll make a table with the counter value n and the cumulative number of bit flips on the right-hand side. Remember we don’t need a formula for the number of bit flips (though it turns out in this case it is possible to come up with one!); we are just trying to figure out how fast it grows in relation to n.
| n | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| flips | 1 | 2 | 1 | 3 | 1 | 2 | 1 | 4 | 1 | 2 | 1 | 3 | 1 | 2 | 1 | 5 | |
| total | 0 | 1 | 3 | 4 | 7 | 8 | 10 | 11 | 15 | 16 | 18 | 19 | 22 | 23 | 25 | 26 | 31 |
We notice some patterns: whenever n is a power of two, the total number of bit flips seems to be one less than the next power of two. In fact, in general it seems that the cumulative number of bit flips is never more than 2n. That is, we conjecture that T(n) < 2n for all n > 0. Based on this evidence, we conjecture that a sequence of n increment operations starting from 0 actually takes \Theta (n), the lower bound we derived from the best case, rather than the worst-case upper bound of \Theta (n \lg n). Intuitively, it seems like the “expensive” increment operations happen infrequently enough that they don’t add too much to the total—we can “average out” their cost over the whole sequence.
The average cost of a single increment operation is just the total cost, T(n), divided by the number of operations (n). If it's true that T(n) < 2n, then the average is T(n)/n < 2n/n = 2 = \Theta (1). In this case we say that a single increment operation takes \Theta (1) amortized time: each increment takes \Theta (1) “on average”, even though an individual increment operation could take longer.
So how can we actually prove this? There are (at least) three methods; we will cover two of them.
Direct calculation [00T5]
- November 18, 2026
Sometimes, we can actually compute a formula for the total T(n) of a sequence of n operations—or at least show that the total T(n) is bounded by some simple formula. Then we can calculate the amortized time for a single operation as T(n)/n.
As an example, let's try coming up with a formula for the total number of bit flips needed to increment a binary counter from 0 to n. We have T(n) = 1 + 2 + 1 + 3 + 1 + 2 + 1 + 4 + 1 + 2 + 1 + 3 + 1 + 2 + 1 + 5 + \dots Incidentally, this pattern is sometimes called the ruler function since it looks like a traditional ruler showing inches, half inches, quarter inches... with each subsequent marking being shorter:
It's not obvious how to come up with a formula for T(n), but a change of perspective is often helpful, such as rearranging the terms being added or slicing them a different way. In this case, if we draw the terms being added with dots, it looks like this:
Instead of adding up each column, we can think about adding by rows. The bottom row contains n dots, because the last bit flips with every single increment. The second-to-last row contains \lfloor n/2 \rfloor dots, because the second-to-last bit flips every other increment. The third-to-last row contains \lfloor n/4 \rfloor dots, and so on. Hence T(n) = n + \lfloor n/2 \rfloor + \lfloor n/4 \rfloor + \lfloor n/8 \rfloor + \dots This formula is exact, since at some point \lfloor n/2^k \rfloor becomes zero, for k > \lg n.We still don't know how to simplify this exactly, but we don't need to: remember that we are just trying to show that T(n) < 2n. So we can compute as follows:
Remark
- November 18, 2026
Note that we only claim T(n) \leq 2n in the above proof, not T(n) < 2n as conjectured, since this is still good enough to give us the result about increment taking \Theta (1) on average. In fact, we can replace the \leq in the proof with <, but it would require a slightly more nuanced argument about why removing the rounding makes the sum strictly larger.
Accounting Method [00T6]
- November 18, 2026
Imagine that each constant-time operation “costs” $1. The idea is to overcharge for some operations and “save up” the extra money, so that we have enough saved up to pay for expensive operations. In general, if we charge c \cdot f(n) for some operations and always have enough left over to pay for the other operations, then we can say that each operation takes O(f(n)) amortized time.
In this example, we imagine that flipping a bit costs $1. Let’s charge $2 every time we flip a bit from 0 to 1. $1 will pay for the flip itself, and the other $1 we imagine being saved next to the 1 bit. Later, when we need to flip the bit back to 0, we will have $1 sitting there which we can use to pay for the flip. 000 \overset {\text {\textdollar } 2}{\longrightarrow } 00\underset {\text {\textdollar }}{1} \overset {\text {\textdollar } 2}{\longrightarrow } 0\underset {\text {\textdollar }}{1} 0 \overset {\text {\textdollar } 2}{\longrightarrow } 0\underset {\text {\textdollar }}{1}\underset {\text {\textdollar }}{1} \overset {\text {\textdollar } 2}{\longrightarrow } \underset {\text {\textdollar }}{1} 00 Notice how every increment operation does exactly one 0 \to 1 flip, for which we pay $2. And it might do a bunch of 1 \to 0 flips, but we get to do those “for free” using the money we saved up from previous increments. All in all, if we pay $2 for each increment we always have enough money to pay for all the bit flips (without ever going negative). Therefore, we conclude that the amortized cost of a single increment operation is $2, that is, \Theta (1).
As a fun aside, this analysis actually shows us how to come up with a precise formula for the total number of bit flips needed to increment from 0 to n: after doing n increment operations, we have paid \text {\textdollar }2n, but not all of that \text {\textdollar } 2n has actually been used. There is $1 sitting next to each 1 bit, and the rest of the \text {\textdollar } 2n has been used to pay for bit flips. So the total number of bit flips needed to increment from 0 to n is exactly 2n - \#n where \#n is the number of 1 bits in the binary representation of n.
★ Amortized analysis example: another binary counter [00TC]
- November 21, 2026
Take the same binary counter incrementing example, but now suppose it costs 2^i to flip bit i. The worst case for a single increment operation is now \Theta (n), since if we have to flip all the bits it will cost 2^0 + 2^1 + 2^2 + \dots which is approximately equal to n. But once again, the total time to do n successive increment operations is actually less than \Theta (n^2). Let’s again make a table and look for patterns.
| n | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| incr | 1 | 3 | 1 | 7 | 1 | 3 | 1 | 15 | 1 | 3 | 1 | 7 | 1 | 3 | 1 | 31 | |
| total | 0 | 1 | 4 | 5 | 12 | 13 | 16 | 17 | 32 | 33 | 36 | 37 | 44 | 45 | 48 | 49 | 80 |
Looking at just the powers of 2 might reveal a useful pattern: 2 \to 4, 4 \to 12, 8 \to 32, 16 \to 80. It looks like each power of 2 is being multiplied by successive integers (2 \times 2, 4 \times 3, 8 \times 4, 16 \times 5…)in particular 2^k is being multiplied by k+1. So we conjecture that the total cost to increment from 0 to n is no greater than n \times (\lg n + 1), which would mean that the amortized cost of each increment operation is \Theta (\lg n).
To show this, we can use the accounting method: every time we do an increment, just put $1 on every bit, whether we flip it or not. (We have to decide up front how many bits we are going to use.) Since bit i is flipped every 2^i increment operations, by the time we flip it we will have accumulated exactly enough money to pay for its cost of 2^i. Therefore, the amortized cost of a single increment is the amount we actually pay: we pay $1 for each bit and there are \Theta (\lg n) bits.
We could also use the direct calculation method. Note that bit i is flipped only once every 2^i increments, and flipping it costs 2^i. So each bit contributes a total cost that is at most the total number of increment operations. There are \lg n + 1 bits, and each contributes a cost of at most n, for a total of at most n (\lg n + 1).
Amortized analysis example: extensible arrays [00TD]
- November 21, 2026
Another example that should be familiar from Data Structures: extensible arrays (e.g. ArrayList in Java). As our cost model, suppose accessing or modifying an array entry costs 1, and allocating a new array and copying the contents of an old array into it costs n (the length of the old array). Every time we do an \mathsf {append} operation, it might cost \Theta (1) (if there is still enough space in the underlying array) or it might cost \Theta (n) if we have to allocate a bigger array, copy all the elements over from the old array, and then insert the new element. So we can definitely say that a sequence of n \mathsf {append} operations is \Omega (n) and O(n^2).
The question is, what strategy should we use for allocating a new array when we run out of space? This turns out to have a big impact on the amortized time of \mathsf {append}.
- Strategy 1: increase the size by some constant c. That is, when our array of size n becomes full, allocate a new array of size n + c and copy the contents of the old array into it.
- Strategy 2: double the size. That is, when our array of size n becomes full, allocate a new array of size 2n.
Let’s consider the amortized time for a single \mathsf {append} operation using these strategies.
Strategy 1: suppose we start with an array of size c. (The starting size of the array does not really change the analysis at all.) The first c \mathsf {append} operations will cost 1 each, and then the next will cost c + 1 (c to copy the now-full array of size c, and 1 more to insert the new element). Then there will be another c-1 operations that cost 1 each until the new array is full. The next will cost 2c + 1 (copy the full array of size 2c plus 1 to insert). And so on.
In general, if we do n successive \mathsf {append} operations, we will end up paying n for the actual inserts, plus c + 2c + 3c + 4c + \dots + \lfloor n/c \rfloor c for array allocations. This is c (1 + 2 + 3 + \dots + \lfloor n/c \rfloor ) = c \cdot \Theta ((n/c)^2) = \Theta (n^2). So the total cost for n successive \mathsf {append} operations is \Theta (n) + \Theta (n^2) = \Theta (n^2), and a single \mathsf {append} has an amortized cost of \Theta (n). In this case, the cost of the expensive operations ends up dominating, even though most of the calls to \mathsf {append} are just \Theta (1).
Intuitively, since c is fixed, as our array becomes large, we spend an increasingly large fraction of the time allocating and copying arrays, relatively speaking.
Strategy 2: suppose we start with an array of size 1. If we do a sequence of n \mathsf {append} operations, of course we will still pay n for the actual insertions; the question is how much we pay for array allocations. After the initial array becomes full (immediately), we pay 1 to allocate a new array of size 2 and copy the element over. When that becomes full, we pay 2 to allocate an array of size 4 and copy over the old elements. And so on. In total, we will pay 1 + 2 + 4 + 8 + \dots + 2^k, where 2^k is the biggest power of 2 which is less than n. But this sum is 2^{k+1} - 1 which is approximately 2n, that is, \Theta (n). So the total cost of n calls to \mathsf {append} is \Theta (n) + \Theta (n) = \Theta (n), and the amortized cost of a single \mathsf {append} is only \Theta (1).
We can also prove this using the accounting method. Charge $3 for each \mathsf {append}. $1 goes to paying for the actual array insertion; the other $2 is saved along with the inserted element. Each time the array becomes full, it will look something like this:
1 3 19 -5 \underset {\text {\textdollar }\text {\textdollar }}{6} \underset {\text {\textdollar }\text {\textdollar }}{8} \underset {\text {\textdollar }\text {\textdollar }}{21} \underset {\text {\textdollar }\text {\textdollar }}{0} Since the array capacity was doubled the last time it was resized, for each element already in the array there will be a newly inserted element with $2 stored next to it. Therefore the total amount of money available is equal to the number of elements in the array, and we can pay to allocate a new array and copy all the elements over.
Thus, the amortized cost of a single \mathsf {append} operation is $3, that is, \Theta (1).
Binomial heaps [00TE]
- November 21, 2026
Binomial heap motivation [00TF]
- November 21, 2026
Recall the binary heap data structure—a complete binary tree where the value at each node is less than the values at both its children. This lets us implement priority queues with \Theta (\lg n)-time insert, remove-minimum, and change-key operations.
Consider the merge operation: given two priority queues, merge them into a single combined priority queue. This is a natural and useful operation in many applications. Unfortunately, binary heaps do not support this operation at all. The best one can do is to just list all the elements in the two heaps and then build a new heap out of the elements; this takes linear time. Can we do better?
Definition Binomial tree [00TG]
- November 21, 2026
Lemma Binomial tree size [00TH]
- November 21, 2026
A binomial tree of order n has 2^n nodes.
Proof.
- November 21, 2026
Remark Binomial trees and binomial coefficients [00TI]
- November 21, 2026
Binomial trees are called binomial since a binomial tree of order n has \binom n k nodes at depth k. This can also be proved by induction, using some facts about binomial coefficients (just think about adding a row of Pascal’s triangle shifted against itself to get the next row).
Lemma Gluing binomial trees [00TJ]
- November 21, 2026
We can make a binomial tree of order n out of two binomial trees of order n-1, by attaching one of the trees as the rightmost child of the other tree’s root node. This is apparent from the pictures above and from the definition.
Definition Binomial heap [00TK]
- November 21, 2026
A binomial heap is a list of binomial trees such that:
- Each binomial tree satisfies the heap property (the value stored at each node is smaller than all its children)
- There is at most one binomial tree of any given order.
Below is an example of a valid binomial heap.
Binomial heaps and binary numbers [00TL]
- November 21, 2026
We usually keep the list of binomial trees sorted from smallest to biggest order, stored using a linked list. (I will draw them sorted from right to left.) Notice that a binomial heap acts a lot like a binary number: since binomial trees have sizes that are powers of two, and a binomial heap has at most one tree of any given order, it is not hard to see that a binomial heap with n total elements has a binomial tree of order k if and only if the 2^k bit of the binary representation of n is a 1. For example, the heap illustrated above has 13 nodes, 13_2 = 1101, and indeed the heap has one tree of order 0 (size 1), one of order 2 (size 4), and one of order 3 (size 8). This also means a binomial heap of size n contains O(\lg n) trees, from order 0 up through order \lfloor \lg n \rfloor .
Merging binomial trees [00TM]
- November 21, 2026
To merge two binomial trees of order k with the heap property into a single binomial tree of order k+1 that also has the heap property, just see which one has the bigger root, and make it the leftmost child of the smaller root. This takes \Theta (1) time.
Merging binomial heaps [00TN]
- November 21, 2026
To merge two binomial heaps A and B, we can do what amounts to addition of binary numbers. Iterate through them in parallel, from order 0 up. For each order k we have possibly a binomial tree of order k from A, possibly a tree of order k from B, and we also may possibly have a carry tree of order k, just like we can carry a 1 bit when adding binary numbers. (When we start out at k = 0 we have no carry tree.)
- If there are no trees of order k, the output has no trees of order k. (0 + 0 = 0.)
- If there is only one tree of order k, just copy it to the output. (0 + 1 = 1.)
- If there are two trees of order k, merge them into a single tree of order k+1 and make it the carry tree for the next iteration; the output has no trees of order k. (1 + 1 = 10, so output 0 and carry the 1.)
- If there are three trees of order k, copy one of them to the output, and merge the other two into an order-(k+1) carry tree. (1 + 1 + 1 = 11: output 1 and carry 1.)
Each iteration of the above algorithm takes constant time: we may have to merge two trees but that takes constant time. So merging two binomial heaps of size n can be done in \Theta (\lg n) time, since we do \Theta (1) work for each order from 0 up to \lfloor \lg n \rfloor .
Other operations on binomial heaps [00TO]
- November 21, 2026
Now, let’s see how we can implement the other priority queue methods as well:
- \mathsf {Insert}: to insert a new element x into a binomial heap H, make x into a size-1 binomial heap (containing a single order-0 binomial tree) and then merge it with H. This is just like incrementing a binary counter! So although it is O(\lg n) in the worst case, it takes \Theta (1) amortized time. (This is even better than a standard binary-tree based heap, which always takes \Theta (\lg n) to insert!)
- \mathsf {Delete}-\mathsf {min}: iterate through the roots of all the trees to find the smallest in \Theta (\lg n) time (the smallest element in the entire heap is guaranteed to be one of the roots). Removing it leaves its children, which are binomial trees of order k-1, k-2, …, 0: a binomial heap! Just merge this remaining binomial heap with the rest of the heap in \Theta (\lg n) time. So overall delete-min takes \Theta (\lg n). (Note we can also keep track of the minimum root so that finding the minimum can be \Theta (1), even though removing it still takes \Theta (\lg n).)
- \mathsf {Change}-\mathsf {key}: just bubble the key up or down within its binomial tree until it is at the right spot. This takes O(\lg n).
★ The Potential Method [00TT]
- November 25, 2026
Motivation [00TU]
- November 25, 2026
There is one more method we can use to prove amortized time bounds, called the potential method, which comes from an analogy with physics. The idea is to think of a data structure as having some “potential energy”. Operations on the data structure will sometimes increase the potential energy (storing up some extra energy that can be used later) and sometimes decrease it (releasing some of the stored energy). The amortized cost of an operation is the actual cost of the operation plus the change in energy. The change in energy can be positive (in which case the amortized cost is more than the actual cost, and some energy is stored for later) or negative (in which case the amortized cost is less than the actual cost, using up some of the stored energy to pay for the difference).
The Potential Method, formally [00TV]
- November 25, 2026
Consider a sequence of operations on a data structure, and let D_i denote the state of the data structure after the ith operation, and \Phi _i the potential energy of D_i. Then the amortized cost of the ith operation (which turned D_{i-1} into D_i) is defined as a_i = c_i + \Phi _i - \Phi _{i-1} where c_i is the actual cost of the operation. Therefore the amortized cost of the sequence of operations is \sum _{i=1}^n a_i = \sum _{i=1}^n (c_i + \Phi _i - \Phi _{i-1}) =
\left ( \sum _{i=1}^n c_i \right ) + \Phi _n - \Phi _0. We need the total amortized cost to be an upper bound on the actual cost, and we can see this will happen as long as \Phi _n - \Phi _0 \geq 0, which motivates the following definition:
\Phi is valid if \Phi _i - \Phi _0 \geq 0 for all i.Definition Valid potential function [00TW]
Remark Potential method generalizes accounting method [00TX]
- November 25, 2026
Note that this can be seen as a generalization of the accounting method: the amount of “extra money” stored in a data structure can be thought of as “potential energy”. But the potential method makes it more clear that the energy does not have to be an integer, nor does it have to be “stored” anywhere in particular. In some cases this leads to an easier analysis even though in theory we could do the analysis using the accounting method (with weird fractional amounts of money stored in a “bank account”).
★ 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.