Which search is faster, binary search or using prefix tree?

落爺英雄遲暮 提交于 2019-12-22 08:29:15

问题


Suppose I have a list of strings and a prefix tree of those strings, and I would like to locate a string given a key, which one is more faster? binary search or prefix tree search?

Why and what's the time complexity?

Thanks!


回答1:


Both techniques have their advantages, and their drawbacks:

Suffix tree

  • Advantages:
    • O(N) building complexity
    • O(M) search of a pattern of length M
    • They allow online construction
  • Drawbacks:
    • Space inefficient
    • Really complex construction algorithms

Binary search (with suffix array)

  • Advantages:
    • You can sort the string array in O(N) time
    • Space efficient (five times less memory (at least))
    • Simple and straightforward construction algorithms
  • Drawbacks:
    • They don't support online construction
    • O(M lg N) time to search a pattern of length M among N strings (this can be reduced to O(M+lg N) but this is still a little slower than suffix tree)

Both of these data structures are really powerful. If your application requires fast searching, and the space supplied is enough, then definitely go for suffix trees. But if the space matters, then suffix array(binary search) is the only choice you have...



来源:https://stackoverflow.com/questions/17273121/which-search-is-faster-binary-search-or-using-prefix-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!