Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is a better implementation to implement a trie node's children - array or hashmap?

I am reading about trie data structure and found two implementations to implement the children in trie node. Following are the details of the two implementations :-

1) Trie node array of length 26 has been used to store children of the trie node.

2) HashMap has been used to store children of the trie node with character as the key and Trie node as the value.

Please let me know which implementation is better and why?

like image 362
ashisahu Avatar asked Sep 15 '16 19:09

ashisahu


People also ask

How would you optimize the space complexity in trie?

One way to implementing Trie is linked set of nodes, where each node contains an array of child pointers, one for each symbol in the alphabet. This is not efficient in terms of time as we can't quickly find a particular child. The efficient way is an implementation where we use hash map to store children of a node.

How many children can a trie have?

In a trie indexing an alphabet of 26 letters, each node has 26 possible children and, therefore, 26 possible pointers.

How do you implement the prefix trie?

Implement the Trie class: Trie() Initializes the trie object. void insert(String word) Inserts the string word into the trie. boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.


1 Answers

It depends - a usual trade-off between memory and speed.

If your strings are short and you don't have memory issues then of course go for the array. This way you make your searches faster. Also good if your letters are evenly distributed among words.

If your strings can be large and there are some letters that occur very rarely then go for the hash map. This way you don't occupy too much unused memory. That's also better if your alphabet is much larger than 26 letters.

Array is faster but potentially consumes more memory than HashMap - but not necessary. Imagine that your bag of words contain all the possible 26^N words of length N that can be made of 26 letters. Then HashMap would be both slower and consume more memory.

like image 67
dreamzor Avatar answered Nov 13 '22 02:11

dreamzor