On this page
A trie stores words the way you read them: one letter at a time, along a path from a root, so words that begin the same way share the start of their path.
What is a trie (prefix tree)?
A trie is a tree for storing strings in which there is one node for every common prefix, according to NIST's Dictionary of Algorithms and Data Structures. The name comes from “retrieval”. Edward Fredkin, who named it, described the structure in Trie memory, published in Communications of the ACM in September 1960.
Each node holds links to its children, one for each possible next letter, plus a flag that says “a word ends here”. Without the flag you could not tell that CAR is a stored word when CART is stored too.
How do you insert words into a trie?
Start with an empty root and insert four words in this order.
CAT. The root has no C child, so create C. C has no A, so create A. A has no T, so create T and mark it as a word end. Three new nodes.
CAR. Follow C, then A; both exist. A has no R child, so create R and mark it. One new node.
CART. Follow C, A, R. R has no T child (the T under A is a different node), so create T under R and mark it. One new node.
DOG. The root has no D, so create D, O and G, and mark G. Three new nodes.
The four words contain 13 letters between them, but the trie needs only 8 nodes below the root. CAT, CAR and CART share C and A, and CART reuses the whole of CAR. Notice that R is both a word end and a step on the way to CART.
Why is trie lookup O(length), not O(number of words)?
To look up CART you take four steps, C, A, R, T, then check the word-end flag. If a child is missing at any step you stop and answer “not stored”. A word of length m costs m steps whatever else the trie holds: with four words or four lakh, finding CART takes four steps, provided each step finds its child in constant time, for example from an array indexed by letter.
Binary search in a sorted list of n words needs about log₂ n comparisons, and each comparison may read several letters; our guide to Big O notation and growth rates shows why that gap widens with n. A hash table finds whole words quickly on average, but it scatters keys, so it cannot answer “which words start with CA?” without checking every key.
Prefix search is where a trie earns its place. Walk C, then A; every word below that node starts with CA. Reaching the node costs O(p) for a prefix of length p, and listing results costs time in proportion to the part of the subtree you visit.
How much memory does a trie use?
If every node keeps an array of 26 child links, one per letter, our trie of 9 nodes (counting the root) holds 9 × 26 = 234 link slots and uses only 8 of them. Two common fixes:
Store children sparsely, in a short list or small hash map per node. Memory drops; each step costs a little more.
Compress single-child chains into one edge labelled with a string. NIST's trie entry lists the Patricia tree and the compact trie as kinds of trie.
Compressed, our trie becomes: root to “CA”, which branches to “T” (a word) and “R” (a word); R has one child “T” (a word); and root to “DOG” (a word). That is 5 nodes below the root instead of 8. Donald Morrison's PATRICIA paper (Journal of the ACM, 1968) bounds retrieval cost linearly by key length and the number of key occurrences, otherwise independent of library size.
How does autocomplete use a trie?
Type “ca” into a search box and an autocomplete feature can walk to the CA node and collect the words below it: CAR, CART, CAT. Visiting children from A to Z returns them in alphabetical order with no extra sorting.
Can you build a trie on paper?
Insert TEA, TEN, TO, IN and INN into an empty trie. Count the nodes below the root.
Which node is both a word end and a step to a longer word?
Compress the trie. How many nodes are left?
Answers: 8 nodes for 13 letters; the N under I, because IN leads to INN; 7 nodes, because only I and N merge into one edge “IN”.
If the word-end flag or the compression rule still feels shaky, the Fermi Concept Library covers concepts in computing alongside physics, chemistry, biology and mathematics. Next, see how a binary heap keeps a priority queue in order.



