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 operation
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

Example
Following are the implementations of this operation in various programming languages −
#include <stdio.h>#include <stdlib.h>structnode{int data;structnode*leftChild,*rightChild;};structnode*newNode(int data){structnode* Node =(structnode*)malloc(sizeof(structnode));
Node->data = data;
Node->leftChild = Node->rightChild =NULL;return(Node);}structnode*rightRotate(structnode*x){structnode*y = x->leftChild;
x->leftChild = y->rightChild;
y->rightChild = x;return y;}structnode*leftRotate(structnode*x){structnode*y = x->rightChild;
x->rightChild = y->leftChild;
y->leftChild = x;return y;}structnode*splay(structnode*root,int data){if(root ==NULL|| root->data == data)return root;if(root->data > data){if(root->leftChild ==NULL)return root;if(root->leftChild->data > data){
root->leftChild->leftChild =splay(root->leftChild->leftChild, data);
root =rightRotate(root);}elseif(root->leftChild->data < data){
root->leftChild->rightChild =splay(root->leftChild->rightChild, data);if(root->leftChild->rightChild !=NULL)
root->leftChild =leftRotate(root->leftChild);}return(root->leftChild ==NULL)? root:rightRotate(root);}else{if(root->rightChild ==NULL)return root;if(root->rightChild->data > data){
root->rightChild->leftChild =splay(root->rightChild->leftChild, data);if(root->rightChild->leftChild !=NULL)
root->rightChild =rightRotate(root->rightChild);}elseif(root->rightChild->data < data){
root->rightChild->rightChild =splay(root->rightChild->rightChild, data);
root =leftRotate(root);}return(root->rightChild ==NULL)? root:leftRotate(root);}}structnode*insert(structnode*root,int k){if(root ==NULL)returnnewNode(k);
root =splay(root, k);if(root->data == k)return root;structnode*newnode =newNode(k);if(root->data > k){
newnode->rightChild = root;
newnode->leftChild = root->leftChild;
root->leftChild =NULL;}else{
newnode->leftChild = root;
newnode->rightChild = root->rightChild;
root->rightChild =NULL;}return newnode;}voidprintTree(structnode*root){if(root ==NULL)return;if(root !=NULL){printTree(root->leftChild);printf("%d ", root->data);printTree(root->rightChild);}}intmain(){structnode* root =newNode(34);
root->leftChild =newNode(15);
root->rightChild =newNode(40);
root->leftChild->leftChild =newNode(12);
root->leftChild->leftChild->rightChild =newNode(14);
root->rightChild->rightChild =newNode(59);printf("The Splay tree is: \n");printTree(root);return0;}
Output
The Splay tree is: 12 14 15 34 40 59
Deletion operation
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.

