Linear Probing: handles collisions by placing the colliding item in the next (circularly) available table cell. If we try to insert a key into a cell that is already occupied, we try again at (and repeat until we find an empty cell to insert into).
In order to get(k):
- We start at cell
- Probe consecutive locations until one of the following occurs:
- item with key is found
- empty cell is found
- cells have been unsuccessfully probed
Example pseudo-code:
def get(k):
i = h(k)
p = 0
repeat:
c = A[i]
if c == null:
return null
else if c.getKey() == k:
return c.getValue()
else:
i = (i + 1) mod N
p += 1
until p == N
return nullIn order to remove(k):
- search for an entry with key
- if such an entry is found, replace it with special item “available” and we return element
- else, return
In order to put(k,v):
- throw an exception if the table is full
- start at cell
- probe consecutive cells until:
- a cell is found that is either empty or stores “available”
- or until cells have been unsuccessfully probed
- we store in cell