Data Structures19 sections · 729 units
Open in Course

Intro

Why tries matter

How would you store a dictionary of 100,000 words and check if a word exists? A hash set gives O(L)O(L) lookup where LL is word length. Good enough?

Now try these: Find all words starting with "pre". Find all words that match "c_t" (any letter in the middle). Count words with a given prefix.

Hash sets struggle with prefix queries. Tries solve these problems. Each node represents a character, and paths from root to nodes spell out prefixes.

All words with prefix "pre" live in the subtree under p→r→e. Operations on tries take O(L)O(L) time. Independent of dictionary size. This makes tries needed for:

  • Autocomplete systems
  • Spell checkers
  • IP routing tables
  • Word games (Boggle, Scrabble)