Which is the best data structure for storing a dictionary? A hash table or a trie? Consider the possibility that the more words can be added to the dictionary later on.
preguntado el 24 de diciembre de 12 a las 21:12
std::map would be the best data structure for a dictionary.
std::unordered_map is the C++11 equivalent of a hash table. While
std::map is the regular associative container.
Neither of these data structures is "better" than one another. It depends completely on what your needs are.
A hash table for strings is good if you are primarily interested in answering the question "does string X exist in my hash table?" It supports (usually) fast lookups and has a low memory footprint; each string is stored exactly once. However, it relies on the existence of a good hash function, is susceptible to hash collisions for pathological inputs, and does not let you do searches like "what string is closest to my string?"
A trie is a good data structure for storing strings that gives good worst-case lookups (you need only look at each character of the input string once). It also has the advantage that strings with similar prefixes can be stored compactly. Additionally, the trie allows you to search for strings with a given prefix, or to do regex searches efficiently, or to find nearby words efficiently. It has the drawback that the memory usage of a trie tends to be much higher than that of a hash table due to the number of pointers being stored.
There are other data structures besides these that you could consider. Radix tries and Patricia trees give a more condensed representation of tries but at some additional programming complexity. Árboles BK can be used if you are interested primarily in finding all strings "close" to some initial string efficiently.
In short, if memory is at a premium or you don't need to do "close string" searches, a hash table is a good choice. If you need to look for nearby strings or do other string operations, a trie is probably a better choice.
¡Espero que esto ayude!