Blog

A Trie Makes Prefix Search Explicit

hello
A branching prefix tree guiding an input path toward ranked completion candidates

A hash map answers exact lookup well. Autocomplete asks a different question: which stored terms begin with this prefix? A trie represents each character as an edge, so reaching the prefix node takes time proportional to the prefix length rather than the number of stored terms.

From that node, traversal can enumerate completions. In practice, enumeration alone is not enough. Users expect the best few suggestions quickly, so nodes may cache top candidates ranked by popularity, recency, or a domain score. Updates then need to maintain those caches along the term’s path.

The simple implementation—one map of child nodes per character—is easy to understand and often memory-heavy. Every node carries object and map overhead, while many nodes have only one child. Compressed tries merge single-child paths; array-based or succinct representations improve locality for read-heavy dictionaries. The right choice depends on vocabulary size and update frequency.

Normalize before indexing. Case folding, Unicode normalization, accents, punctuation, and token boundaries determine whether visually similar input follows the same path. Store a display form separately from the normalized key so search behavior does not damage presentation.

Ranking deserves an explicit contract. Pure frequency can trap popular suggestions forever and expose globally common terms where tenant-specific or permission-aware results are required. Filter authorization before returning candidates, and avoid caching a list whose visibility differs by user.

Tries are strongest when prefix traversal is central and the dictionary is stable enough to justify the index. A sorted array with binary search may be simpler for a compact static set. A search engine is more suitable when completion needs fuzzy matching, language analysis, or complex ranking.

The structure solves prefix location. A complete autocomplete service still needs bounded result collection, relevance signals, privacy rules, update propagation, and latency measurement.