What is a Trie?
A Trie (pronounced "try", from retrieval) is a tree-like data structure for storing strings where each node represents a character.
The Phone Book Analogy: Imagine a phone book organized by first letter, then second letter, etc.:
- •Looking up "Smith" → S section → Sm section → Smi → Smit → Smith
- •Once you're in the "Sm" section, you've eliminated all names not starting with "Sm"
Visual Structure:
Key Properties:
- •Root is empty (represents empty prefix)
- •Each edge represents a character
- •Path from root = prefix
- •
isEndflag marks complete words - •Shared prefixes share nodes (memory efficient for similar words)