[   ^ to index...   |   <-- previous   ]

Implementations of a map

The implementation of a map as a binary search tree should be fairly obvious: simply store the value and key in the node, and sort on the key:

struct TreeMapNode { KeyType key; ValueType value; TreeMapNode *left, *right; };

However, other implementations are possible...

Hashing

We cannot use arrays in general to implement a map for two reasons. First, most of the time the key domain will not consist of integers only. Second, even if the key domain were the integers, we do not want to spend the memory necessary to handle all possible keys directly.

Hash tables can be viewed as a way of overcoming these limitations. The idea:

  1. Use the array locations as buckets containing lists of pointers.
  2. Define a "hash function" that maps each element of the key domain to an index into the array.

Why is this good?

With a good hash function, in fact, even for moderately sized data sets (say, a few thousand elements), a hash table can provide a much faster implementation of a map than a tree can.

Most modern languages provide some form of hash table in the standard library. C++ currently does not define a standard hash_map, but most vendors provide one and a future revision of the language may include one.


Last modified: Wed Aug 16 20:00:07 PDT 2000