3 ms·
My favorite trie implementation defines the trie as: ``` from collections import defaultdict def trie(): return defaultdict(trie) ``` Yeah, a `trie` i
by hughdbrown 3y ago
My favorite trie implementation defines the trie as:
```
from collections import defaultdict
def trie():
return defaultdict(trie)
```
Yeah, a `trie` is a dictionary whose values are tries. And the values are automatically created by reference:
```
>>> t = trie()
>>> t["e"]["a"]["r"]["t"]["h"]
defaultdict(<function trie at 0x10d5ebc40>, {})
```