My favourite data structure: The trie
jamesg.blog
jamesg.blog
It's easy to end up writing code that's got good algorithmic time complexity, but where the CPU spends its time sitting cold and waiting on RAM.
It’s been interesting preparing for coding interviews. Quite a few times, the “optimal” solution time-complexity wise, is slower than the brute force solution
Many of those it’s because of the inputs. If you know what they are or what their order distribution is going to be, you can usually do quite a lot better than the naive “ideal” solution
[1] https://os.unil.cloud.switch.ch/tind-customer-epfl/30e62590-...
It is very elegant.
(can one hit all of these CS concepts by examining only two data structures? or is this path via tries didactically minimal?)
```
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>, {})
```
Only after I "invented" it I could actually find a paper written in 1978 that was exactly "my" idea.
I couldn't find it first because the terminology that paper used was quite different from the modern terminology. Only when I had a glimpse in the solution I was able to find the paper because I happened to use terms related to the solution space and not those related to the problem space