VOOZH about

URL: https://www.geeksforgeeks.org/dsa/insertion-in-splay-tree/

⇱ Insertion in Splay Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Insertion in Splay Tree

Last Updated : 23 Jul, 2025

It is recommended to refer following post as prerequisite of this post.
Splay Tree | Set 1 (Search)
As discussed in the previous post, Splay tree is a self-balancing data structure where the last accessed key is always at root. The insert operation is similar to Binary Search Tree insert with additional steps to make sure that the newly inserted key becomes the new root.
Following are different cases to insert a key k in splay tree.
1) Root is NULL: We simply allocate a new node and return it as root.
2)Splay the given key k. If k is already present, then it becomes the new root. If not present, then last accessed leaf node becomes the new root.
3) If new root's key is same as k, don't do anything as k is already present.
4) Else allocate memory for new node and compare root's key with k. 
.......4.a) If k is smaller than root's key, make root as right child of new node, copy left child of root as left child of new node and make left child of root as NULL. 
.......4.b) If k is greater than root's key, make root as left child of new node, copy right child of root as right child of new node and make right child of root as NULL.
5) Return new node as new root of tree.
Example:


100 [20] 25
/ \ \ / \
50 200 50 20 50
/ insert(25) / \ insert(25) / \
40 ======> 30 100 ========> 30 100
/ 1. Splay(25) \ \ 2. insert 25 \ \
30 40 200 40 200
/
[20]


Output: 

Preorder traversal of the modified Splay tree is
25 20 50 30 40 100 200

This is an implementation of a Splay Tree data structure, which is a self-balancing binary search tree with the ability to bring the most recently accessed node to the root of the tree. The code defines a node class and several utility functions to perform operations on the tree, such as left and right rotations and insertion of new nodes.

The splay function takes a node pointer root and an integer key, and it returns a pointer to the node that contains the key after performing a splay operation. The splay operation brings the node with the key to the root of the tree, or the last accessed node if the key is not found in the tree. The splay operation is performed by rotating the tree around a node in a zig-zag or zig-zig pattern until the target node is at the root.

The insert function takes a node pointer root and an integer k, and it returns a pointer to the root of the updated tree after inserting the new key k. The function first performs a splay operation on the node with the closest value to k and then adds a new node containing k to the tree, as a child of the splayed node.

The preOrder function performs a preorder traversal of the tree and prints the keys of each node in the order they are visited.

The code in the main function creates a sample tree, performs an insertion operation with the insert function, and then prints the preorder traversal of the modified tree with the preOrder function.


This article is compiled by Abhay Rathi.

Comment