Tries (Prefix Trees)
Also Known As
prefix tree
digital tree
radix tree
autocomplete
TL;DR
A tree where each path from root to node spells a prefix — enabling O(m) insert, lookup, and prefix search where m is the string length, regardless of dictionary size.
Explanation
A Trie (from re-TRIE-val) stores strings character by character — each node represents one character, and a path from root to a terminal node spells a complete word. Lookup and insert are O(m) where m is the string length, independent of the number of stored strings. Prefix search (all words starting with 'pre') is O(m + k) where k is the number of matches. Applications: autocomplete, spell checkers, IP routing tables, and dictionary implementations. Space can be improved with compressed tries (Patricia/Radix trees).
Common Misconception
✗ A hash map is always better than a Trie for string lookup — hash maps are O(1) average for exact lookup; Tries are O(m) but support prefix search which hash maps cannot do efficiently.
Why It Matters
Autocomplete features that suggest completions for partial input require prefix search — a Trie does this natively, while a sorted array requires binary search and a hash map requires scanning all keys.
Common Mistakes
- Using a Trie when only exact lookups are needed — a hash map is simpler and faster.
- Not marking terminal nodes — the Trie cannot distinguish between 'car' as a stored word vs 'car' as a prefix of 'card'.
- Naive Trie with 26-element arrays per node — wastes memory; use a hash map per node for sparse alphabets.
- Forgetting that Trie lookup is case-sensitive by default — normalise input.
Code Examples
✗ Vulnerable
// Linear scan for prefix search — O(n * m):
$words = ['apple','application','apply','banana','band'];
$prefix = 'app';
$results = array_filter($words, fn($w) => str_starts_with($w, $prefix));
// O(n) scan for every prefix query — unusable at scale
✓ Fixed
// Trie — O(m) prefix search regardless of dictionary size:
class TrieNode {
public array $children = [];
public bool $isEnd = false;
}
class Trie {
private TrieNode $root;
public function __construct() { $this->root = new TrieNode(); }
public function insert(string $word): void {
$node = $this->root;
foreach (str_split($word) as $char) {
$node->children[$char] ??= new TrieNode();
$node = $node->children[$char];
}
$node->isEnd = true;
}
// search(), startsWith(), getSuggestions() all O(m)
}
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
22 Mar 2026
Views
19
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
Amazonbot 6
Perplexity 4
Google 2
Unknown AI 2
Ahrefs 2
How they use it
crawler 15
crawler_json 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: High
⚡ Quick Fix
Use a trie when you need fast prefix lookups (autocomplete, spell check) — each node is a PHP array of children, O(m) insert/search where m is the key length
📦 Applies To
PHP 5.0+
web
cli
🔗 Prerequisites
🔍 Detection Hints
Autocomplete or prefix search implemented with LIKE 'term%' database query — candidate for trie or search engine
Auto-detectable:
✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: Medium
✗ Manual fix
Fix: High
Context: Class
Tests: Update