Lecture 14 - Problems

Goals

  1. Hash tables can be used to implement the Set ADT. Write pseudocode for the three main Set operations, insert, contains, remove for the HashSet below, which is specific to integers.

    public class IntHashSet {
      LinkedList<Integer>[] A; // an array of LinkedList<Integer>s
      // assume a constructor is implemented that instantiates A
    
      /** insert value into the set. return false if the value
        * was already in the set, true otherwise */
      boolean insert(int value);
    
      /** return true if value is in the set, 
        * false otherwise */
      boolean contains(int value);
    
      /** remove value from the set. return true if the
        * value was in the set, false otherwise */
      boolean remove(int value);
    }
  2. What’s the minimum and maximum load factor of a hash table that uses chaining for collision resolution?

In the following problems, assume that the hash function can be computed in constant time.

  1. What are the worst-case runtime classes of contains, remove, and insert in a HashSet that has \(N\) entries and capacity \(C\)?
  2. Like quicksort, hash tables have the unusual property that the average case runtime is not the same as the worst case. Under some reasonable assumptions, the average-case time to find an element in a hash table is the time to search the bucket in which the element is stored. This means the average-case runtime is determined by the average size of a bucket. If the array size (capacity) is \(C\) and the load factor is \(\lambda\), what is the average bucket size? What does this mean for the average-case runtime of contains, remove, and insert?
  3. Suppose you have a generic implementation of the Set ADT that can store a Set of any single type (akin to the IntHashSet from above, but generic). Describe how you could go about using the Set to implement a Map. Describe any initial setup needed, then for each Map method (get, put, remove) describe how you would implement it using the Set’s methods (insert, contains, remove). Hint: Consider this perspective: a Map is very much like a Set of key-value pairs in which the keys are unique.