## Splay TreeSplay trees are the self-balancing or self-adjusted binary search trees. In other words, we can say that the splay trees are the variants of the binary search trees. The prerequisite for the splay trees that we should know about the binary search trees. As we already know, the time complexity of a binary search tree in every case. The time complexity of a binary search tree in the average case is ## What is a Splay Tree?A splay tree is a self-balancing tree, but AVL and Red-Black trees are also self-balancing trees then. What makes the splay tree unique two trees. It has one extra property that makes it unique is splaying. A splay tree contains the same operations as a Binary search tree, i.e., Insertion, deletion and searching, but it also contains one more operation, i.e., splaying. So. all the operations in the splay tree are followed by splaying. Splay trees are not strictly balanced trees, but they are roughly balanced trees. Let's understand the search operation in the splay-tree. Suppose we want to search 7 element in the tree, which is shown below: To search any element in the splay tree, first, we will perform the standard binary search tree operation. As 7 is less than 10 so we will come to the left of the root node. After performing the search operation, we need to perform splaying. Here splaying means that the operation that we are performing on any element should become the root node after performing some rearrangements. The rearrangement of the tree will be done through the rotations. ## Note: The splay tree can be defined as the self-adjusted tree in which any operation performed on the element would rearrange the tree so that the element on which operation has been performed becomes the root node of the tree.## Rotations
- Zig rotation (Right rotation)
- Zag rotation (Left rotation)
- Zig zag (Zig followed by zag)
- Zag zig (Zag followed by zig)
- Zig zig (two right rotations)
- Zag zag (two left rotations)
- Does the node which we are trying to rotate have a grandparent?
- Is the node left or right child of the parent?
- Is the node left or right child of the grandparent?
## Cases for the Rotations
To rearrange the tree, we need to perform some rotations. The following are the types of rotations in the splay tree: **Zig rotations**
The zig rotations are used when the item to be searched is either a root node or the child of a root node (i.e., left or the right child).
- If the child is a left child, the right rotation would be performed, known as a zig right rotation.
- If the child is a right child, the left rotation would be performed, known as a zig left rotation.
In the above example, we have to search 7 element in the tree. We will follow the below steps:
In the above example, we have to search 20 element in the tree. We will follow the below steps:
**Zig zig rotations**
Sometimes the situation arises when the item to be searched is having a parent as well as a grandparent. In this case, we have to perform four rotations for splaying. Let's understand this case through an example. Suppose we have to search 1 element in the tree, which is shown below:
In order to make node 1 as a root node, we need to perform two right rotations known as zig zig rotations. When we perform the right rotation then 10 will move downwards, and node 7 will come upwards as shown in the below figure: Again, we will perform zig right rotation, node 7 will move downwards, and node 1 will come upwards as shown below: As we observe in the above figure that node 1 has become the root node of the tree; therefore, the searching is completed.
In order to search 20, we need to perform two left rotations. Following are the steps required to search 20 node:
In the second left rotation, node 15 will move downwards, and node 20 becomes the root node of the tree, as shown below: As we have observed that two left rotations are performed; so it is known as a zig zig left rotation. **Zig zag rotations**
Till now, we have read that both parent and grandparent are either in RR or LL relationship. Now, we will see the RL or LR relationship between the parent and the grandparent.
Suppose we want to search 13 element in the tree which is shown below:
Still, node 13 is not the root node, and 13 is at the right of the root node, so we will perform left rotation known as a zag rotation. The node 10 will move downwards, and 13 becomes the root node as shown below: As we can observe in the above tree that node 13 has become the root node; therefore, the searching is completed. In this case, we have first performed the zig rotation and then zag rotation; so, it is known as a zig zag rotation. **Zag zig rotation**
Suppose we want to search 9 element in the tree, which is shown below:
Still the node 9 is not a root node, and 9 is at the left of the root node, so we will perform the right rotation known as zig rotation. After performing the right rotation, node 9 becomes the root node, as shown below: As we can observe in the above tree that node 13 is a root node; therefore, the searching is completed. In this case, we have first performed the zag rotation (left rotation), and then zig rotation (right rotation) is performed, so it is known as a zag zig rotation. ## Advantages of Splay tree- In the splay tree, we do not need to store the extra information. In contrast, in AVL trees, we need to store the balance factor of each node that requires extra space, and Red-Black trees also require to store one extra bit of information that denotes the color of the node, either Red or Black.
- It is the fastest type of Binary Search tree for various practical applications. It is used in
**Windows NT and GCC compilers**. - It provides better performance as the frequently accessed nodes will move nearer to the root node, due to which the elements can be accessed quickly in splay trees. It is used in the cache implementation as the recently accessed data is stored in the cache so that we do not need to go to the memory for accessing the data, and it takes less time.
## Drawback of Splay treeThe major drawback of the splay tree would be that trees are not strictly balanced, i.e., they are roughly balanced. Sometimes the splay trees are linear, so it will take O(n) time complexity.
In the
Now, we perform
Now, we will perform splaying. As 17 is having a parent as well as a grandparent so we will perform zig zig rotations. In the above figure, we can observe that 17 becomes the root node of the tree; therefore, the insertion is completed.
Now, we have to splay the tree. As 7 is having a parent as well as a grandparent so we will perform two right rotations as shown below: Still the node 7 is not a root node, it is a left child of the root node, i.e., 17. So, we need to perform one more right rotation to make node 7 as a root node as shown below:
In the above algorithm, T is the tree and n is the node which we want to insert. We have created a temp variable that contains the address of the root node. We will run the while loop until the value of temp becomes NULL. Once the insertion is completed, splaying would be performed
In the above implementation, x is the node on which the rotation is performed, whereas y is the left child of the node x.
In the above implementation, x is the node on which the rotation is performed and y is the right child of the node x. ## Deletion in Splay treeAs we know that splay trees are the variants of the Binary search tree, so deletion operation in the splay tree would be similar to the BST, but the only difference is that the delete operation is followed in splay trees by the splaying operation.
There are two types of deletions in the splay trees: - Bottom-up splaying
- Top-down splaying
In bottom-up splaying, first we delete the element from the tree and then we perform the splaying on the deleted node.
Suppose we want to delete 12, 14 from the tree shown below: - First, we simply perform the standard BST deletion operation to delete 12 element. As 12 is a leaf node, so we simply delete the node from the tree.
The deletion is still not completed. We need to splay the parent of the deleted node, i.e., 10. We have to perform Still, node 10 is not a root node; node 10 is the left child of the root node. So, we need to perform the right rotation on the root node, i.e., 14 to make node 10 a root node as shown below: - Now, we have to delete the 14 element from the tree, which is shown below:
As we know that we cannot simply delete the internal node. We will replace the value of the node either using . Suppose we use inorder successor in which we replace the value with the lowest value that exist in the right subtree. The lowest value in the right subtree of node 14 is 15, so we replace the value 14 with 15. Since node 14 becomes the leaf node, so we can simply delete it as shown below:inorder successorStill, the deletion is not completed. We need to perform one more operation, i.e., splaying in which we need to make the parent of the deleted node as the root node. Before deletion, the parent of node 14 was the root node, i.e., 10, so we do need to perform any splaying in this case.
In top-down splaying, we first perform the splaying on which the deletion is to be performed and then delete the node from the tree. Once the element is deleted, we will perform the join operation.
Suppose we want to delete 16 from the tree which is shown below:
The node 16 is still not a root node, and it is a right child of the root node, so we need to perform left rotation on the node 12 to make node 16 as a root node. Once the node 16 becomes a root node, we will delete the node 16 and we will get two different trees, i.e., left subtree and right subtree as shown below: As we know that the values of the left subtree are always lesser than the values of the right subtree. The root of the left subtree is 12 and the root of the right subtree is 17. The first step is to find the maximum element in the left subtree. In the left subtree, the maximum element is 15, and then we need to perform splaying operation on 15. As we can observe in the above tree that the element 15 is having a parent as well as a grandparent. A node is right of its parent, and the parent node is also right of its parent, so we need to perform two left rotations to make node 15 a root node as shown below: After performing two rotations on the tree, node 15 becomes the root node. As we can see, the right child of the 15 is NULL, so we attach node 17 at the right part of the 15 as shown below, and this operation is known as a ## Note: If the element is not present in the splay tree, which is to be deleted, then splaying would be performed. The splaying would be performed on the last accessed element before reaching the NULL.
In the above algorithm, we first check whether the root is Null or not; if the root is NULL means that the tree is empty. If the tree is not empty, we will perform the splaying operation on the element which is to be deleted. Once the splaying operation is completed, we will compare the root data with the element which is to be deleted; if both are not equal means that the element is not present in the tree. If they are equal, then the following cases can occur:
Next TopicFundamental of the Data Structure |