9.2Open Addressing (Array)
Delete: the deleted marker
Deleting looks easy at first: find the key and empty its cell. But in open addressing an empty cell means "the path ends here". If you empty the deleted cell, the keys that sit after it can no longer be found. The fix is to put a separate marker in the cell.
- Why making the deleted cell
EMPTYbreaks search - How tombstones are handled by search and insertion
- Why a table with many deletions has to be rebuilt
1Idea
Say the table holds 14 (3), 25 (4), 36 (5) and 18 (7). Imagine deleting 25 and making cell 4 EMPTY. When 36 is searched for, the search starts at 3, sees EMPTY at 4 and says "36 is not here". Yet 36 sits one cell further on. The deletion has broken the path to 36.
EMPTY, the path to 36 breaks. The search stops at 4 and never sees 36 in cell 5.So the deleted cell gets DELETED (-2). This value means "a key used to be here, the path goes on". The search code does not change at all: DELETED is neither EMPTY nor the key being searched for, so search passes over it like a filled cell.
The delete function first finds the key with search. If it is not found, it returns false. If it is, the cell gets DELETED and count goes down by one.
Insertion reuses tombstones
A deleted cell should not go to waste; a new key can be written there. But insertion cannot write into the first DELETED cell right away, because the key may already be further along the path. So insertion remembers the first DELETED cell, keeps searching until an EMPTY cell, and if it has not seen the key, writes into the cell it remembered:
bool insert(struct HashTable* t, int key) { int h = hash(key); int slot = -1; // the first cell we may write to for (int i = 0; i < M; i++) { int j = (h + i) % M; if (t->items[j] == key) { return false; // already there } if (t->items[j] == DELETED && slot == -1) { slot = j; // remember it, keep looking } if (t->items[j] == EMPTY) { if (slot == -1) { slot = j; } break; // the key cannot be further on } } if (slot == -1) { return false; // the table is full } t->items[slot] = key; t->count++; return true;}As DELETED cells pile up, paths get longer: even in an almost empty table, search walks until it meets EMPTY. A table with many deletions is rebuilt from time to time, which clears the markers.
2Step by step
The table holds 14, 25, 36 and 18; 25 and 36 share home cell 3. First 25 is deleted, then we see that 36 can still be found.
- remove(25) first calls search(25).
25 % 11 = 3, first attemptj = 3: 14. j = 4: 25 found. The search returns 4.- Cell 4 gets
DELETED(DEL in the drawing) andcountbecomes 3. The cell is marked, not empty. - Now search(36).
36 % 11 = 3,j = 3: 14. j = 4:DELETED. NeitherEMPTYnor 36; the search does not stop.j = 5: 36 found. Thanks to the marker, the path did not break.
#define M 11#define EMPTY -1#define DELETED -2 struct HashTable { int items[M]; int count;}; int hash(int key) { return key % M;} int search(struct HashTable* t, int key) { int h = hash(key); // home cell for (int i = 0; i < M; i++) { int j = (h + i) % M; // attempt i if (t->items[j] == EMPTY) { return -1; // end of the path } if (t->items[j] == key) { return j; // found } } return -1;} bool removeKey(struct HashTable* t, int key) { // remove() is taken by stdio.h int j = search(t, key); if (j == -1) { return false; // not in the table } t->items[j] = DELETED; // not EMPTY: keep the path whole t->count--; return true;}static final int M = 11;static final int EMPTY = -1;static final int DELETED = -2; class OpenHashTable { int[] items = new int[M]; int count = 0; OpenHashTable() { java.util.Arrays.fill(items, EMPTY); } int hash(int key) { return key % M; } int search(int key) { int h = hash(key); // home cell for (int i = 0; i < M; i++) { int j = (h + i) % M; // attempt i if (items[j] == EMPTY) { return -1; // end of the path } if (items[j] == key) { return j; // found } } return -1; } boolean remove(int key) { int j = search(key); if (j == -1) { return false; // not in the table } items[j] = DELETED; // not EMPTY: keep the path whole count--; return true; }}M = 11EMPTY = -1DELETED = -2 class OpenHashTable: def __init__(self): self.items = [EMPTY] * M self.count = 0 def h(self, key): return key % M def search(self, key): h = self.h(key) # home cell for i in range(M): j = (h + i) % M # attempt i if self.items[j] == EMPTY: return -1 # end of the path if self.items[j] == key: return j # found return -1 def remove(self, key): j = self.search(key) if j == -1: return False # not in the table self.items[j] = DELETED # not EMPTY: keep the path whole self.count -= 1 return TrueThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Deletion is one search and one write; like search it takes O(1) on average and O(n) in the worst case.
No extra memory is used.
Tombstones lower the load factor but do not shorten paths: what matters for search speed is the number of cells that are not EMPTY.
4Common mistake
A common mistake is emptying the deleted cell:
t->items[j] = EMPTY; // wrong: breaks the path to the keys behind itThis line hides every key that landed on the same path after the deleted one; they are still in the table, yet searches return -1. Another mistake is letting insertion write into the first DELETED cell right away: if the key is already further on, it ends up in the table twice.
5Try it
In the table at the end of the drawing (3: 14, 4: DEL, 5: 36, 7: 18), which cell does insert(47) write into?
The answer is A. 47 % 11 = 3. Cell 3 holds 14; 4 is DELETED, so insertion remembers it. 5 holds 36 and 6 is EMPTY: 47 is not on the path. It is written into the first cell remembered, cell 4.
Show the answer
The answer is A. 47 % 11 = 3. Cell 3 holds 14; 4 is DELETED, so insertion remembers it. 5 holds 36 and 6 is EMPTY: 47 is not on the path. It is written into the first cell remembered, cell 4.
In the same table, what does search(25) do?
The answer is B. Cell 3 holds 14, cell 4 DELETED (not 25), cell 5 holds 36. Cell 6 is EMPTY: the path ends. 25 was deleted, so the search returns -1.
Show the answer
The answer is B. Cell 3 holds 14, cell 4 DELETED (not 25), cell 5 holds 36. Cell 6 is EMPTY: the path ends. 25 was deleted, so the search returns -1.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.