Just as linked lists and dynamic arrays are convenient implementations of the idea of a "vector"/"list" ADT, so we can regard trees as a convenient implementation of the map ADT.
In mathematical terms, a map (as the term is commonly used in computer science) is a partial function whose domain is a set of keys and whose range is a set of values. The idea behind this function is that it "maps" each key onto a unique value.
In pseudocode, the following interface defines the important map operations:
class Map { // Insert/"associate" a given value for the given key. void insert(KeyType key, ValueType value); // Returns true if the given key exists in the map bool contains(KeyType key); // Retrieve the value associated with the given key. // Precondition: contains(key) is true ValueType find(KeyType key); // Remove the pointer to the value associated with // the given key. Returns the value removed. // Precondition: contains(key) is true ValueType remove(KeyType key); }; Common uses for maps are as dictionaries (in fact, you will often hear the "dictionary" ADT used as a synonym for "map"). Sometimes maps are called "associative arrays", because it is natural to subscript maps as you might subscript an array:
ValueType& operator[](KeyType key) { return find(key); } The implementation of a map as a tree should be fairly obvious: simply store the value pointer and key in the node, and sort on the key. However, other implementations are possible:
We were able to insert, remove, and find elements with our various list data structures. Any vector that provides the above operations serves as a map. What is the disadvantage?
On the other hand, if our key type is int, we have a very natural implementation of a map as a vector... what is it? What is the space overhead for this scheme?
Hash tables are a way of overcoming this limitation. Brief idea: Use the array locations as buckets containing lists of pointers. Define a "hash function" that maps each element of the key domain to an index into the array. Given a good hash function, the buckets will each contain less than some constant number of elements, giving O(1) lookup for a key.