Tries
A tree that stores words letter by letter, so words that start the same share the same path. Seven steps: build one from five words, search it, and autocomplete a prefix.
In 30 seconds
A trie stores a set of words as a tree of letters. Each node holds one character, and following a path down from the root spells out a word.
Words that begin the same way share the beginning of their path, so a common prefix is stored once. A marker on a node says “a word ends here”.
Looking a word up takes one step per letter, however many words are stored. And every word starting with a given prefix sits below one node, which is what makes autocomplete fast.
Tries, step by step
Also called a prefix tree. Each node needs a way to find its children, usually a small array or map, so tries can use a lot of memory. Radix trees save space by merging chains of single children.
- Step 1: One node per character. A trie stores words as paths through a tree. To insert “car”, start at the empty root and take one step per letter: to c, then a, then r, creating each node on the way if it is not there yet. The word is never stored in one place; it is spelled out by the path.
- Step 2: Mark where a word ends. The path c, a, r also passes through “c” and “ca”, which were never inserted. So each node carries an end-of-word marker, a yes or no flag, and insert switches it on at the last letter. Only a path that finishes on a marked node counts as a stored word.
- Step 3: Shared prefixes, stored once. Now insert “cat”. The nodes for c and a already exist, so the walk reuses them and only adds a new t. “cart” reuses c, a and r and adds one node below r. Words that start the same way share the start of their path, so a common prefix is stored once however many words use it.
- Step 4: A small dictionary. Add “do” and “dog” and the trie holds five words. They share nothing with the c-words, so they branch off at the root. Notice “do”: its last node is marked and also has a child, because one word can be the prefix of another. Fifteen letters fit in eight nodes.
- Step 5: Search follows the letters. To look up “cart”, walk from the root one letter at a time: c, a, r, t. Four letters, four steps, and the last node is marked, so the word is there. The time depends on the length of the word, not on how many words the trie holds. It would be the same four steps among a million words.
- Step 6: Two ways to miss. A search can fail in two ways. For “cab” the walk reaches a and finds no child b, so it stops early: nothing stored starts with “cab”. For “ca” every letter is found, but the walk ends on a node with no marker. “ca” is a prefix of stored words, not a word itself.
- Step 7: Autocomplete. Autocomplete is what tries do best. Walk down the typed prefix, here “ca”; every stored word that starts with it lives in the subtree below that node. Collect the marked nodes in that subtree and you have the suggestions: car, cart and cat. The d-branch is never touched.
Try it yourself
Learn this first
Spotted a mistake on this page?