Operations in A Splay Tree
Operations in A Splay Tree
Insertion: To insert a new element into the tree, start by performing a regular binary search tree insertion.
Then, apply rotations to bring the newly inserted element to the root of the tree.
Deletion: To delete an element from the tree, first locate it using a binary search tree search. Then, if the
element has no children, simply remove it. If it has one child, promote that child to its position in the tree.
If it has two children, find the successor of the element (the smallest element in its right subtree), swap its
key with the element to be deleted, and delete the successor instead.
Search: To search for an element in the tree, start by performing a binary search tree search. If the
element is found, apply rotations to bring it to the root of the tree. If it is not found, apply rotations to the
last node visited in the search, which becomes the new root.
Rotation: The rotations used in a splay tree are either a Zig or a Zig-Zig rotation. A Zig rotation is used to
bring a node to the root, while a Zig-Zig rotation is used to balance the tree after multiple accesses to
elements in the same subtree.
1) Zig Rotation:
The Zig Rotation in splay trees operates in a manner similar to the single right rotation in AVL Tree rotations.
This rotation results in nodes moving one position to the right from their current location. For example,
consider the following scenario:
2) Zag Rotation:
The Zag Rotation in splay trees operates in a similar fashion to the single left rotation in AVL Tree rotations.
During this rotation, nodes shift one position to the left from their current location. For instance, consider the
following illustration:
3) Zig-Zig Rotation:
The Zig-Zig Rotation in splay trees is a double zig rotation. This rotation results in nodes shifting two
positions to the right from their current location. Take a look at the following example for a better
understanding:
4) Zag-Zag Rotation:
In splay trees, the Zag-Zag Rotation is a double zag rotation. This rotation causes nodes to move two
positions to the left from their present position. For example:
The Zig-Zag Rotation in splay trees is a combination of a zig rotation followed by a zag rotation. As a result
of this rotation, nodes shift one position to the right and then one position to the left from their current
location. The following illustration provides a visual representation of this concept:
6) Zag-Zig Rotation:
The Zag-Zig Rotation in splay trees is a series of zag rotations followed by a zig rotation. This results in
nodes moving one position to the left, followed by a shift one position to the right from their current location.
The following illustration offers a visual representation of this concept: