It is recommended to refer following post as prerequisite of this post.
Following are the different cases to delete a key k from splay tree.
- If Root is NULL: We simply return the root.
- Else Splay the given key k. If k is present, then it becomes the new root. If not present, then last accessed leaf node becomes the new root.
- If new root’s key is not same as k, then return the root as k is not present.
- Else the key k is present.
- Split the tree into two trees Tree1 = root’s left subtree and Tree2 = root’s right subtree and delete the root node.
- Let the root’s of Tree1 and Tree2 be Root1 and Root2 respectively.
- If Root1 is NULL: Return Root2.
- Else, Splay the maximum node (node having the maximum value) of Tree1.
- After the Splay procedure, make Root2 as the right child of Root1 and return Root1.
Preorder traversal of the modified Splay tree is 2 1 6 9 7
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Splay Tree | Set 2 (Insert)
- Splay Tree | Set 1 (Search)
- Red-Black Tree | Set 3 (Delete)
- Delete Operation in B-Tree
- K Dimensional Tree | Set 3 (Delete)
- Write a program to Delete a Tree
- Delete the last leaf node in a Binary Tree
- Deleting a binary tree using the delete keyword
- Non-recursive program to delete an entire binary tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Trie | (Delete)
- Delete leaf nodes with value as x
- Delete leaf nodes with value k
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Check if max sum level of Binary tree divides tree into two equal sum halves
Improved By : nidhi_biet