Search in a Trie

Learn the algorithm for a word search in a trie. This lesson highlights the different scenarios which are taken care of in the algorithm.

Search algorithm

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

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

Case 1: Non-Existent word

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

For a better understanding, check out the illustration below:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.