What is the best data structure for text auto completion?

你离开我真会死。 提交于 2021-02-04 15:57:06

问题


I have a long list of words and I want to show words starting with the text entered by the user. As user enters a character, application should update the list showed to user. It should be like AutoCompleteTextView on Android. I am just curious about the best data structure to store the words so that the search is very fast.


回答1:


A trie could be used . http://en.wikipedia.org/wiki/Trie https://stackoverflow.com/search?q=trie

A nice article - http://www.sarathlakshman.com/2011/03/03/implementing-autocomplete-with-trie-data-structure/

PS : If you have some sub-sequences that "don't branch" then you may save space by using a radix trie, which is a trie implementation that puts several characters in node when possible - http://en.wikipedia.org/wiki/Radix_tree




回答2:


You may find this thread interesting:

  • String similarity algorithms?

It's not exactly what you want, instead it's a slightly extended version of your problem.




回答3:


For implementation of autocomplete feature, ternary search trees(TST) are also used:

http://igoro.com/archive/efficient-auto-complete-with-a-ternary-search-tree/

However, if you want to find any random substring within a string, try a Generalised suffix tree.

http://en.wikipedia.org/wiki/Generalised_suffix_tree




回答4:


Tries (and their various varieties) are useful here. A more detailed treatment on this topic is in this paper. Maybe you can implement a completion trie for Android?



来源:https://stackoverflow.com/questions/9471823/what-is-the-best-data-structure-for-text-auto-completion

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