Prefix search in a radix tree/patricia trie

后端 未结 4 1582
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 16:16

I\'m currently implementing a radix tree/patricia trie (whatever you want to call it). I want to use it for prefix searches in a dictionary on a severely underpowered piece

4条回答
  •  心在旅途
    2020-12-30 16:46

    An alternative algorithm: Keep It Simple Stupid!

    Just make a sorted list of your keywords. When you have a prefix, binary search to find where that prefix would be located in the list. All of your possible completions will be found starting at that index, ready to be accessed in place.

    This algorithm will will require only 5% of the code of a Patricia trie and will be easy to maintain, understand, and update. It is almost certain this simple list search will be more efficient as well.

    The only downside is if you have huge numbers of long keywords with similar prefixes, a trie can save some storage since it doesn't need to keep the full prefix for every entry. In practice, if you have less than a few million words, this is not a savings because the pointer overhead of the tree will dominate. This savings is more for applications like searching databases of DNA strings with millions of characters, not text keywords.

提交回复
热议问题