Docsity

Vector Map - 6 Questions - Data Structure - Exam | CS 261, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Worksheet 5: Vector Map Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 5: Vector Map
In this lesson you will once again use a Vector as an underlying data container,
only this time implementing a Map. A Map is an indexed collection class. This
means that elements are always provided in a pair consisting of a key and a
value. Just as we used a symbolic constant to define the EleType for a vector, we
will use two symbolic types to define both the KeyType and the ValueType for our
vector. The interface file for this abstraction is shown on the next page. By
default, we will use a character pointer for the key, and a double for the value.
Insead of the EQ and LE macros used with with the vector, we will have macros
for LtKey and EqKey. The Map API is shown near the bottom of the page. When
a value is inserted both the key and the value are provided. To search the
collection the user provides a key, and the corresponding value is returned. A
dictionary that associates words and definitions is a good mental model of a Map.
The idea behind the VectorMap is that internally
elements in the vector are stored as instances of
class Association. The internal class
Association stores a key and a value.
Each element in the internal Vector
is a pointer to an Association:
When searching to see if there is an entry with a given key, for example, each
element of the vector is examined in turn. The key is tested against the search
key, and if it matches the element has been found.
A similar approach is used to delete a value. A loop is used to find the
association with the key that matches the argument. Once the index of this
association is found, the Vector remove operation is used to delete the value.
Elements in a map must have unique key. Within the method put one easy way
to assure this is to first call containsKey, and if there is already an entry with the
given key call remove to delete it. Then the new association is simply added to
the end.
struct association {
KeyType key;
ValueType value;
};
pf3
pf4
pf5

Partial preview of the text

Download Vector Map - 6 Questions - Data Structure - Exam | CS 261 and more Exams Data Structures and Algorithms in PDF only on Docsity!

Worksheet 5: Vector Map

In this lesson you will once again use a Vector as an underlying data container, only this time implementing a Map. A Map is an indexed collection class. This means that elements are always provided in a pair consisting of a key and a value. Just as we used a symbolic constant to define the EleType for a vector, we will use two symbolic types to define both the KeyType and the ValueType for our vector. The interface file for this abstraction is shown on the next page. By default, we will use a character pointer for the key, and a double for the value. Insead of the EQ and LE macros used with with the vector, we will have macros for LtKey and EqKey. The Map API is shown near the bottom of the page. When a value is inserted both the key and the value are provided. To search the collection the user provides a key, and the corresponding value is returned. A dictionary that associates words and definitions is a good mental model of a Map. The idea behind the VectorMap is that internally elements in the vector are stored as instances of class Association. The internal class Association stores a key and a value. Each element in the internal Vector is a pointer to an Association: When searching to see if there is an entry with a given key, for example, each element of the vector is examined in turn. The key is tested against the search key, and if it matches the element has been found. A similar approach is used to delete a value. A loop is used to find the association with the key that matches the argument. Once the index of this association is found, the Vector remove operation is used to delete the value. Elements in a map must have unique key. Within the method put one easy way to assure this is to first call containsKey , and if there is already an entry with the given key call remove to delete it. Then the new association is simply added to the end. struct association { KeyType key; ValueType value; };

void vectorMapPut (struct vector *vec, KeyType key, ValueType val) { struct association * ap; if (vectorMapContainsKey(vec, key)) vectorMapRemove(vec, key); ap = (struct association *) malloc(sizeof(struct association)); assert(ap != 0); ap->key = key; ap->value = val; vectorAdd(vec, ap); } To extract a value you use the function get. Rather than return the association element, this function takes as argument a pointer to a memory address where the value will be stored. This technique allows us to ignore an extra error check; if there is no key with the given value the function should do nothing at all. void vectorMapGet(struct vector *vec, KeyType key, ValueType *valptr); Based on your implementation, fill in the following table with the algorithmic execution time for each operation:

containsKey (Object key) O(

get (Object key) O(

put (Object key, Object value) O(

remove (Object key) O(

ifndef vectormaph

define vectormaph

include

ifndef KeyType

define KeyType char *

endif

ifndef ValueType

define ValueType double

endif

ifndef KeyLT

define KeyLT(a, b) (strcmp(a, b) < 0)

endif

ap = (struct association *) malloc(sizeof(struct association));

assert(ap != 0);

ap->key = key;

ap->value = val;

vectorAdd(vec, ap);

int vectorMapContainsKey (struct vector *vec, KeyType key) {

void vectorMapRemove (struct vector *vec, KeyType key) {

On Your Own

  1. In what ways is a map similar to an array or a vector? In what ways are they different?
  2. The put operation described earlier removes any prior association with the given key before insertion a new association. An alternative would be to search the list of associations, and if one is found simply replace the value field. If no association is found, then insert a new association. Write the put method using this approach. Which is easier to understand? Which is likely to be faster?
  3. Sometimes the map abstraction is defined using a number of additional functions. Show how each of these can be implemented using a VectorMap. What is the big-oh complexity of each? a. int containsValue (struct vector &v, ValueType testValue) b. void clear (struct vector &v) // empty all entries from map c. boolean isEmpty (struct vector & v) d. void keySet (struct vector &v, struct vector &e) // return a set containing all keys e. void values (struct vector &v, struct vector &e) // return a bag containing all values
  4. Assuming the method keySet described in question 5 is provided, show how to produce an Iterator that will loop over the map keys. Do you think the Map interface should have included an iterator? What values should this iterator produce?
  5. The method equals returns a boolean value that is true if the argument is a map, and the two maps have the same set of keys (even if they have different values associated with those keys). What is the big-oh complexity of this operation?
  6. A variation on a map, termed a multi-map, allows multiple entries to be associated with the same key. Explain how the multimap abstraction complicates the operations of search and removal.