Deletion in Splay Tree
It is recommended to refer following post as prerequisite of this post. Splay Tree | Set 1 (Search) 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
References: https://www.geeksforgeeks.org/splay-tree-set-1-insert/ http://courses.cs.washington.edu/courses/cse326/01au/lectures/SplayTrees.ppt
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
My Personal Notes arrow_drop_up
Please Login to comment...