Imagine a library where the librarian automatically moves every book you request to the front shelf. Borrow the same novel twice this week and it is right there waiting for you. Request a book you haven't touched in years and it comes from the back — but now it sits up front too, ready for next time.
That is precisely how a splay tree works. Invented in 1985 by Daniel Sleator and Robert Tarjan, it is a binary search tree with one extra rule: after every search, insertion, or deletion, the touched key is moved to the root via a sequence of tree rotations called a splay.
There are no balance bits, no height counters, no colors. The tree restructures itself lazily, based entirely on what you actually look up. The payoff is elegant: if you access n keys in any order, the total cost is — as if the tree had been perfectly balanced all along.
This is amortized analysis at its finest. Individual operations can take in the worst case, but the pain is always spread so thinly across future cheap operations that the average never hurts you. The formal proof relies on a potential function called the access lemma — a clever bookkeeping trick that charges each rotation against an imaginary bank account stored in the nodes themselves.
Comments
Loading comments...