Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are hash maps better than trie maps?

By trie map I mean an associative array, where the payloads are stored in a trie instead of a hash table.

When I'm using a hash map/table, the keys I use are typically strings. What are the advantages of a hash map over some trie based map? I have read that a hash map is faster - but it seems to me that a consistent hash functions would have to check each element of the (char) array for the final hash - iterating over the array once. In a trie you would similarly have to iterate over the array just once.

It does seem to me that this would use a lot more memory when encoding small objects (even if you only allow lower case alpha characters in the keys, it's 26 pointers per node and often multiple nodes per key), but on the plus side you never have to worry about resizing. Why is it that hash maps are so common, but I've never seen a trie map?

like image 456
Ramfjord Avatar asked Apr 08 '11 08:04

Ramfjord


People also ask

Is HashMap better than Trie?

The trie solution is more flexible to support more applications, such as auto-complete. Also, we can easily print all the words in the dictionary in alphabetic order with a trie. Therefore, if we want a full-text lookup application, the hash table is better as it has a faster lookup speed.

Why is hash better than map?

Null Keys : STL Map allows one null key and multiple null values whereas hash table doesn't allow any null key or value. Thread synchronization : Map is generally preferred over hash table if thread synchronization is not needed. Hash table is synchronized.

Why are hash maps useful?

Hashmaps are probably the most commonly used implementation of the concept of a map. They allow arbitrary objects to be associated with other arbitrary objects. This can be very useful for doing things like grouping or joining data together by some common attribute.

How efficient are hash maps?

A HashMap shouldn't be more than 70% – 75% full. If it gets close, it gets resized and entries rehashed. Rehashing requires n operations which is costly wherein our constant time insert becomes of order O(n) It's the hashing algorithm which determines the order of inserting the objects in the HashMap.


2 Answers

Just in case you are a Scala programmer, TrieMap is a "concurrent thread-safe lock-free implementation of a hash array mapped trie". There's none in Java standard lib this moment.

like image 73
Dan Avatar answered Sep 17 '22 18:09

Dan


Hash maps are more common than trie maps because they are more generic: they can be made to work on any object that is hashable, while a trie works on sequences. Hash tables also have better locality of reference in common implementations because they store elements close together.

(Strictly speaking, every object is a sequence of bits, but then a generic trie would require the user to serialize their object before storing it in the trie. That's pretty inconvenient compared to defining custom hash functions.)

like image 34
Fred Foo Avatar answered Sep 21 '22 18:09

Fred Foo