AlgoViz

Trie (prefix tree)

medium 20 min

A trie (pronounced "try") is a tree that stores strings one character per level, so words with the same opening letters share the same path from the root.

Where it shows up

The same pattern appears across everyday software.

  • Search engine autocomplete

    As you type, the search box walks a trie of popular queries one character per keystroke and shows the most searched completions. Real engines shard the trie across servers and cache the answers for hot prefixes.

  • Spell checkers and keyboards

    Word processors and phone keyboards test words against a dictionary trie and suggest corrections by exploring branches close to what was typed.

  • IP routing

    Routers store address prefixes in a binary trie and forward each packet using the longest prefix that matches its destination address.

  • Word games

    Boggle, Scrabble and crossword solvers walk a dictionary trie alongside the board and drop any letter path that is not the start of a real word.

  • Contact and command search

    Phone contact lists, IDE symbol search and shell command completion narrow the list with every character typed using prefix lookups.