Data Structures · Visualization

Trie: Search

Trie

Searching for an exact word walks down the trie following each character of the word in order. Time: O(L), where L is the word's length. Space: O(1) extra.

Big-O: Lookup O(word length), independent of the number of words.

Trie: Search

Trie

Searching for an exact word walks down the trie following each character of the word in order. If at any point the needed character edge doesn't exist, the word isn't in the trie. If all characters are found, the word only counts as present if the final node is specifically marked 'end of word' (otherwise it might just be a prefix of some other word).

How Search Works

  1. Start at the root.
  2. For each character in the word, check if a matching child edge exists.
  3. If it doesn't exist at any point, the word is not in the trie — stop.
  4. If all characters are matched, check whether the final node is marked 'end of word'.

Search Worked Example

Input: Search for 'do' in a trie containing 'dog' and 'do'

  1. root -> d (exists) -> o (exists)
  2. All characters matched — now check the 'o' node's end-of-word flag
  3. Flag is true (since 'do' was inserted) → word found

Output: true (found)

Try your own input
Trie: Introduction
A tree where each path from the root spells out a word one character per edge — words sharing a prefix share the same starting path.
root
root
c
a
t
r
t
d
o
g
n
e
Active path End of word
Trie: Introduction
Press ▶ Run to watch it step by step.
Python · Tries in Code
def search(root, word):
    cur = root
    for ch in word:
        if ch not in cur.children:
            return False
        cur = cur.children[ch]
    return cur.is_end      # must be a full word

# O(word length)
# example: search "do"
Input: Search for 'do' in a trie containing 'dog' and 'do'

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Search Time & Space Complexity

TimeO(L), where L is the word's length
SpaceO(1) extra

When to Use Search

  • Validating whether a typed word exists in a dictionary
  • Checking if a search term matches a known product name exactly
  • Fast exact-match lookup after narrowing by prefix

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.