Splay tree examples [00U2]
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.