Advanced Structures
Tries (prefix trees)
Character-by-character lookup for strings
Every time your phone finishes a word before you do, something is walking a trie. It is a tree where edges are letters and the path from the root spells a word - so all the words that begin the same way share the same opening corridor and only fork where they truly differ. That one idea turns "find every word starting with car" from a scan of the whole dictionary into a three-step walk that hands you a subtree. In this lesson you will type into a live trie and watch the matching path - and the candidate list - update on every keystroke.
One shared spine of letters
One edge per character, plus a flag
A trie node holds a small map from the next character to a child node - nothing more. To find or store a key you start at the root (which represents the empty string) and follow one edge per character. Two subtleties make it work:
- The root is empty. It carries no letter; it is just the shared starting point every word walks out from.
- End-of-word markers. Because car is a prefix of card, the path for card passes straight through the r node. So each node carries a boolean: "a complete word ends here." Without it you couldn't tell a real word from a mere waypoint.
Cost tracks key length, not dataset size
Every operation walks the trie one character at a time, so the work is proportional to the length of the key - and is completely independent of how many keys are already stored:
A balanced BST spends comparisons and each comparison may re-scan the whole key; a hash table hashes the whole key too. A trie instead does one cheap branch per character - and, uniquely, gives you every completion of a prefix essentially for free, because they all sit in a single subtree:
Autocomplete you can feel
Type a prefix into the search box and watch the path from the root light up one node per keystroke, while every word living in the subtree below drops into the completions panel. Click a completion to drill in. Then add a word of your own and watch the branch grow - reused characters glow amber (no duplication), brand-new ones glow green.
completions
Type a prefix above - the matching path lights up and the words below it appear.
A prefix query is navigation, then a subtree
Speed has a price: pointers everywhere
The insert, in a handful of lines
Both insert and prefix-search fall straight out of the definition - walk character by character, creating a child only when one doesn't already exist, and mark the final node:
class Node:
def __init__(self):
self.kids = {} # char -> Node
self.end = False # does a word end here?
def insert(root, word):
node = root
for ch in word:
if ch not in node.kids: # no branch for this letter yet?
node.kids[ch] = Node() # grow one (existing letters are REUSED)
node = node.kids[ch] # step down the shared path
node.end = True # flag the last node end-of-word
def starts_with(root, prefix):
node = root
for ch in prefix:
if ch not in node.kids:
return [] # path breaks -> no such prefix
node = node.kids[ch]
return collect(node, prefix) # DFS the subtree for completionsCheck yourself
What determines how long it takes to search a trie for a key?
In a trie, how are two words that share a common prefix stored?
Recall: what makes a trie's lookup independent of how many keys it stores - and why is an end-of-word marker necessary?
Recall the loop invariant. Try to state it, then check.
Lock it in
- A trie is a tree whose edges are letters: words sharing a prefix share one path and fork only where they differ, so the common stem is stored exactly once.
- Every operation walks one edge per character, so insert, search, and prefix queries cost in the key length - independent of how many keys are stored.
- The root is empty and each node carries an end-of-word flag; without it you cannot tell a stored word from a mid-path waypoint like car inside card.
- A prefix query is navigation then a subtree, which is why autocomplete and routing tables reach for tries; the price is many pointers, clawed back by shared prefixes and radix compression.
Primary source
Ask your teacher