How Do I Choose Between a Hash Table and a Trie (Prefix Tree)?

后端 未结 8 1782
终归单人心
终归单人心 2020-11-27 09:13

So if I have to choose between a hash table or a prefix tree what are the discriminating factors that would lead me to choose one over the other. From my own naive point of

相关标签:
8条回答
  • 2020-11-27 09:48

    There's something I haven't seen anyone mention explicitly that I think is important to keep in mind. Both hash tables and tries of various kinds will typically have O(k) operations, where k is the length of the string in bits (or equivalently in chars).

    This is assuming you have a good hash function. If you don't want "farm" and "farm animals" to hash to the same value, then the hash function will have to use all the bits of the key, and so hashing "farm animals" should take about twice as long as "farm" (unless you're in some sort of rolling hash scenario, but there are somewhat similar operation-saving scenarios with tries too). And with a vanilla trie, it's clear why inserting "farm animals" will take about twice as long as just "farm". In the long run it's true with compressed tries as well.

    0 讨论(0)
  • 2020-11-27 09:55

    Use a tree:

    1. If you need auto complete feature
    2. Find all words beginning with 'a' or 'axe' so on.
    3. A suffix tree is a special form of a tree. Suffix trees have a whole list of advantages that hash cannot cover.
    0 讨论(0)
提交回复
热议问题