š Tries (Prefix Trees)
You're typing "app" in a search box. Instantly, suggestions appear: "apple," "application," "apply." How does it find all words starting with "app" so fast? Meet the Trie - a tree structure designed specifically for prefix-based operations!
The Dictionary Problem
Imagine building a dictionary. You need to:
- Check if a word exists
- Find all words starting with a prefix
- Suggest corrections for misspellings
Why Not Use a Hash Table?
Hash table: Great for exact lookups, but can't efficiently find all words with prefix "app"
Trie: Designed for prefix operations - finds all matches in O(m + n) time!
What is a Trie?
Trie = Tree of Characters
A Trie (pronounced "try") is a tree where:
- Each node represents a character
- Each path from root to marked node = one word
- Shared prefixes share nodes (space efficient!)
Example: "app", "apple", "apply" share nodes aāpāp
See the Structure
Basic Operations
1. Insert
How It Works
- Start at root
- For each character in word:
- If child node exists, move to it
- If not, create new node
- Mark last node as "end of word"
Time: O(m) where m = word length
2. Search
How It Works
- Follow path for each character
- If path breaks ā word doesn't exist
- If reach end, check if marked as "end of word"
Key insight: "app" exists, but "appl" might not (even if path exists!)
Time: O(m)
3. Prefix Search (Autocomplete)
How It Works
- Navigate to prefix node
- Collect all words in that subtree
- Return as suggestions
Time: O(m + n) where m = prefix length, n = number of results
Real-World Applications
Where Tries Shine
- Autocomplete: Google search, IDE code completion
- Spell Checkers: Word processors, browsers
- IP Routing: Longest prefix matching
- Dictionary: Word games, text editors
- Phone Contacts: T9 predictive text
- DNA Sequencing: Pattern matching in genomes
The Complete Code
Click Run to execute your code
Trie vs Hash Table
Comparison
| Operation | Trie | Hash Table |
|---|---|---|
| Insert | O(m) | O(m) |
| Search | O(m) | O(m) |
| Prefix search | O(m + n) ā | O(N) ā |
| Space | O(ALPHABET Ć N Ć M) | O(N Ć M) |
Trie advantage: Prefix operations!
Trie disadvantage: More space (but shared prefixes help)
Space Optimization
Reducing Memory Usage
- Compressed Trie: Merge single-child chains
- Ternary Search Tree: Use 3-way branching
- Hash Map Children: Instead of array (saves space for sparse alphabets)
Summary
What You've Learned
Tries are specialized trees for string operations:
- Structure: Each path = one word
- Insert: Create nodes for each character - O(m)
- Search: Follow path, check end marker - O(m)
- Prefix: Navigate to prefix, collect subtree - O(m + n)
- Space: Shared prefixes save memory
- Applications: Autocomplete, spell check, IP routing
- Trade-off: More space for faster prefix operations
What's Next?
You've completed Module 9! You now understand heaps, their applications, and tries. These are powerful tools for real-world systems. Next, we can explore more advanced data structures or move to graphs and algorithms!
Enjoying these tutorials?