SlideShare a Scribd company logo
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.

More Related Content

PPTX
Splay trees by NIKHIL ARORA (www.internetnotes.in)
PPTX
Splay trees by NIKHIL ARORA (www.internetnotes.in)
PDF
AVL tree ( Balanced Binary Search Tree)-Data Structure
PDF
AVL tree ( Balanced Binary Search Tree)-Data Structure
PPTX
Lecture 14 splay tree
PPTX
Lecture 14 splay tree
Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
Lecture 14 splay tree
Lecture 14 splay tree

Similar to Splay Trees Data Structure and Algorithm (DSA).pptx (20)

PPTX
Splay Tree Presentation Slides
PPTX
Splay Tree Presentation Slides
PDF
B Tree, Introduction ,example,Splay tree
PDF
B Tree, Introduction ,example,Splay tree
PPT
1.8 splay tree
PPT
1.8 splay tree
PPT
Splay trees
PPT
Splay trees
PPTX
AVL tree PPT.pptx
PPTX
AVL tree PPT.pptx
PPT
M.E - Computer Science and Engineering-Data structure avl-tree
PPT
M.E - Computer Science and Engineering-Data structure avl-tree
PPT
3-avl-tree.ppt
DOCX
Splay trees balance trees in a different way from AVL trees. A.docx
PPT
3-avl-tree.ppt
DOCX
Splay trees balance trees in a different way from AVL trees. A.docx
PPTX
Splay tree && euler tour tree
PPTX
Splay tree && euler tour tree
PPTX
Splay trees
PPTX
Splay trees
Splay Tree Presentation Slides
Splay Tree Presentation Slides
B Tree, Introduction ,example,Splay tree
B Tree, Introduction ,example,Splay tree
1.8 splay tree
1.8 splay tree
Splay trees
Splay trees
AVL tree PPT.pptx
AVL tree PPT.pptx
M.E - Computer Science and Engineering-Data structure avl-tree
M.E - Computer Science and Engineering-Data structure avl-tree
3-avl-tree.ppt
Splay trees balance trees in a different way from AVL trees. A.docx
3-avl-tree.ppt
Splay trees balance trees in a different way from AVL trees. A.docx
Splay tree && euler tour tree
Splay tree && euler tour tree
Splay trees
Splay trees
Ad

Recently uploaded (20)

PPTX
Artificial Intelligence
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Well-logging-methods_new................
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Current and future trends in Computer Vision.pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPT
Mechanical Engineering MATERIALS Selection
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Construction Project Organization Group 2.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Artificial Intelligence
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CH1 Production IntroductoryConcepts.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Well-logging-methods_new................
Operating System & Kernel Study Guide-1 - converted.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
bas. eng. economics group 4 presentation 1.pptx
Foundation to blockchain - A guide to Blockchain Tech
Current and future trends in Computer Vision.pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Mechanical Engineering MATERIALS Selection
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Lecture Notes Electrical Wiring System Components
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Construction Project Organization Group 2.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Ad

Splay Trees Data Structure and Algorithm (DSA).pptx

  • 1. 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
  • 2. 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.
  • 3. After the shift, the tree will look like −
  • 4. 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.
  • 5. The operational node becomes the root node after the shift −
  • 6. 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.
  • 7. The first rotation will shift the tree to one position right −
  • 8. The second right rotation 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-zag rotations 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 first rotation, the tree will look like −
  • 11. 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.
  • 12. 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.
  • 13. After the first rotation, the tree is −
  • 14. The final tree after the second rotation −
  • 15. 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.
  • 16. First rotation is performed, 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 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
  • 19. ■ 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.
  • 20. Zag (Left) Rotation is applied on the new node
  • 21. 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.
  • 22. ■ 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.
  • 23. 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.