AI Features

Search in a Trie

This lesson defines the algorithm for a word search in a trie. It also highlights the different scenarios taken care of in the algorithm.

Search Algorithm

If we want to check whether a word is present in the trie or not, we just need to keep tracing the path in the trie corresponding to the characters in the ​word.

The logic isn’t too complex, but there are a few cases we need to take care of.

Case 1: Non-Existent Word

If we are searching for a word that doesn’t exist in the trie and is not a subset of any other word, by principle, we will find null before the last character of the word can be found.

For a better understanding, check out the illustration below:

Case 2: Word Exists as a Substring

This is the case where our word can be found as a substring of another word, but the isEndWord property for it has been set to false.

In the example below, we are searching for the word be. It is a subset of the already existing word bed, but the e node has not ...

Ask