# 23 - 22:42:55 CS 61B Lecture 23 Monday Todays reading...

This preview shows pages 1–2. Sign up to view the full content.

03/16/09 22:42:55 1 23 CS 61B: Lecture 23 Monday, March 16, 2009 Today’s reading: Hash Tables (continued) ----------- Reminder: n is the number of keys (words) whose definitions we want to store, and we use a table of N buckets. The compression function is h(hashCode) = hashCode mod N. Several keys are hashed to the same bucket in the table if h(hashCode1) = h(hashCode2). This circumstance is called a _collision_. How do we handle collisions without losing entries? We use a simple idea called _chaining_. Instead of having each bucket in the table reference one entry, we have it reference a linked list of entries, called a _chain_. If several keys are mapped to the same bucket, their definitions all reside in that bucket’s linked list. Chaining creates a second problem: how do we know which definition corresponds to which word? The answer is that we must store each key in the table with its definition. The easiest way to do this is to have each listnode store an _entry_ that has references to both a key (the word) and an associated value (its definition). --- ---------------------------------------------------------- defTable |.+-->| . | . | X | . | X | . | . | . .. --- ----|-------|---------------|---------------|-------|----- v v v v v --- --- --- --- --- |.+>pus |.+>swirl |.+>tough |.+>cool|.+>mud |.+>goo |.+>vortex |.+>sucks 2BU |.+>jrs |.+>wet dirt |.| |X| |X| |.| |X| -+- --- --- -+- --- | | v v --- ^ --- |.+>sin < chains > |.+>gigantic |.+>have fun |.+>very big |X| |X| --- --- Hash tables usually support at least three operations. An Entry object references a key and its associated value. public Entry insert(key, value) Compute the key’s hash code and compress it to determine the entry’s bucket. Insert the entry (key and value together) into that bucket’s list. I’m not public Entry find(key) Hash the key to determine its bucket. Search the list for an entry with the given key. If found, return the entry; otherwise, return null. public Entry remove(key) Hash the key to determine its bucket. Search the list for an entry with the given key. Remove it from the list if found. Return the entry or null. What if two entries with the same key are inserted? There are two approaches. Following Goodrich and Tamassia, we can insert both, and have find() or

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/21/2010 for the course CS 61B taught by Professor Canny during the Spring '01 term at Berkeley.

### Page1 / 2

23 - 22:42:55 CS 61B Lecture 23 Monday Todays reading...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online