Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best data structure for text auto completion?

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.

like image 953
ipman Avatar asked Feb 27 '12 20:02

ipman


People also ask

Which data structure is used for autocomplete?

Autocomplete is a feature of suggesting possible extensions to a partially written text and is widely used in search engine, code IDEs and much more. Trie data structure is a perfect fit to implement this feature efficient in terms of memory and time [O(length of string)].

Which is the most efficient data structure?

Arrays. An array is a linear data structure that holds an ordered collection of values. It's the most efficient in storing and accessing a sequence of objects.

How is Auto Complete implemented?

Cloud Search's autocomplete assists a user by suggesting words when the user is typing a search query. The suggestions might be words from document titles, search operators, search operator values, names and email from the Google Workspace domain, and so on.


2 Answers

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

like image 103
andrew cooke Avatar answered Oct 22 '22 22:10

andrew cooke


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.

like image 45
nodakai Avatar answered Oct 22 '22 21:10

nodakai