A threaded binary tree node looks like following.
We have already discussed Insertion of Threaded Binary Search Tree
In deletion, first the key to be deleted is searched, and then there are differenct cases for deleting the Node in which key is found.
Case A: Leaf Node need to be deleted
In BST, for deleting a leaf Node the left or right pointer of parent was set to NULL. Here instead of setting the pointer to NULL it is made a thread.
If the leaf Node is to be deleted is left child of its parent then after deletion, left pointer of parent should become a thread pointing to its predecessor of the parent Node after deletion.
par -> lthread = true; par -> left = ptr -> left;
If the leaf Node to be deleted is right child of its parent then after deletion, right pointer of parent should become a thread pointing to its successor. The Node which was inorder successor of the leaf Node before deletion will become the inorder successor of the parent Node after deletion.
Case B: Node to be deleted has only one child
After deleting the Node as in a BST, the inorder successor and inorder predecessor of the Node are found out.
s = inSucc(ptr); p = inPred(ptr);
If Node to be deleted has left subtree, then after deletion right thread of its predecessor should point to its successor.
p->left = s;
Before deletion 15 is predecessor and 2 is successor of 16. After deletion of 16, the Node 20 becomes the successor of 15, so right thread of 15 will point to 20.
If Node to be deleted has right subtree, then after deletion left thread of its successor should point to its prredecessor.
s->left = p;
Before deletion of 25 is predecessor and 34 is successor of 30. After deletion of 30, the Node 25 becomes the predecessor of 34, so left thread of 34 will point to 25.
Case C: Node to be deleted has two children
We find inorder successor of Node ptr (Node to be deleted) and then copy the information of this successor into Node ptr. After this inorder successor Node is deleted using either Case A or Case B.
Below is Complete code:
10 13 14 16 17 5 30
This article is contributed by Anuj Chauhan. 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.
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Threaded Binary Tree
- Threaded Binary Tree | Insertion
- Reverse Morris traversal using Threaded Binary Tree
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
- Binary Search Tree | Set 1 (Search and Insertion)
- Count the Number of Binary Search Trees present in a Binary Tree
- Binary Tree to Binary Search Tree Conversion
- Binary Tree to Binary Search Tree Conversion using STL set
- Difference between Binary Tree and Binary Search Tree
- Floor in Binary Search Tree (BST)
- Make Binary Search Tree
- Binary Search Tree | Set 2 (Delete)
- Optimal Binary Search Tree | DP-24