a
is non-negative, the modulus operator a % b
calculates the remainder when a
is divided by b
. As a refresher, compute the following:
12 % 3
14 % 3
8 % 5
3 % 10
a >= 0
, what is the possible range of values resulting from computing a % b
?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
What is the load factor of the table you created?
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. */
get(Object key);
V
/** Associates the specified value with the
* specified key in this map */
put(K key, V value);
V
/** Removes the mapping for a key from this map
* if it is present */
remove(Object key);
V
/** Return the number of key-value entries in the map. */
int size();
// more methods
}
What output is produced by the following pseudocode that uses a SomeMap
class that implements the Map interface?
Map m = new SomeMap<Character,Integer>();
.put('X', 4);
m.put('Y', 3);
m.put('Z', 2);
mprint(m.get('Y'));
.put('X', 8);
mprint(m.get('X'));
print(m.size());
.remove('Y');
mprint(m.get('Y'));