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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.