Splay trees, intuitively [00U0]

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!)