Trie visualizer: prefix tree and autocomplete

A trie stores words letter by letter, so words that begin the same way share the same path from the root. Type your own words and watch the tree form, then search for a word, autocomplete a prefix, insert or delete, and compare the trie with its compressed form, the radix tree.

Words
9
Trie nodes
14
Radix nodes
10
Show
Rank suggestions by
Trie · 14 nodes for 33 letters
· c d a o r t g t d e t f u l
end of a word path walked below the prefix
Suggestions for "car"
  1. car1
  2. card1
  3. care1
  4. careful1
  5. cart1
Steps
  1. follow 'c'
  2. follow 'a'
  3. follow 'r'
  4. collect every word below: 5 words in 7 nodes
Letters typed
33
Trie nodes
14 + root
Radix tree nodes
10 + root

How to use the visualizer

Type or paste words into the box, separated by spaces, commas or new lines; the trie is rebuilt as you type. Add a frequency after a colon (app:5) to rank autocomplete suggestions by how often each word is used. Choose an operation and type a word or prefix: the path it walks is highlighted and each step is listed. Insert and Delete change the word list when you press their button.

  • Nodes with a double ring end a word. The root is drawn as a dot.
  • Switch to Radix tree to see the same words with single-child chains merged.
  • Copy answer gives the trie as an indented outline and the steps of the current operation.

What is a trie?

A trie, or prefix tree, is a tree for storing strings in which each edge stands for one character and each node for the prefix spelled out on the way down to it. The root is the empty string. A word is stored by following or creating one node per letter and marking the last node as the end of a word, since a word can also be the prefix of a longer one, like "car" in "card".

Because the position of a node already says which prefix it is, a trie never compares whole keys. Finding a word of m letters takes m steps whether the trie holds ten words or ten million.

Insert, search and delete

Insert walks down the word's letters from the root, creating a child wherever one is missing, and marks the final node. Search walks the same way and succeeds only if every letter is found and the final node is marked; reaching an unmarked node means the string is only a prefix of stored words.

Delete unmarks the final node, then removes nodes from the bottom up as long as they have no children and end no other word. In the example car, card, care, careful, cart, cat, dog, do, dot, deleting "careful" removes 3 nodes (the letters f, u and l), while deleting "car" removes 0, because its nodes are still needed by card, care and cart.

Autocomplete

Autocomplete is a prefix query: walk down the letters typed so far to one node, then collect every word in the subtree below it. In the example, the prefix "car" leads to a node whose subtree holds car, card, care, careful, cart; the query visits 10 nodes. Real autocomplete ranks the candidates, usually by how often each was searched or typed, and shows the top few; frequencies after a colon in the word list do the same here.

Collecting the whole subtree takes time proportional to its size, which grows when a short prefix matches thousands of words; storing the best few completions at each node in advance avoids the walk, at the cost of memory.

How big a trie gets

A trie has one node for each distinct prefix of its words, plus the root. The 9 example words have 33 letters but share their beginnings, so the trie needs 14 nodes. The more prefixes words share, the bigger the saving.

Each node also needs a way to find its children. An array with a slot for every letter of the alphabet is fastest, but 26 pointers per node add up quickly, and most slots stay empty; a hash map or sorted list per node saves memory at some cost in speed.

Radix trees

Long chains of nodes with a single child, the tails of words that share nothing with others, waste space. A radix tree (also called a compressed trie, or Patricia trie in its binary form) merges each such chain into one node labelled with several characters. Every remaining node is the root, ends a word, or branches. The example words need 10 nodes this way instead of 14. The 38 reserved words of JavaScript, 191 letters in all, need 158 trie nodes and 54 radix tree nodes.

Where tries are used

Tries suit any job that needs prefixes: autocomplete in search boxes and code editors, spell checkers that suggest words sharing a beginning, and word games. Tries over the bits of network addresses can store routing tables, where a lookup must find the longest stored prefix of an address.

Trie questions and answers

How many nodes does a trie have?

One per distinct prefix of the stored words, plus the root. For car, card, care, careful, cart, cat, dog, do, dot that is 14 nodes plus the root, for 33 letters of input, because words that share a beginning share its nodes.

What is the time complexity of a trie?

Insert, search and delete take O(m) steps for a word of m letters, however many words are stored, because each step follows one child pointer. Listing the completions of a prefix also costs time proportional to the size of the subtree below it.

How does a trie do autocomplete?

It walks down the letters of the prefix to one node; every word below that node starts with the prefix. Collecting them and sorting by how often each was used gives the suggestions. For the prefix "car" in this page's example the words below are car, card, care, careful, cart.

What is the difference between a trie and a radix tree?

A radix tree (compressed trie, Patricia trie) merges every chain of nodes that have one child and end no word into a single edge labelled with several letters. The 38 reserved words of JavaScript need 158 trie nodes but only 54 in a radix tree.

Trie or hash table?

A hash table finds a whole word as fast or faster and uses less memory, but it cannot list the words with a given prefix or return them in order without looking at every key. A trie does both naturally, which is why it suits autocomplete, spell checking and routing tables.

Further reading

Published Updated