find(k)
: If M
contains an entry e=(k,v)
, then return an iterator p
referring to this entry; otherwise, return the special iterator end
put(k,v)
: If M
does not have an entry with key equal to k
, then add entry (k,v)
to M
; otherwise, replace the value with v
and return an iterator referring to the inserted/modified entryerase(k)
: Remove from M
the entry with key equal to k
; an error condition occurs if M
has no such entryerase(p)
: Remove from M
the entry referenced by iterator p
; an error condition occurs if p
points to the end sentinelbegin()
: Return an iterator to the first entry of M
end()
: Return an iterator to a position just beyond the end of M
size()
, empty()
Operation | Output | Map |
---|---|---|
empty() | true | ร |
put(5,A) | p1:[(5,A)] | (5,A) |
put(7,B) | p2:[(7,B)] | (5,A),(7,B) |
put(2,C) | p3:[(2,C)] | (5,A),(7,B),(2,C) |
put(7,D) | p2:[(7,E)] | (5,A),(7,D),(2,C) |
find(7) | p2:[(7,E)] | (5,A),(7,D),(2,C) |
find(3) | end | (5,A),(7,D),(2,C) |
find(2) | p3:[(2,C)] | (5,A),(7,D),(2,C) |
size() | 3 | (5,A),(7,D),(2,C) |
erase(5) | - | (7,D),(2,C) |
erase(p2) | - | (7,D) |
find(7) | p1:[(7,D)] | (7,D) |
empty() | false | (7,D) |
L
(based on a doubly-linked list), in arbitrary orderAlgorithm find(k):
for each p in [L.begin(), L.end()) do
if p.key() == k then
return p
return L.end() // there is no entry with key equal to k
Algorithm put(k,v):
for each p in [L.begin(), L.end()) do
if p.key() == k then
*p โ (k, v)
return p
p = L.insertBack((k, v)) // there is no entry with key k
n = n + 1 // increment number of entries
return p
Algorithm erase(k):
for each p in [L.begin(), L.end()) do
if p.key() == k then
L.erase(p)
n = n - 1 // decrement number of entries
return
put
takes O(n)
time since we need to determine whether it is already in the
sequencefind
and erase
take O(n)
time since in the worst case (the item is not found) we traverse the entire sequence to look for an item with the given keyfirstEntry()
: smallest key in the maplastEntry()
: largest key in the mapceilingEntry(k)
: smallest key >= k
floorEntry(k)
: largest key <= k
M
supports the abstraction of using keys as indices with a syntax such as M[k]
.n
items uses keys that are known to be integers in a range from 0
to N-1
, for some N>=n
.0
to N-1
?
h
maps keys of a given type to integers in a fixed interval [0,N-1]
h(x) = x mod N
is a hash function for integer keysh(x)
is called the hash value of key x
H
for a given key type consists of
h
N
(k,o)
at index i = h(k)
N=10,000
and the hash function h(x) = last four digits of x
H
h_1: keys โ integers
h_2: integers โ [0, N - 1]
H,e,l,l,o
H,e,l,l,o
: 72,101,108,108,111
z
, the polynomial will be determined.N
of the hash table is usually chosen to be a primea
and b
are nonnegative integers such that b
H
with N = 9
slots (A[0 โ 8]
) and let the hash function be h(k) = k mod N
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
---|---|---|---|---|---|---|---|---|
ย | (Kevin, PA) | (John, MA) | (James, CA) | ย | (Jacob, TX) | (Mary, NY) | ย | (Jane, WY) |
ย | (Angie, PA) | ย | ย | ย | ย | (Mike, WA) | ย | ย |
ย | (Julie, FL) | ย | ย | ย | ย | ย | ย | ย |
h(x) = x mod 13
18,41,22,44,59,32,31,73,
in this orderH
with N = 11
slots(A[0โ10]
) and let the hash function be h(k) = k mod N
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
(Kevin, PA) | (Jane, WY) | ย | ย | (Mary, NY) | (John, MA) | (Mike, WA) | (James, CA) | (Julie, FL) | (Angie, PA) | (Jacob, TX) |
i = 0, 1, ..., N -1
h'(k)
and handles collisions by placing an item in the first available cell of the series
h'(k)
cannot have zero valuesN
must be a prime to allow probing of all the cellsq<N
q
is a primeh'(k)
are 1, 2, ... , q
N=13
h(k) = k mod 13
h'(k) = 7 โ (k mod 7)
18,41,22,44,59,32,31,73,
in this orderO(n)
timeS
of distinct (key, element) items is a series of lists such thatS_i
contains the special keys +โ
and -โ
S_0
contains the keys of S
in nondecreasing orderS_h
contains only the two special keys(x,o)
into a skip list, we use a randomized algorithm:i
the number of times the coin came up headsi >= h
, we add new lists , each containing only the two special keysx
in the skip list and find the positions p_0, p_1 , ..., p_i
of the items with largest key less than x in each list S_0, S_1, ... , S_i
j โ 0, ..., i
, we insert item (x, o)
into list S_j
after position p_j
15
, with i = 2