Tries
Prefix trees for autocomplete, word search and XOR problems.
4 pages, free to read
- 1
Trie basics
BeginnerA trie stores strings as paths of characters from a shared root, so words with the same prefix share nodes and any lookup costs O(L) for a string of length L, no matter how many words are stored.
9 min read - 2
Prefix search & autocomplete
IntermediateWalk the trie down to the node for the typed prefix, then explore only that node's subtree to list, count or rank the words that start with it.
10 min read - 3
Word search with a trie
AdvancedTo find many words on a letter grid at once, put the words in a trie and run one backtracking DFS per cell that follows the trie, so every path stops the moment no word can continue it.
12 min read - 4
Bitwise trie (maximum XOR)
AdvancedStore numbers in a trie by their bits from most significant to least, then for each number greedily walk toward the opposite bit at every level to find the partner that gives the largest XOR, in O(bits) per query.
11 min read