Advanced Data Structures and Implementation
12.1 Top-Down Splay Trees
An example of the top-down splaying algorithm is shown in Figure 12.4.
How to insert an item into a tree?
A new node is allocated(if necessary), and if the tree is empty, a one-node tree is created. Otherwise, we splay root around the inserted value x. If the data in the new root is equal to x, we have a duplicate; instead of reinserting x, we preserve newNode for a future insertion and return immediately. If the new root contains a value larger than x, then the new root and its right subtree become a right subtree of newNode, and the root’s left subtree becomes the left subtree of newNode.
The deletion in spray trees is easy, because a splay will place the target of the deletion at the root.
12.2 Red-Black Trees
Operations on red-black trees take
A red-blace tree is a binary search tree with the following coloring properties:
1. Every node is colored either red or black.
2. The root is black.
3. If a node is red, its children must be black.
4. Every path from a node to a null reference must contain the same number of black nodes.
A consequence of the coloring rules is that the height of a red-black tree is at most