Home > Blockchain >  Is hash map (or hash table) should have an array in its internal structure?
Is hash map (or hash table) should have an array in its internal structure?

Time:08-30

I've seen many examples or articles explaining hash map based on array.

For example, all of the data is stored in an array, and you can get an index of a value by calling hash function with its key.

So, in case that there is no collision, any access to hash map is O(1). As the access is actually for an array, knowing its index.

My question here is that, is there any implementation of specific language or library that hash map is not built on array?

Should hash map must be built on array?

CodePudding user response:

While there are other data structures that use both hash functions and data structures other than arrays to provide key/value mappings (you can read about some at https://en.wikipedia.org/wiki/Hash_trie), I'd argue that they are not "hash maps" as the combined phrase is generally intended and understood. When people talk about hash maps, they generally expect a hash table underneath, and that will have one array of buckets (or occasionally a couple to allow a gradual resizing for more consistent performance).

A general expectation for hash maps is that they provide amortised O(1) lookup, and that is only possible if - once the key is hashed - you can do O(1) lookup using the hash value. An array is the obvious data structure with that property. There are some other options, like having a fixed-depth tree of arrays, the upper levels holding pointers to the lower levels, with the final level being either values or pointers to them. At least one level must be allowed to grow arbitrarily large for the overall container to support arbitrary size. With a constant maximum depth, the number of lookup steps isn't correlated with the number of keys or elements stored, so it's still O(1) lookup. That provides partial mitigation of the cost of copying/moving elements between large arrays more often, but at the cost of constantly having a greater (but constant) number of levels to traverse during lookups.

A C std::deque utilises this kind of structure to provide O(1) lookup. See STL deque accessing by index is O(1)? for explanations, diagrams. That said, I've never seen anyone choose to use a deque when implementing a hash map.

  • Related