KEMBAR78
Splay Trees Data Structure and Algorithm (DSA).pptx
Splay Trees
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 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.
Search
The search operation in a Splay tree follows the same
procedure of the Binary Search Tree operation. However,
after the searching is done and the element is found,
splaying is applied on the node searched. If the element is
not found, then unsuccessful search is prompted.

Splay Trees Data Structure and Algorithm (DSA).pptx

  • 1.
    Splay Trees Splay treesare 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
  • 2.
    Zig rotation The zigrotations 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.
  • 3.
    After the shift,the tree will look like −
  • 4.
    Zag rotation The zagrotations 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.
  • 5.
    The operational nodebecomes the root node after the shift −
  • 6.
    Zig-Zig rotation The zig-zigrotations are performed when the operational node has both parent and a grandparent. The node is rotated two places towards its right.
  • 7.
    The first rotationwill shift the tree to one position right −
  • 8.
    The second rightrotation will once again shift the node for one position. The final tree after the shift will look like this −
  • 9.
    Zag-Zag rotation The zag-zagrotations are also performed when the operational node has both parent and a grandparent. The node is rotated two places towards its left.
  • 10.
    After the firstrotation, the tree will look like −
  • 11.
    Then the finaltree 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.
  • 12.
    Zig-Zag rotation The zig-zagrotations 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.
  • 13.
    After the firstrotation, the tree is −
  • 14.
    The final treeafter the second rotation −
  • 15.
    Zag-Zig rotation The zag-zigrotations 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.
  • 16.
    First rotation isperformed, the tree is obtained as −
  • 17.
    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.
  • 18.
    Basic Operations ofSplay 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
  • 19.
    ■ If thetree 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.
  • 20.
    Zag (Left) Rotationis applied on the new node
  • 21.
    Deletion The deletion operationin 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.
  • 22.
    ■ If root_leftis 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.
  • 23.
    Search The search operationin a Splay tree follows the same procedure of the Binary Search Tree operation. However, after the searching is done and the element is found, splaying is applied on the node searched. If the element is not found, then unsuccessful search is prompted.