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
- Insertion in a Trie recursively
- Trie | (Insert and Search)
- Recursively remove all adjacent duplicates
- Count occurrences of a substring recursively
- Decode a string recursively encoded as count followed by substring
- Recursively Reversing a linked list (A simple implementation)
- Find middle of singly linked list Recursively
- Repeatedly search an element by doubling it after every successful search
- Trie | (Delete)
- Boggle | Set 2 (Using Trie)
- Implement a Dictionary using Trie
- Bottom-up traversal of a Trie
- Trie | (Display Content)
- Persistent Trie | Set 1 (Introduction)
- Why is Binary Search preferred over Ternary Search?
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai