Trie is an efficient information retrieval data structure. Using Trie, search complexities can be brought to an optimal limit (key length).
The task is to search a string in a Trie using recursion.
root / \ t a | | h n | | \ e s y / | | i r w | | | r e e | r Input : str = "anywhere" Output : not found Input : str = "answer" Output : found
Searching a key is similar to insertion operation, however, we only compare the characters and move down. The search can terminate due to the end of a string or lack of key in the trie. In the former case, if the endOfWord field of the last node is true, then the key exists in the trie. In the second case, the search terminates without examining all the characters of the key, since the key is not present in the trie.
Below is the implementation of the above approach :
not found found
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.
- Insertion in a Trie recursively
- Trie | (Insert and Search)
- Recursively remove all adjacent duplicates
- Count occurrences of a substring recursively
- Find middle of singly linked list Recursively
- Recursively Reversing a linked list (A simple implementation)
- Decode a string recursively encoded as count followed by substring
- Repeatedly search an element by doubling it after every successful search
- Boggle | Set 2 (Using Trie)
- Trie | (Delete)
- Why is Binary Search preferred over Ternary Search?
- Persistent Trie | Set 1 (Introduction)
- Implement a Dictionary using Trie
- Trie | (Display Content)
- Bottom-up traversal of a Trie
- Advantages of Trie Data Structure
- Counting the number of words in a Trie
- Auto-complete feature using Trie
- Trie memory optimization using hash map
- Pattern Searching using a Trie of all Suffixes
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai