put(k,v)
- associate key k
with value v
in the mapget(k)
- return the value v
associated with key k
{ Peter -> CS, Alice -> CS, Bob -> CS }
abba
would be encoded as { 'a' -> 2, 'b' -> 2 }
[0,n)
buckets
of size n
v
associated with a key k
is stored in buckets[k]
None
to indicate that a key k
is not in the map{ 1 -> 'a', 3 -> 'b', 5 -> 'c' }
with a keyspace [0,6)
`[ None, 'a', None, 'b', None, 'c' ]`
`[ 0 , 1 , 2 , 3 , 4 , 5 ]`
put(k,v)
: buckets[k] = v
get(k)
: buckets[k]
put
and get
are O(1)
using this implementation[0,n]
A hash function h
maps keys from a keyspace into a domain [0,n]
We can use hash functions to extend our array based idea for other keyspaces
Example:
[100000,5000000]
[0,19]
h(x) = x % 20
n
and a hashfunction h: keys -> [0,n)
put(k,v) = buckets[h(k)] = v
get(k) = buckets[h(k)]
h(k1) = h(k2)
for two keys k1
and k2
k1
and k2
with h(k1) = h(k2)
in the hashtable
(key,value)
pairsh(x) = x % 4
{ 5 -> 'a', 9 -> 'b', 0 -> 'c' }
[ 0 ] -> (0,'c')
[ 1 ] ->
[ 2 ] -> (5, 'a') -> (9, 'b')
[ 3 ] ->
def get(k):
lc = buckets[h(k)]
while lc:
if lc.key == k:
return lc.val
lc = lc.next
return None
def put(k,v):
lc = buckets[h(k)]
while lc:
if lc.key == k:
lc.val = v
return
newlc = ListCell(k,v,buckets[h(k)])
buckets[h(k)] = newlc
[1,2,3,2,3,3]
with {1,2,3}
e
be the element we use as value for every key
{1,2,3}
{ 1 -> e, 2 -> e, 3 -> e }
add
, contains
and delete
operations are in $O(1)$ for hashsets