39 - Data Structure and Algorithms - Splay Trees
39 - Data Structure and Algorithms - Splay Trees
Splay Trees
For instance, a value “A” is supposed to be inserted into the tree. If the tree is
empty, add “A” to the root of the tree and exit; but if the tree is not empty, use
binary search insertion operation to insert the element and then perform
splaying on the new node.
Similarly, after searching an element in the splay tree, the node consisting of
the element must be splayed as well.
Zig rotation
Zag rotation
Zig-Zig rotation
Zag-Zag rotation
Zig-Zag rotation
Zag-Zig rotation
Zig rotation
The zig rotations are performed when the operational node is either the root
node or the left child node of the root node. The node is rotated towards its
right.
After the shift, the tree will look like −
Zag rotation
The zag rotations are also performed when the operational node is either the
root node or the right child nod of the root node. The node is rotated towards
its left.
The operational node becomes the root node after the shift −
Zig-Zig rotation
The zig-zig rotations are performed when the operational node has both parent
and a grandparent. The node is rotated two places towards its right.
The first rotation will shift the tree to one position right −
The second right rotation will once again shift the node for one position. The
final tree after the shift will look like this −
Zag-Zag rotation
The zag-zag rotations are also performed when the operational node has both
parent and a grandparent. The node is rotated two places towards its left.
Zig-Zag rotation
The zig-zag rotations are performed when the operational node has both a
parent and a grandparent. But the difference is the grandparent, parent and
child are in LRL format. The node is rotated first towards its right followed by
left.
The zag-zig rotations are also performed when the operational node has both
parent and grandparent. But the difference is the grandparent, parent and child
are in RLR format. The node is rotated first towards its left followed by right.
First rotation is performed, the tree is obtained as −
After second rotation, the final tree is given as below. However, the operational
node is not the root node yet so one more rotation needs to be performed to
make the said node as the root.
Insertion
The insertion operation in a Splay tree is performed in the exact same way
insertion in a binary search tree is performed. The procedure to perform the
insertion in a splay tree is given as follows −
Check whether the tree is empty; if yes, add the new node and exit
If the tree is not empty, add the new node to the existing tree using the
binary search insertion.
Then, suitable splaying is chosen and applied on the newly added node.
Now, the tree is split into two trees, the left subtree and the right
subtree; with their respective first nodes as the root nodes: say root_left
and root_right.
If root_left is a NULL value, then the root_right will become the root of
the tree. And vice versa.
But if both root_left and root_right are not NULL values, then select the
maximum value from the left subtree and make it the new root by
connecting the subtrees.