Outline of Class 47: Dictionaries and Hash Tables, Continued
Held: Monday, April 27, 1998
- We have an exam tomorrow. A review
sheet is available (and was updated last night after I wrote the
exam). Any questions on the exam? I'll spend as much
time as necessary today answering review questions.
- Don't forget to sign up for the departmental picnic lunch. There's a
sign-up sheet outside my door. Only sign up once!
- What do you do when you try to insert an object into a hash table,
and there's already an object at the position?
- You can put multiple objects in the same position (by using
a list, a tree, or even another hash table with a different
- You can find another place in the table for the object by adding
another value to the hash value.
- You can grow the hash table.
- The first method requires us to provide a dynamic data structure
for each cell of the table. It also means that the lookup cost
also involves searching through the structure.
- The second method requires us to do more than one "step" when
looking up a value (as we might need to repeatedly skip
already filled spaces).
- The third method requires us to grow the table regularly, and at
a significant cost (proportional to the number of elements in the
- The second method also assumes a limited number of objects will
be placed in the table.
when you delete an object, you need to consider whether one of
the objects in other cells really belonged there.
- There are a number of ways you can compute the "next" space in
the second method.
- You can offset by a fixed amount.
- You can use a second function to determine the offset (making
the offset object-dependent).
- You can can use a sequence of hash functions.
- Despite all this joyous freedom, I'll admit that I prefer the first
- Our analysis of Hash Tables to date has been based on two simple
operations: lookup and insert.
- What happens if we want to remove elements? This can significantly
- If we've chosen the "shift into a blank space" method, what do we
do when it comes time to remove elements?
- Do we shift everything back? If so, think about how far we may have
- Do we leave the thing there as a blank? We might then then remove
it later when it's convenient to do so.
- Do we do something totally different?
- Note also that there are different ways of specicfying "remove". We
might remove the element with a particular key. We might instead remove
elements based on their value. The second is obviously a much slower
operation than the first (unless we've developed a special way to handle
that problem - see if you can think of one).