Академический Документы
Профессиональный Документы
Культура Документы
00 Tutorial 11
HashTables
elem[0]
item 1 item 2 item 3
elem[1]
elem[2] linked list
...
array
binary
search
tree
Pop quiz: what is the average search
time for each of these? Worst case
search time? We want to improve
the average search time.
HashTable
Contents of a bucket are a collection
of objects whose keys hash to the same value
Bucket index is the (0 in this case). Can be a linked list, array,
hash code of the key Vector, binary tree, etc.
In general,
bucket [0] Obj 1 Obj 2 null
objects in the
bucket [1] hash table have
bucket [2] two components:
a key, and a
value. The hash
bucket [n] Obj 2: key code is computed
on the key. The
Obj 2: value value is the data
that is stored. In
some cases, the
key and value will
be the same.
Super Simple Example
We will design hash table with the
following restrictions
Key is the same as the value
Can only hold Strings
Hash function is the length of the String
Always has just 5 buckets