11.4 Splay trees
Like the AVL tree, the splay tree is not actually a distinct data structure, but rather reimplements the BST insert, delete, and search methods to improve the performance of a BST. The goal of these revised methods is to provide guarantees on the time required by a series of operations, thereby avoiding the worst-case linear time behaviour of standard BST operations. No single operation in the splay tree is guaranteed to be efficient. Instead, the access rules of the splay tree guarantee that a series of operations will take time for a tree of nodes whenever . Thus, a single insert or search operation could take time. However, such operations are guaranteed to require a total of time, for an average cost of per access operation. This is a desirable performance guarantee for any search-tree structure.
Unlike the AVL tree, the splay tree is not guaranteed to be height balanced. What is guaranteed is that the total cost of the entire series of accesses will be cheap. Ultimately, it is the cost of the series of operations that matters, not whether the tree is balanced. Maintaining balance is really done only for the sake of reaching this time efficiency goal.
The splay tree access functions operate in a manner reminiscent of the move-to-front rule for self-organising lists (see Section 7.2), and of the path compression technique for managing a series of Union/Find operations (see Section 11.5). These access functions tend to make the tree more balanced, but an individual access will not necessarily result in a more balanced tree.
Whenever a node is accessed (e.g., when is inserted, deleted, or is the goal of a search), the splay tree performs a process called splaying. Splaying moves to the root of the BST. When is being deleted, splaying moves the parent of to the root. As in the AVL tree, a splay of node consists of a series of rotations. A rotation moves higher in the tree by adjusting its position with respect to its parent and grandparent. A side effect of the rotations is a tendency to balance the tree. There are three types of rotation.
Note that even if you only search for a value, the tree structure is changed because the found node will be splayed to the root. This is a big difference between splay trees and the search trees we have looked at previously.
11.4.1 Splaying
A single rotation is performed only if is a child of the root node. The single rotation is illustrated by Figure 11.7. It basically switches with its parent in a way that retains the BST property. While this figure is slightly different from Figure 11.5, in fact the splay tree single rotation is identical to the AVL tree single rotation.

Unlike the AVL tree, the splay tree requires two types of double rotation. Double rotations involve , its parent (call it ), and ’s grandparent (call it ). The effect of a double rotation is to move up two levels in the tree.
- ZigZag
-
The first double rotation is called a zigzag rotation. It takes place when either of the following two conditions are met:
- is the left child of , and is the right child of .
- is the right child of , and is the left child of .
In other words, a zigzag rotation is used when , , and form a zigzag. The zigzag rotation is illustrated by Figure 11.8.
- ZigZig
-
The other double rotation is known as a zigzig rotation. It takes place when either of the following two conditions are met:
- is the left child of , which is in turn the left child of .
- is the right child of , which is in turn the right child of .
Thus, a zigzig rotation takes place in those situations where a zigzag rotation is not appropriate. The zigzig rotation is illustrated by Figure 11.9.


Note that zigzag rotations tend to make the tree more balanced, because they bring subtrees and up one level while moving subtree down one level. The result is often a reduction of the tree’s height by one. While Figure 11.8 appears somewhat different from Figure 11.6, in fact the zigzag rotation is identical to the AVL tree double rotation. Zigzig promotions and single rotations do not typically reduce the height of the tree; they merely bring the newly accessed record toward the root.
11.4.2 Searching in a splay tree
The splay tree’s search operation is identical to searching in a BST. However, once the value has been found, it is splayed to the root. This means that when you search for a value the tree structure is changed. This is a big difference between splay trees and the search trees we have looked at previously.
Splaying a node involves a series of double rotations until the node reaches either the root or the child of the root. Then, if necessary, a single rotation makes it the root. This process tends to re-balance the tree. Regardless of balance, splaying will make frequently accessed nodes stay near the top of the tree, resulting in reduced access cost. Proof that the splay tree meets the guarantee of is beyond the scope of our study.
Example: Searching in a splay tree
Consider a search for value 89 in the splay tree of Figure 11.10 (a). The splay tree’s search operation is identical to searching in a BST. However, once the value has been found, it is splayed to the root. Three rotations are required in this example. The first is a zigzig rotation, whose result is shown in figure (b). The second is a zigzag rotation, whose result is shown in figure (c). The final step is a single rotation resulting in the tree of figure (d). Notice that the splaying process has made the tree shallower.
