Data Structures19 sections · 729 units
Open in Course

Trie Search Operation

Following the path

Searching checks if the complete path exists and ends at a word:

def search(word):
    node = root
    for char in word:
        if char not in node.children:
            return false
        node = node.children[char]
    return node.isEndOfWord

If any character is missing, the word doesn't exist. If you reach the end but isEndOfWord is false, the path exists as a prefix of some other word, but the word itself isn't in the trie.

Example: trie contains "card". Search for "car" follows c→a→r, but isEndOfWord is false (only "card" ends at d). Search returns false.

Time: O(L)O(L) where LL is word length.