← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Tries (Prefix Trees)

Data Structures PHP 5.0+ Advanced
debt(d7/e5/b3/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints indicate no automated tooling exists for this concept — the code_pattern note says a LIKE 'term%' database query is a candidate for a trie, but spotting this misuse requires a developer familiar with data structures to review query patterns or notice performance degradation in production. No linter or SAST tool from the term's detection_hints can flag this.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix describes implementing a trie with PHP arrays per node — replacing a database LIKE query or hash map with a trie (or vice versa) requires building the data structure, migrating the data population logic, and updating query/search call sites. This is more than a one-line patch but typically contained within one component or feature area.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). A trie implementation in PHP is a localised data structure choice — it applies to a specific autocomplete or prefix-search feature within web or CLI contexts, not broadly across the codebase. Future maintainers of that feature pay the tax (understanding trie structure, terminal node marking, memory considerations) but the rest of the codebase is largely unaffected.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception field calls out that developers assume a hash map is always better for string lookup, missing that tries support prefix search which hash maps cannot do efficiently. Common mistakes reinforce additional gotchas: not marking terminal nodes (confusing prefixes with stored words), memory waste from naive 26-element arrays, and case-sensitivity issues. These are documented traps that developers reliably stumble into.

About DEBT scoring →

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)
}

Added 16 Mar 2026
Edited 22 Mar 2026
Views 37
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 2 pings S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 6 Perplexity 4 Ahrefs 4 Scrapy 3 Google 2 Unknown AI 2 SEMrush 2 Claude 1 Bing 1 Meta AI 1 Sogou 1
crawler 25 crawler_json 2
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


✓ schema.org compliant