Search in a Trie
Explore how to search for words in a Trie by tracing character paths and checking word termination nodes. Understand different cases where a word may be absent or present, and learn the O(h) time complexity for search operations in a Trie.
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 ...