During delete operation we delete the key in bottom up manner using recursion. The following are possible conditions when deleting key from trie,
- Key may not be there in trie. Delete operation should not modify trie.
- Key present as unique key (no part of key contains another key (prefix), nor the key itself is prefix of another key in trie). Delete all the nodes.
- Key is prefix key of another long key in trie. Unmark the leaf node.
- Key present in trie, having atleast one other key as prefix key. Delete nodes from end of key until first leaf node of longest prefix key.
The below code presents algorithm to implement above conditions.
Yes No Yes
— Venki. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Trie | (Insert and Search)
- Auto-complete feature using Trie
- Longest prefix matching - A Trie based solution in Java
- Pattern Searching using a Trie of all Suffixes
- Find shortest unique prefix for every word in a given list | Set 1 (Using Trie)
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- Longest Common Prefix using Trie
- Count of distinct substrings of a string using Suffix Trie
- Boggle | Set 2 (Using Trie)
- Trie memory optimization using hash map
- Trie | (Display Content)
- Counting the number of words in a Trie
- Sorting array of strings (or words) using Trie
- Word Break Problem | (Trie solution)
- Sorting array of strings (or words) using Trie | Set-2 (Handling Duplicates)
- Advantages of Trie Data Structure
- Persistent Trie | Set 1 (Introduction)
- Program for assigning usernames using Trie
- Bottom-up traversal of a Trie
- Check if the given Trie contains words starting from every alphabet