Module A-10·16 min read

Prefix trees for O(L) lookups — autocomplete, wildcard search, and why a trie beats a hash map for prefix queries.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module A-10 — Tries: Prefix Trees and Autocomplete

What this module covers: Module F-4's hash map answers "does this exact key exist" in O(1). It has no good answer at all for "what keys start with this prefix" — that requires scanning every key. A trie is a tree built specifically to make prefix queries fast, by encoding shared prefixes as shared paths through the tree. This module covers the structure, its core operations, and autocomplete — the application a trie is built for.


The Structure: Shared Prefixes Are Shared Paths

A trie (from retrieval, though commonly pronounced "try" to avoid confusion with "tree") is a tree where each edge represents one character, and any path from the root spells out a prefix. Words that share a prefix — "car", "card", "care" — literally share the same path through the tree for their common letters, only branching apart where they first differ.

typescript

"car", "card", and "care" inserted into the same trie produce a single shared path for c → a → r, with the tree only branching into three separate continuations (nothing further, d, or e) at that point — the shared prefix is stored exactly once, no matter how many words share it. isEndOfWord matters specifically because a word can be a prefix of another word ("car" is a prefix of "card") — without this flag, there'd be no way to tell whether the path to a given node represents a complete, insertable word or just an intermediate letter on the way to a longer one.


Search and StartsWith: Both Just a Walk Down the Tree

typescript

Both operations are the identical walk, differing only in the final check: search additionally requires isEndOfWord (this exact string was inserted as a complete word), while startsWith only cares that the path exists at all (some word in the trie begins this way). Both cost O(L), where L is the length of the string being searched — critically, independent of how many words the trie holds. This is the trie's core advantage over a hash map (Module F-4): a hash map's O(1) lookup only answers "does this exact key exist" — it has no efficient way at all to answer "what keys start with this prefix," short of checking every single key it holds, which is O(n × L) in the worst case. A trie answers the prefix question in the same O(L) it takes to answer the exact-match question, because the tree structure itself is organized by shared prefixes.


Autocomplete: Walk to the Prefix, Then Collect Everything Below It

Autocomplete needs "every word starting with this prefix," not just "does this prefix exist." The fix: walk to the prefix's node (exactly startsWith's walk), then run a traversal (Module P-4) from that point, collecting every complete word found in the subtree beneath it.

typescript

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.