Tries are very fast data structures. Looking up a word takes O(sizeofword) time, while std::maps are self-balacing trees. Why aren’t the standard C++ map templates implemented with tries. Is there any specific reason? Are there any tradeoffs of using a trie instead of a self-balancing tree?
Tries are very fast data structures. Looking up a word takes O(sizeofword) time, while
Share
Tries can only be used when the keys to be stored can be processed digit by digit or character by character. The C++
std::mapandstd::setare designed to work with any comparable elements as keys, and thus can’t be implemented in a way that processes the keys character by character. They instead (typically) use balanced binary search trees, which don’t need to introspect on the keys and can instead just use a comparator to do fast lookups.If you know for sure that certain properties hold for your keys, you can in some cases do even better than tries (see the van Emde Boas tree for an example). However, library designers have to design for a lot of use cases and thus may need to pick options that are slower than the absolutely best option because they need to handle the largest possible number of options.
Additionally, it’s actually perfectly possible that a conforming C++ implementation contain a specialization of
std::maporstd::setthat uses a trie when the keys are strings. I don’t believe that any do, but it is in theory possible.Hope this helps!