![]() |
VOOZH | about |
In the SET 1 post on TST we have described how to insert and search a node in TST. In this article, we will discuss algorithm on how to delete a node from TST.
During delete operation we delete the key in bottom-up manner using recursion. The following are possible cases when deleting a key from trie.
- Key may not be there in TST.
Solution: Delete operation should not modify TST.- Key present as unique key (no part of key contains another key (prefix), nor the key itself is prefix of another key in TST).
Solution: Delete all the nodes.- Key is prefix key of another long key in TST.
Solution: Unmark the leaf node.- Key present in TST, having atleast one other key as prefix key.
Solution: Delete nodes from end of key until first leaf node of longest prefix key.
Explanation for delete_node function
Implementation:
1.Content of the TST before deletion: BUGS CAT CATS UP 2.Content of the TST after deletion: BUGS CATS UP