Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
51 views

39 - Data Structure and Algorithms - Splay Trees

Uploaded by

laraib
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

39 - Data Structure and Algorithms - Splay Trees

Uploaded by

laraib
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Data Structure and Algorithms -

Splay Trees

Bhabani Shankar Pradhan


Splay trees are the altered versions of the Binary Search Trees, since it
contains all the operations of BSTs, like insertion, deletion and searching,
followed by another extended operation called splaying.

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.

But how do we perform splaying? Splaying, in simpler terms, is just a process


to bring an operational node to the root. There are six types of rotations for it.

 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.

After the first rotation, the tree will look like −


Then the final tree after the second rotation is given as follows. However, the
operational node is still not the root so the splaying is considered incomplete.
Hence, other suitable rotations are again applied in this case until the node
becomes the root.

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.

After the first rotation, the tree is −

The final tree after the second rotation −


Zag-Zig rotation

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.

Basic Operations of Splay Trees


A splay contains the same basic operations that a Binary Search Tree provides
with: Insertion, Deletion, and Search. However, after every operation there is
an additional operation that differs them from Binary Search tree operations:
Splaying. We have learned about Splaying already so let us understand the
procedures of the other operations.

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.

Zag (Left) Rotation is applied on the new node


Deletion

The deletion operation in a splay tree is performed as following −

 Apply splaying operation on the node to be deleted.

 Once, the node is made the root, delete the 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.

You might also like