See the tree behind autocomplete.
A trie stores words one character at a time. Words that begin alike share the same path.
THE PREFIX TREE
Learning dictionaryOne edge. One character.
Prefix pathMatching subtreeComplete word
SEARCH TRACE
Build the dictionary
Insert cart to reuse c → a → r and add just one new node. Insert ca to mark an existing node as a complete word.
Find the prefix. Explore below it.
- Start at the root. It represents the empty prefix.
- Follow one edge per letter. Searching “car” follows c → a → r. Other branches are skipped.
- Collect words below that node. A double ring means a word ends here. “car” can be a word and still lead to “care”.
Finding a prefix of length p takes O(p) child lookups. Listing completions takes additional work to visit their branches and produce the words.
Diagram reference: USF’s Trie (Prefix Tree) visualization ↗