Splay Tree in Data Structure - Simplerize
Splay Tree in Data Structure - Simplerize
A Splay Tree is a roughly balanced Binary Search Tree (BST) that always keeps the Singly Linked List
recently accessed items closer to the root node. As a result, the search operation works Doubly Linked List
must faster when the same data is accessed again.
Circular Linked List
3. Stack
Table of Contents
1. Introduction 4. Queue
2. The Splay Operation Linear Queue
2.1 Zig Rotation
2.2 Zag Rotation Circular Queue
2.3 Zig-Zig Rotation
Priority Queue
2.4 Zag-Zag Rotation
2.5 Zig-Zag Rotation Double Ended Queue
2.6 Zag-Zig Rotation
5. Hash Table
3. Insertion on Splay Tree
4. Deletion on Splay Tree Separate Chaining
5. Search on Splay Tree Linear Probing
6. Traversals on Splay Tree
7. Implementation of Splay Tree in C++ 6. Tree
8. References Binary Tree
Representation Categories
Data Structures
30 Roughly balanced: Rotations are Kubernetes
used to bring the recently
20 50
accessed node to the root. This
Archives
10 40 60
helps the tree be roughly
March 2024
balanced.
Splay Tree Representation April 2023
Zig Rotation: Right rotation.
Zag Rotation: Left rotation. March 2023
1. // Tree node representation
2. struct Node February 2023
3. {
4. int data; // Actual data January 2023
5. Node* left; // Pointer to left child
6. Node* right; // Pointer to right child December 2022
7. Node* parent; // Pointer to parent node
8. };
April 2022
9.
10. // The root node
11. Node* root; February 2022
January 2022
Applications
October 2021
Cache applications
Windows NT (in the virtual memory, networking, and file system code)
GCC compiler and GNU C++ library
The sed string editor
Fore Systems network routers
The most popular implementation of Unix malloc, Linux loadable kernel modules,
and much other software.
The splay tree operations such as insertion, deletion, and search are followed by one
special operation called splaying. It is nothing but a set of rotations performed to bring
the recently accessed node as the root.
Rotations
Splay tree rotation works primarily on three nodes: the target node, the parent, and the
grandparent. The following rotations are used by the splay-tree.
The combination of simple Left and Right rotation can bring any node as root. So why
should we take the grandparent into account and have additional types of rotations?
Because the Zig-Zig and Zag-Zag rotations work on the grandparent first. So that it
better keeps the recently accessed items closer to the root and helps to maintain the
optimal balance.
Perform the Zig rotation to make the target node (10) as root.
20 10
10 20
Perform the Zag rotation to make the target node (20) as root.
10 20
20 10
Perform the Zig-Zig rotation to make the target node (10) as root. Note that the
grandparent (30) is rotated first followed by the parent (20).
30 10
20 20 20
10 10 30 30
Perform the Zag-Zag rotation to make the target node (30) as root. Note that the
grandparent (10) is rotated first followed by the parent (20).
10 30
20 20 20
30 10 30 10
10 10
30 20 20
20 30 10 30
Perform the Zag-Zig rotation to make the target node (20) as root. Note that the parent
node (10) is rotated first followed by the grandparent (30).
30 30
10 20 20
20 10 10 30
For Zig-Zig and Zag-Zag cases, the rotation is performed on the grandparent first
followed by the parent. This is required to maintain the optimal tree balance and to
keep the recently accessed items closer to the root node.
For Zig-Zag and Zag-Zig cases, the rotation is performed on the parent first
followed by the grandparent. The reverse order does not work in this case, because
performing the first rotation on the grandparent would affect the original position
of the splaying node.
Algorithm
Code
A node is always inserted at the bottom of the tree. Traverse the tree based on the order,
insert the new node, and perform splaying to make the new node as root.
Algorithm
1. Start the traversal from the root node to find the data location.
2. If the data element is smaller than the current node, traverse on the left subtree.
3. Otherwise, traverse on the right subtree if the data element is greater.
4. Insert the node when the traversal reached the bottom.
5. Splay the tree to make the new node as root. Refer to “The Splay Operation” for
detailed steps.
Code
Examples
The following diagrams illustrate the insertion of the data 20, 10, 60, 40, 50, and 30.
Insert element 20
Insert element 20
Insert element 10
10
Subsequently, splay the tree to make
node 10 as root.
Insert element 10 The node has only the parent and
not the grandparent, so perform a
Zig (right) rotation since the node is
on the left side of the root.
Perform a Zig Rotation from the node (20)
20 10
10 20
Insert element 60
Insert element 60
Left) rotation.
10 60
20 20 20
60 10 60 10
Insert element 40
20
The node (40) has the grandparent
and is located on the Left-Right side,
10 40 so perform a Zag-Zig (Left-Right)
rotation.
Here we perform the operation on
the parent node first.
Insert element 40
20 40 40
10 40 20 20 60
10 10
Insert element 50
Insert element 50
50
40 40 40 60
20 60 20 50 20
10 50 10 60 10
Insert element 30
60
The node (30) has the grandparent
40
and is located on the Left-Right side.
20 Hence perform a Zag-Zig (Left-
Right) rotation to make the new
10 30
node as root.
Insert element 30
Perform a Zag-Zig Rotation
50 50
40 60 40 60
20 30
10 30 20
10
The new node (30) is still one level below the root node. Hence repeat the rotation
until it becomes the root.
Perform a Zig Rotation since the node (30) is on the left of the root.
50
30 60 30
20 40 20 50
10 10 40 60
Firstly, traverse the tree to find the target and splay the node to make it root. Delete the
root node and combine the left and right subtree afterward. The left subtree becomes
the root if the right subtree is empty and vice versa. If it has both the left and right
subtree, the next smaller element will be identified from the right subtree and becomes
the new root.
Algorithm
1. Start the traversal from the root node to find the target node.
2. If the data element is smaller than the current node, traverse on the left subtree.
3. Otherwise, traverse on the right subtree if the data element is greater.
4. Splay the tree to bring the target node (or the last accessed node) as root. Refer to
“The Splay Operation” for detailed steps.
5. Delete the root node if the target matches and assign the new root.
No child for the target node: Set root to NULL.
Only has the right subtree: Set root as the right subtree.
Only has the left subtree: Set root as the left subtree.
Has both left and right subtree: Find the next smaller data element (in-order
successor) node from the right subtree, splay to make this as the new root,
and link the left subtree on its left.
Code
Examples
20
perform the Zag-Zag rotation to
50
make it a root.
10 40 60 Perform Zag rotation first on the
grandparent (30)
Perform the next Zag rotation on the
parent (50)
Delete the node (60)
The target node (60) becomes the
root after Zag-Zag rotation
60
30 50 50
20 50 30 60 30
10 40 60 20 40 20 40
10 10
Delete the node (60) as it becomes the root now. The new root will be its left child
(50) since the right subtree is empty.
60
50 50
30 30
20 40 20 40
10 10
30
Left, so perform the Zig rotation to
make it the root.
20 40 Find the node (30) and perform Zig
rotation
10 So, the target node (30) becomes
the new root
Delete the node (30)
50
30 30 30
20 40 20 50 20 50
10 10 40 10 40
Deleting the node (30) separates the left and right subtree. So we need to identify
the smaller elements on the right subtree (Inorder-successor), and splay the node
to make the Root. Finally, link the left subtree with the new root.
40
20 50 20 40 20 50
10 40 10 50 10
Search the data element by traversing the tree from the root node. A splay operation is
performed at the end irrespective of whether the element is found or not. Hence a
search operation can modify the splay tree to keep the recently accessed items on top.
Algorithm
1. Start the traversal from the root node to find the target node.
2. If the data matches the current node, return the node.
3. Traverse on the left subtree if the data element is smaller than the current node,
4. Traverse on the right subtree if the data element is greater.
5. Splay the tree with the target node found or with the last accessed node. Refer to
“The Splay Operation” for detailed steps.
Code
Example
The node (20) does not have a grandparent and is located on the Left, so perform the Zig
rotation to make it the root.
40 20
20 50 10 40
10 50
See Traversals on Binary Tree that are applicable to the Splay Tree.
Output
8. References