Search in a Trie
This lesson defines all the cases we need to consider when searching for a word in a Trie. We will also look at the implementation of this algorithm in Java.
Searching for a word in Trie
If we want to search whether a word is present in the Trie or not, then we just need to keep tracing the path in the Trie that corresponds to the characters in the word.
Case 1: Word is not present in Trie
If there is no path, as with the word “bedroom” in the below example, then we will only be able to trace till “bed”. Therefore, we return false
because there is no character path for “r” after “bed”, indicating that this word is not present in the Trie.
Case 2: Path found but isEndWord()
is not set for the last character
It returns false if the last node is not the end of the word, even if our word has been exhausted and the path is present. , For example, if we are searching for the word “be” then the path for “ being” is present. However, the value of ...