Trie explained in plain terms means understanding how autocomplete can suggest a word before you finish typing it. The central question is how a system can find every word that starts with a few letters without scanning a dictionary, and the answer is a tree of prefixes: a structure that shares the beginning of words, so that finding a prefix means walking down a single path.
A trie, pronounced try, is a tree data structure that stores strings by their prefixes, with each edge labelled by a character and each node representing the prefix formed by the path from the root. It is the structure behind autocomplete, spell checking, and IP routing tables.
The idea is simple: words that start the same way share the same path, so looking up a word costs only its own length. This article explains what a trie is, how it works, and where it is used.
Key takeaways
- A trie stores words by their shared prefixes in a tree.
- Each edge is a character, and each node is the prefix on its path.
- Looking up a word costs only the length of the word.
- Finding every word with a given prefix is a walk down one path.
- It suits autocomplete, spell checking, and routing tables.
What a trie is
A trie has one node for each distinct prefix of the words it stores. The root represents the empty prefix, its children represent one-letter prefixes, and so on down to the full words, which are marked as complete. Words that share a beginning share the nodes along that beginning, so the structure stores the shared part only once.
The name comes from retrieval, and it is pronounced try to distinguish it from tree. The structure was described by Edward Fredkin in 1960, though the idea of a prefix tree is older, and it has been a standard tool for string problems ever since.
A trie trades memory for speed. It can use more space than a sorted list of words, because it stores a node for every prefix, but it answers prefix questions in time proportional to the length of the prefix, whatever the size of the dictionary. Our guide to hashing explains a different way to look up words quickly, and our guide to Elasticsearch explains a search engine that uses prefix structures for suggestions.
How a trie works
Inserting a word walks down from the root, following the edge for each character, and creates a node whenever the edge is missing. At the end of the word, the final node is marked as the end of a word, so the trie can tell a complete word from a prefix of a longer one.
Searching for a word follows the same walk. If every character has an edge and the final node is marked, the word is present. If an edge is missing at any point, the word is absent, and the search stops early without reading the rest of the word.
Finding every word with a given prefix means walking to the node for that prefix and then listing everything below it. That is autocomplete: type a few letters, walk to their node, and return the words in the subtree, usually the most popular first.
Deleting a word unmarks its final node and removes any nodes that are no longer needed. A node can be removed only when it has no children and is not the end of another word, so deleting “car” leaves “card” intact, because the shared nodes still have a purpose.
Compressed tries save space by merging chains of nodes that have only one child. A radix tree, or Patricia trie, collapses a long chain into a single edge labelled with several characters, which keeps the lookup speed while using far less memory.

Comparing a trie with other word structures
A trie is one of several ways to store and search words, and each was built with a different emphasis. A hash table answers exact lookups in constant time but cannot find prefixes, a sorted list can binary search but cannot list a prefix quickly, and a suffix tree finds substrings anywhere, but each approach trades one strength for another.
The table below sets out the main contrasts between the leading approaches.
| Aspect | Trie | Hash table | Sorted list |
|---|---|---|---|
| Exact lookup | Time of the word’s length | Constant, on average | Logarithmic, by binary search |
| Prefix search | Walk to the prefix, list below | Not supported | Slow, must scan a range |
| Autocomplete | Natural, list the subtree | Not supported | Possible, with extra work |
| Memory | High, a node per prefix | Moderate | Low, the words only |
| Best for | Autocomplete and routing | Exact-match lookups | Small, static word lists |
The practical result is that a trie is the right choice for autocomplete and prefix search, a hash table suits exact lookups, and a sorted list fits a small dictionary that rarely changes.
Where tries are used
Autocomplete in search boxes and editors is the classic use of a trie. As the user types, the system walks the trie to the typed prefix and offers the completions below it, which is why suggestions appear before the word is finished.
Spell checkers use tries to find near matches. When a word is not in the dictionary, the checker walks the trie looking for words a small edit away, which is far cheaper than comparing the word against every entry.
IP routing tables are tries of addresses. A router stores network prefixes in a binary trie, and a packet is forwarded by the longest prefix that matches its destination, which is a walk down the trie to the deepest matching node.
Phone keyboards use tries for predictive text. The keyboard keeps a trie of the words a user types, and as each letter arrives it narrows the subtree, offering the words that are most likely to come next.
DNA sequence search uses tries to find patterns in genomes. A trie of short sequences lets a researcher find every occurrence of a pattern quickly, which is essential when the text being searched is billions of letters long.
Strengths and limits
The greatest strength of a trie is that lookup time depends only on the word, not on the dictionary. A trie with a million words answers a lookup as fast as one with a thousand, which is why it scales so well for autocomplete.
It is also a strength that a trie answers prefix questions directly. Finding every word that starts with a prefix is a single walk, not a scan, which no hash table can offer.
The limits are real, though. A trie uses more memory than a list of words, because it stores a node for every prefix, and it is a poor fit for data that is not made of strings. For exact lookups on a small set, a hash table is simpler and cheaper.
Trie checklist
- Use a trie when the questions are about prefixes, not just whole words.
- Mark word endings; a prefix is not a word unless it is marked.
- Compress long single-child chains with a radix tree when memory matters.
- Order completions by popularity, not alphabetically, for autocomplete.
- Delete carefully; only remove nodes that nothing else needs.
- Keep the trie in memory for speed; it is a poor fit for disk.
- Test with real typing patterns; short prefixes return huge subtrees.
- Combine with a hash table when you also need exact lookups.
- Remember that a trie stores strings; other data needs a different structure.
Frequently asked questions
What is a trie in simple terms?
It is a tree that stores words by their prefixes, so words sharing a beginning share the same path, and finding a word costs only its length.
Why is it pronounced try?
Because the name comes from retrieval, and the pronunciation distinguishes it from an ordinary tree.
What is the difference between a trie and a hash table?
A hash table finds whole words in constant time but cannot search by prefix, while a trie finds prefixes naturally but uses more memory.
What is a radix tree?
It is a compressed trie that merges chains of single-child nodes into one edge, saving memory while keeping the same lookup speed.
How does a trie power autocomplete?
By walking to the node for the typed prefix and listing the words in the subtree below it, usually in order of popularity.
When should I not use a trie?
When the data is not strings, when memory is very tight, or when only exact lookups are needed and prefixes do not matter.
The bottom line
Trie explained in one line is this: share the beginnings of words, and finding a prefix costs only its length.
It is the structure behind every suggestion that appears before you finish typing, and the reason autocomplete feels instant.
This article is general information about how tries work, not advice about choosing a data structure for a particular system.







