ads note 9.15
Notes on height balance, splaying, and why a costly individual operation can still be inexpensive over a sequence.
Background
Throughout these notes, means rotating with its parent so that moves up one level. The rotation preserves the binary search tree’s in-order sequence.
AVL Trees
An empty tree is height-balanced (HB). A nonempty tree is height-balanced if and only if both subtrees and are height-balanced and their heights differ by at most one:
The balance factor of a node is
An AVL tree is a binary search tree in which every node satisfies
The four imbalance cases are LL, RR, LR, and RL. LL and RR require a single rotation; LR and RL require a double rotation.
After an insertion, rebalancing the lowest unbalanced ancestor with one single or double rotation restores the tree’s balance. After a deletion, rebalancing may need to continue toward the root.
Splay Trees
When a search finds a node , a splay tree moves it to the root. Let be its parent and its grandparent, when they exist.
There are three cases. The diagrams below omit the attached subtrees; the mirrored cases work symmetrically.
Zig
is the root, so has no grandparent.
Perform . Now is the root, and splaying is complete.
Zig-zag
and point in opposite directions: one is a left child and the other is a right child.
Perform twice. This moves above both and .
Zig-zig
and point in the same direction: both are left children or both are right children.
First perform , then . The order matters: this is not the same as rotating twice.
After either double rotation, repeat the appropriate case until becomes the root. A final zig is needed only if ends up one level below the root.
Amortized Analysis
A single operation may be expensive even when a whole sequence is efficient. For example, if operations take time in total, their amortized cost is per operation.
Amortized analysis gives a bound over an operation sequence; it does not require a probability distribution over inputs.
Let be the actual cost of operation , and let be its assigned amortized cost. We want the assigned costs to cover the actual total:
The potential method
Let denote the state of the data structure after operation . A potential function measures stored credit in that state. Define
Equivalently, the credit added during an operation is
A positive change saves credit; a negative change spends it. Summing over all operations makes the intermediate potential terms cancel:
Thus, together with is a sufficient condition for the amortized total to upper-bound the actual total. If the initial potential is nonzero, keep that term in the bound.
The potential function is chosen to match the data structure and its expensive operations. It is an analysis tool, not necessarily a value stored by the implementation.
For a stack, choose . If a push or a successful pop has unit actual cost, then
The push pays for itself and saves one unit of credit for the eventual pop.
Amortized Analysis of Splay Trees
The goal is to show an amortized bound for splaying in a tree with nodes. All logarithms below are base two.
Subtree size, rank, and potential
Let be the size of the subtree rooted at , including itself. Define its rank by
Rank measures subtree size on a logarithmic scale. It is not the actual height: a long chain can have linear height but logarithmic rank.
For a single splay step, use subscripts and for the states before and after the step:
Choose the tree’s potential to be
Only the ranks of the nodes participating in the rotations change. We count one unit of actual cost per rotation. The search path has length proportional to the number of rotations, up to an additive constant, so this model suffices for the asymptotic access bound.
We will use the following inequality. For and , the arithmetic–geometric mean inequality gives , hence
Zig: one rotation
The actual cost is , and only and change rank:
Since ‘s subtree shrinks and ‘s subtree grows, and . Therefore,
Zig-zag: two rotations
Here . After the step, contains exactly the subtree that was previously rooted at , so . These terms cancel:
Because ,
After the rotations, the subtrees rooted at and are disjoint children of , giving
Applying the logarithmic inequality,
It follows that
Zig-zig: two rotations
Again, and . Using and gives
The old subtree of and the new subtree of are disjoint parts of the new subtree of , so
Therefore,
Substituting this into the cost bound yields
Summing the steps
Each double rotation costs at most amortized. A zig contributes an additional constant, but it occurs at most once, at the end.
The rank changes telescope across the entire splay. If is the original root, then ‘s final rank equals . Thus,
This is the access lemma in the unweighted case, giving an amortized cost. A single access can still take actual time.
For a sequence starting from an arbitrary nonempty tree, is generally not zero. The total-cost accounting is
For accesses on a fixed set of nodes, gives the bound , including the initial potential.
Reference: MIT 6.851 — Splay Trees and the Access Lemma.