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 email@example.com. 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Splay Tree | Set 1 (Search)
- Splay Tree | Set 2 (Insert)
- K Dimensional Tree | Set 3 (Delete)
- Delete Operation in B-Tree
- Red-Black 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 k
- Delete leaf nodes with value as x
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
Improved By : nidhi_biet