All articles
· 9 min deep-divedata-structuresalgorithms
Article 1 in your session

Tries — Prefix Trees for Autocomplete, Spell Check, and IP Routing

A scroll-driven visual deep dive into trie data structures. From basic prefix trees to compressed tries and radix trees — with real-world applications in autocomplete, spell checking, IP routing tables, and T9 keyboard input.

Introduction 0%
Introduction
🎯 0/3 0%

🔡

Type one letter.
Get a thousand suggestions.

How does Google Autocomplete return suggestions in under 100ms across billions of queries? How does your phone keyboard predict the next word? The answer is a trie (pronounced “try”) — a tree where each path from root to node spells out a word prefix. Search for any prefix in O(k) time where k is the prefix length, independent of the dictionary size.

↓ Scroll to build the data structure behind every search bar

Trie Structure

A Tree of Characters

rootabpaap★ “app”t★ “apt”t★ “bat”r★ “bar”l→ e ★ “apple”
A trie storing the words: 'app', 'apple', 'apt', 'bat', 'bar'
Operations

Time complexity — independent of dictionary size

✏️ Insert

Walk from the root, creating child nodes for each character as needed, then mark the final node as a word end (★). The cost depends only on the word length — adding to a 10-word trie and a 10-million-word trie takes the same time.

O(k) — k = word length
🔍 Exact Search

Follow edges character by character from root. If the full path exists AND the last node is marked as a word end, the word is found. If any character is missing, the word doesn't exist — no further searching needed.

O(k) — k = word length
🔮 Prefix Search (Autocomplete)

Walk k edges to reach the prefix node, then collect all complete words below it via DFS or BFS. Typing 'ap' instantly finds 'app', 'apple', 'apt' — the trie structure naturally groups words by prefix.

O(k + p) — k to reach prefix, p to collect results
🗑️ Delete

Walk to the word's end and unmark it as a word end. Then backtrack toward the root, removing any nodes that no longer have children or word-end markers — cleaning up unused branches.

O(k) — k = word length
💡 Key Insight

Every operation is O(k) where k is the word length, completely independent of n (the number of words stored). A trie with 1 million words searches exactly as fast as one with 10 words — only the query length matters.

↑ Answer the question above to continue ↑
🟢 Quick Check Knowledge Check

You have a trie containing 10 million English words. You search for the word 'algorithm' (9 characters). How many nodes do you visit?

Compressed Tries

Space Optimization: Compressed Tries and Radix Trees

Standard TrieMany single-child nodes = wasted memoryr → o → m → a → n → c → e n → t → i → cWords: “romance”, “romantic”Nodes: 11 (1 per character)Many nodes with only 1 childMemory: 11 × node_sizeCompressed (Radix) TrieSingle-child chains merged into one edge”roman” → “ce” → “tic”Words: “romance”, “romantic”Nodes: 4 (branch at ‘n’)Every node has ≥2 children or is word endMemory: 4 × node_size (64% reduction)
Standard trie vs compressed trie: merge single-child chains into single nodes
↑ Answer the question above to continue ↑
🟡 Checkpoint Knowledge Check

You're building autocomplete for a search engine with 2 billion queries stored. A user types 'how t'. How should you design the system?

Real-World Uses

Tries in Production Systems

🔍 Autocomplete• Google search suggestions• IDE code completion• Command palette (VS Code, Slack)Sub-100ms response required🌐 IP Routing• Longest prefix match (LPM)• Router forwarding tables• CIDR block matchingEvery internet packet uses a trie📖 Spell Checking• Dictionary lookup in O(k)• Suggest corrections (edit distance)• Word frequency rankingGrammarly, browser spellcheck📱 T9 / Swipe Keyboards• Map key sequences to words• Predictive text input• Next-word predictionSwiftKey, Gboard, old Nokia T9
Tries power autocomplete, routing, spell-checking, and text input systems worldwide
↑ Answer the question above to continue ↑
🔴 Challenge Knowledge Check

A standard trie storing 10 million English words wastes massive memory because most internal nodes have only 1 child. What optimization reduces space by 80%+ while preserving O(k) lookup?

🎓 What You Now Know

Tries store strings as paths in a tree — each edge is a character, each node represents a prefix. Search time depends on word length, not dictionary size.

Prefix search is uniquely efficient in tries: O(k + p) — k to reach the prefix node, p to enumerate results. Hash maps cannot do this.

Compressed tries reduce memory by merging single-child chains — turning O(total characters) space to O(unique prefixes) space.

Tries power autocomplete, IP routing, spell checking, and predictive text — every search bar and every internet router uses one.

Tries are a specialized form of trees. For general hierarchical data, check out the Trees article. For hash-based lookups without prefix support, see Hash Tables. ⚡

Keep Learning