Trie (prefix tree)
medium 20 minA trie (pronounced "try") is a tree that stores strings one character per level, so words with the same opening letters share the same path from the root.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
After inserting "app" and "apple" into an empty trie, how many nodes are there, including the root?
What is the time complexity of searching a trie for a word of length m?
Why use a trie rather than a hash set for a word list?
The trie holds cat, car, card and care. What does search("ca") return?
The trie holds car, card and care. You delete "car". Which nodes are removed?
What is the tightest bound on the number of nodes for a word set W?
Autocomplete for prefix p of length m returns words whose letters below the prefix node total k. What does it cost?
Which is a reason to choose a 26-slot array for children over a dict?
In Word Search II, what lets the grid DFS stop early?
Which task is a poor fit for a trie?
Pick an answer to see why.
Not quite. root, a, p, p, l, e: six nodes. "apple" reuses the a-p-p path of "app" and adds only l and e.
Not quite. We traverse exactly one node per character, so m steps, independent of the number of stored words.
Not quite. A trie enables prefix operations that a hash set can only do by scanning every word. Exact lookups cost O(L) in both, and a trie often uses more memory.
Not quite. The path exists, but ca was never inserted, so the a node's is_end is False. starts_with("ca") would be True.
Not quite. r still leads to card and care, so it must stay. Delete only clears its is_end flag.
Not quite. Each character adds at most one node, plus the root. Shared prefixes reduce the count further.
Not quite. O(m) to walk to the prefix node, then O(k) for the DFS over its subtree.
Not quite. The array gives a direct index and natural sorted order, at the cost of 26 slots per node and a fixed alphabet.
Not quite. If the path so far is not a prefix of any word, the trie has no matching child and that branch is cut.
Not quite. Exact membership with no prefix questions is simpler and smaller with a hash set.
Flashcards
Say your answer, then reveal it.
Card 1 of 11
What is a trie?
A tree that stores strings one character per level. Each root-to-node path spells a prefix, and words with a common prefix share nodes.
Say your answer, then reveal it.
What two fields does a trie node have?
children (a dict from letter to node, or a 26-slot array) and is_end (True if a word ends at this node).
Say your answer, then reveal it.
What does the TRIE mnemonic stand for?
Tree for Retrieval, Insert char by char, End marks word completion.
Say your answer, then reveal it.
What is the only difference between search and starts_with?
search also checks is_end on the last node; starts_with only needs the path to exist.
Say your answer, then reveal it.
Time of insert, search and starts_with?
O(L) for a string of length L, one child lookup per character. It does not depend on how many words are stored.
Say your answer, then reveal it.
How much space does a trie use?
At most one node per stored character, O(total characters), less when prefixes are shared. Array nodes multiply that by the alphabet size.
Say your answer, then reveal it.
Dict children vs a 26-slot array?
Dict: any alphabet, pays only for existing children, O(1) average. Array: exact O(1) index and alphabetical order, but 26 slots per node, mostly empty.
Say your answer, then reveal it.
When can delete remove a node?
Only when it is not the end of another word and has no children. Otherwise just clear is_end and stop pruning.
Say your answer, then reveal it.
How does autocomplete work on a trie?
Walk the prefix to its node (O(m)), then DFS the subtree and collect every node marked as an end (O(k)). Rank by a frequency stored on end nodes.
Say your answer, then reveal it.
Why a trie for Word Search II?
One grid DFS guided by a trie of all words finds every word at once and stops as soon as the path is not a prefix of any word.
Say your answer, then reveal it.
What is a good picture for a trie?
A phone book: names grouped by first letter, then by second, and so on. Finding a name is following the path letter by letter.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.