Trie (Prefix Tree)
A tree-like data structure used for efficient retrieval of keys in a dataset of strings.Structure
- Each node represents a single character
- The root is empty; paths from root to marked nodes form stored words
- Each node has up to 26 children (for lowercase English letters)
- A boolean flag marks the end of a word
Core Operations
Where m = length of the word/prefix.
Implementation Skeleton
Classic Problems
- Implement Trie
- Word Search II (backtracking + trie)
- Design Add and Search Words
- Replace Words
- Longest Word in Dictionary
- Auto-complete systems
When to Use
- Prefix matching and auto-complete
- Spell checkers and dictionary lookups
- IP routing (longest prefix match)
- Word games (Boggle, Scrabble solvers)
