散列表
Hash Tables
Use BST and red-black BST to implement symbol tables could achieve average
The basic plan is to save items in a key-indexed table (index is a function of the ley).
Hash function. Method for computing array index from key.
Issues.
- Efficiently computing the hash function.
- Equality test: Method for checking whether two keys are equal.
- Collision resistance: Algorithm and data structure to handle two keys that hash to the same array index.
Hash Functions
Use hashCode() to get hash values. View Java 配套笔记 on how to implement hashCode() for user-defined type.
Note that hashCode() returns an int between int between
private int hash(Key key) {
return (key.hashCode() & 0x7fffffff) % M;
// M is typically a prime or power of 2
}
The efficiency of hash tables is based on the uniform hashing assumption.
Uniform hashing assumption. Each key is equally likely to hash to an integer between
Collision Resolutions
Collision. Two distinct keys hashing to the same index.
Two kinds of symbol tables could effiiciently handle collision under uniform hashing assumption.
- Separate chaining symbol table.
- Linear probing symbol table.
Separate chaining ST. Use an array of
- Hash: map key to interger
between and . - Insert: put at front of the i-th chain (if not already there).
- Search: need to search only i-th chain.
public Value get(Key key) {
int i = hash(key);
for (Node x = st[i]; x != null; x = x.next)
if (key.equals(x.key)) return (Value) x.val;
return null;
}
public void put(Key key, Value val) {
int i = hash(key);
for (Node x = st[i]; x != null; x = x.next)
if (key.equals(x.key)) { x.val = val; return; }
st[i] = new Node(key, val, st[i]); // insert at front
}
Consequence. Number of probes (equals() and hashCode()) for search/insert is proportional to
too large too many empty chains. too small chains too long. - Typical choice:
constant-time ops.
Linear probing ST. (open addressing) When a new key collides, find next empty slot, and put it there.
- Hash. Map key to integer
between and . - Insert. Put at table index
if free; if not try , , etc. - Search. Search table index
; if occupied but no match, try , , etc.
Note that array size
public Value get(Key key) {
for (int i = hash(key); keys[i] != null; i = (i+1) % M)
if (key.equals(keys[i])) return vals[i]; // search hit
return null; // search miss
}
public void put(Key key, Value val) {
int i;
for (i = hash(key); keys[i] != null; i = (i+1) % M)
if (keys[i].equals(key)) break;
keys[i] = key;
vals[i] = val;
}
Proposition. Under uniform hashing assumption, the average # of probes in a linear probing hash table of size
too large too many empty array entries. too small clustering happens. search time blows up. - Typical choice: [half full]
. - # probes for search hit is about
. - # probes for search miss is about
.
- # probes for search hit is about
The deletion in linear-probing ST is a bit tricky.
- Tombstones.
- Reinsert all of the key-value pairs in the same cluster appeared after the deleted key-value pair.
Hash Tables vs. BST
Hash tables.
- Simplier to code.
- No effective alternative for unordered keys.
- Faster for simple keys in practice (a few arithmetic ops versus
compares).
Balanced search trees.
- Stronger performance guarantee. (Hash tables need uniform hashing assumption)
- Support for ordered ST operations.
- Easier to implement
compareTo()correctly thanequals()andhashCode().
Java system includes both implementation:
- Red-black BSTs:
java.util.TreeMap,java.util.TreeSet. - Hash tables:
java.util.HashMap,java.util.IdentityHashMap.