Example
Following are the implementations of Splay Tree deletion operation in various programming languages −
#include <stdio.h>#include <stdlib.h>structnode{int data;structnode*leftChild,*rightChild;};structnode*newNode(int data){structnode* Node =(structnode*)malloc(sizeof(structnode));
Node->data = data;
Node->leftChild = Node->rightChild =NULL;return(Node);}structnode*rightRotate(structnode*x){structnode*y = x->leftChild;
x->leftChild = y->rightChild;
y->rightChild = x;return y;}structnode*leftRotate(structnode*x){structnode*y = x->rightChild;
x->rightChild = y->leftChild;
y->leftChild = x;return y;}structnode*splay(structnode*root,int data){if(root ==NULL|| root->data == data)return root;if(root->data > data){if(root->leftChild ==NULL)return root;if(root->leftChild->data > data){
root->leftChild->leftChild =splay(root->leftChild->leftChild, data);
root =rightRotate(root);}elseif(root->leftChild->data < data){
root->leftChild->rightChild =splay(root->leftChild->rightChild, data);if(root->leftChild->rightChild !=NULL)
root->leftChild =leftRotate(root->leftChild);}return(root->leftChild ==NULL)? root:rightRotate(root);}else{if(root->rightChild ==NULL)return root;if(root->rightChild->data > data){
root->rightChild->leftChild =splay(root->rightChild->leftChild, data);if(root->rightChild->leftChild !=NULL)
root->rightChild =rightRotate(root->rightChild);}elseif(root->rightChild->data < data){
root->rightChild->rightChild =splay(root->rightChild->rightChild, data);
root =leftRotate(root);}return(root->rightChild ==NULL)? root:leftRotate(root);}}structnode*insert(structnode*root,int k){if(root ==NULL)returnnewNode(k);
root =splay(root, k);if(root->data == k)return root;structnode*newnode =newNode(k);if(root->data > k){
newnode->rightChild = root;
newnode->leftChild = root->leftChild;
root->leftChild =NULL;}else{
newnode->leftChild = root;
newnode->rightChild = root->rightChild;
root->rightChild =NULL;}return newnode;}structnode*deletenode(structnode* root,int data){structnode* temp;if(root ==NULL)returnNULL;
root =splay(root, data);if(data != root->data)return root;if(!root->leftChild){
temp = root;
root = root->rightChild;}else{
temp = root;
root =splay(root->leftChild, data);
root->rightChild = temp->rightChild;}free(temp);return root;}voidprintTree(structnode*root){if(root ==NULL)return;if(root !=NULL){printTree(root->leftChild);printf("%d ", root->data);printTree(root->rightChild);}}intmain(){structnode* root =newNode(34);
root->leftChild =newNode(15);
root->rightChild =newNode(40);printf("The Splay tree is \n");printTree(root);
root =deletenode(root,40);printf("\nThe Splay tree after deletion is \n");printTree(root);return0;}
Output
The Splay tree is 15 34 40 The Splay tree after deletion is 15 34
Search operation
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.
Example
Following are the implementations of this operation in various programming languages −
#include <stdio.h>#include <stdlib.h>structnode{int data;structnode*leftChild,*rightChild;};structnode*newNode(int data){structnode* Node =(structnode*)malloc(sizeof(structnode));
Node->data = data;
Node->leftChild = Node->rightChild =NULL;return(Node);}structnode*rightRotate(structnode*x){structnode*y = x->leftChild;
x->leftChild = y->rightChild;
y->rightChild = x;return y;}structnode*leftRotate(structnode*x){structnode*y = x->rightChild;
x->rightChild = y->leftChild;
y->leftChild = x;return y;}structnode*splay(structnode*root,int data){if(root ==NULL|| root->data == data)return root;if(root->data > data){if(root->leftChild ==NULL)return root;if(root->leftChild->data > data){
root->leftChild->leftChild =splay(root->leftChild->leftChild, data);
root =rightRotate(root);}elseif(root->leftChild->data < data){
root->leftChild->rightChild =splay(root->leftChild->rightChild, data);if(root->leftChild->rightChild !=NULL)
root->leftChild =leftRotate(root->leftChild);}return(root->leftChild ==NULL)? root:rightRotate(root);}else{if(root->rightChild ==NULL)return root;if(root->rightChild->data > data){
root->rightChild->leftChild =splay(root->rightChild->leftChild, data);if(root->rightChild->leftChild !=NULL)
root->rightChild =rightRotate(root->rightChild);}elseif(root->rightChild->data < data){
root->rightChild->rightChild =splay(root->rightChild->rightChild, data);
root =leftRotate(root);}return(root->rightChild ==NULL)? root:leftRotate(root);}}structnode*insert(structnode*root,int k){if(root ==NULL)returnnewNode(k);
root =splay(root, k);if(root->data == k)return root;structnode*newnode =newNode(k);if(root->data > k){
newnode->rightChild = root;
newnode->leftChild = root->leftChild;
root->leftChild =NULL;}else{
newnode->leftChild = root;
newnode->rightChild = root->rightChild;
root->rightChild =NULL;}return newnode;}structnode*search(structnode* root,int data){returnsplay(root, data);}voidprintTree(structnode*root){if(root ==NULL)return;if(root !=NULL){printTree(root->leftChild);printf("%d ", root->data);printTree(root->rightChild);}}voidpreOrder(structnode*root){if(root !=NULL){printf("%d ", root->data);preOrder(root->leftChild);preOrder(root->rightChild);}}intmain(){structnode* root =newNode(34);
root->leftChild =newNode(15);
root->rightChild =newNode(40);
root->leftChild->leftChild =newNode(12);
root->leftChild->leftChild->rightChild =newNode(14);
root->rightChild->rightChild =newNode(59);printf("The Splay tree is \n");printTree(root);int ele =14;printf("\nElement to be searched: %d", ele);
root =search(root, ele);printf("\nModified preorder traversal if element is found: ");preOrder(root);}
Output
The Splay tree is 12 14 15 34 40 59 Element to be searched: 14 Modified preorder traversal if element is found: 14 12 15 34 40 59
Leave a Reply