Sub topic 8
Deleting from an open-addressing table
This chapter assumes you know how linear probing inserts and finds keys, as covered in the previous chapter. It also assumes you are comfortable with the modulo operator and introductory Big-O.
Why deletion is a problem
With linear probing, a lookup starts at the key's home slot and walks forward until it finds the key or reaches an empty slot. An empty slot means "the key is not here", because an insert would have used it.
Suppose keys 1, 9 and 17 all hash to slot 1 in a table of 8 slots. They fill slots 1, 2 and 3. If you delete 9 by setting slot 2 back to empty, a lookup for 17 starts at slot 1, sees a different key, reaches the empty slot 2, and stops. It reports that 17 is missing, even though it sits in slot 3. Clearing the slot breaks the probe chain.
Tombstones
The fix is to mark the slot as deleted instead of empty. This marker is called a tombstone. The two markers mean different things:
- Empty: nothing was ever stored here, so a lookup can stop.
- Tombstone: something was stored here and removed, so a lookup must keep going.
Implementation
The table below uses two sentinel objects, _EMPTY and _DELETED. The remove method writes _DELETED, and _find_slot skips over it.
_EMPTY = object()
_DELETED = object()
class ProbingHashTable:
def __init__(self, capacity=8, max_load=0.5):
self._slots = [_EMPTY] * capacity
self._used = 0 # live entries plus tombstones
self._live = 0 # live entries only
self._max_load = max_load
def _find_slot(self, key):
n = len(self._slots)
i = hash(key) % n
for _ in range(n):
slot = self._slots[i]
if slot is _EMPTY:
return None
if slot is not _DELETED and slot[0] == key:
return i
i = (i + 1) % n
return None
def get(self, key, default=None):
i = self._find_slot(key)
return default if i is None else self._slots[i][1]
def put(self, key, value):
i = self._find_slot(key)
if i is not None:
self._slots[i] = (key, value)
return
if (self._used + 1) / len(self._slots) > self._max_load:
self._resize(len(self._slots) * 2)
n = len(self._slots)
i = hash(key) % n
while self._slots[i] is not _EMPTY and self._slots[i] is not _DELETED:
i = (i + 1) % n
if self._slots[i] is _EMPTY:
self._used += 1
self._slots[i] = (key, value)
self._live += 1
def remove(self, key):
i = self._find_slot(key)
if i is None:
return False
self._slots[i] = _DELETED
self._live -= 1
return True
def _resize(self, new_capacity):
old = self._slots
self._slots = [_EMPTY] * new_capacity
self._used = 0
self._live = 0
for slot in old:
if slot is not _EMPTY and slot is not _DELETED:
self.put(slot[0], slot[1])
t = ProbingHashTable()
for k in (1, 9, 17):
t.put(k, k * 10)
t.remove(9)
print(t.get(9), t.get(17))This prints None 170. Key 17 is still found because the lookup walks past the tombstone.
Two counters
The class tracks _used and _live separately. A tombstone is not a live entry, but it still lengthens probe chains, so it must count toward the load factor. The resize check therefore uses _used, which includes tombstones.
put also reuses the first tombstone it meets on the probe path. It calls _find_slot first, so it never inserts a duplicate of a key that sits further along the chain. Reusing a tombstone does not raise _used, since that slot was already counted.
Cleaning up
Tombstones pile up in a table with many deletes. Probe chains grow, and misses get slower, because a missed lookup must walk to a true empty slot. The _resize method discards tombstones, since it reinserts only live entries. Many implementations also rebuild the table when tombstones make up too much of it, even if the live count is small.
Exercise
A table has capacity 8, and keys 2, 10 and 18 were inserted in that order (all hash to slot 2). Key 10 is removed, then key 26 (also slot 2) is inserted. Which slot does 26 occupy, and does _used change?
Show answer
Key 26 reuses the tombstone in slot 3, the slot key 10 held. The _find_slot call finds no 26 first, so it is a new key. _used does not change because that slot was already counted as used. _live rises by one.