Exercises - Lecture 14

14A

  1. How large an array would you need to create a direct address table that stores a Set of letters in the alphabet (ignore case)?
  2. If a is non-negative, the modulus operator a % b calculates the remainder when a is divided by b. As a refresher, compute the following:
  3. Assuming a >= 0, what is the possible range of values resulting from computing a % b?

14B

  1. Create a hash table with capacity (i.e., underlying array size) 8. Using h(x) = x % 8 as the hash function and chaining for collision resolution, insert the following values into the hash table in the order given and draw the resulting table. Assume this table is to be used to implement the Set ADT, so duplicate values are not allowed.

    1, 11, 16, 4, 5, 8, 0, 1, 13
  2. What is the load factor of the table you created?

14C

As a reminder, here’s the Map interface you saw in the lecture video, with the addition of a size method:

public interface Map<K,V> {
    /** Returns the value to which the specified key
      * is mapped, or null if this map contains no
      * mapping for the key. */
    V get(Object key);

    /** Associates the specified value with the
      * specified key in this map */
    V put(K key, V value);
    
    /** Removes the mapping for a key from this map
      * if it is present */
    V remove(Object key);

    /** Return the number of key-value entries in the map. */
    int size();
  
    // more methods
}
  1. What output is produced by the following pseudocode that uses a SomeMap class that implements the Map interface?

    Map m = new SomeMap<Character,Integer>();
    m.put('X', 4);
    m.put('Y', 3);
    m.put('Z', 2);
    print(m.get('Y'));
    m.put('X', 8);
    print(m.get('X'));
    print(m.size());
    m.remove('Y');
    print(m.get('Y'));