问题
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