Trie
Data StructuresavgO(L)worstO(L)spaceO(total letters)
An empty trie: just the root
1 / 40
Operations8 / 40
- 1. insert("car")
- 2. insert("cat")
- 3. insert("cart")
- 4. insert("dog")
- 5. insert("do")
- 6. search("cat")
- 7. search("ca")
- 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)