algo·visual

Trie

Data Structures
avgO(L)worstO(L)spaceO(total letters)

An empty trie: just the root

1 / 40
Operations8 / 40
  1. 1. insert("car")
  2. 2. insert("cat")
  3. 3. insert("cart")
  4. 4. insert("dog")
  5. 5. insert("do")
  6. 6. search("cat")
  7. 7. search("ca")
  8. 8. search("dot")

How it works

A prefix tree: each edge is a letter and each path from the root spells a prefix, so words that share a prefix share nodes. Insert and search take O(L) for a word of length L, independent of how many words are stored. A flag marks where whole words end.

Complexity

Best
O(L)
Average
O(L)
Worst
O(L)
Space
O(total letters)

Search algorithms

Jump to any algorithm or page