I am trying to understand the open addressing method. I refer to T. H. Cormen’s book on this topic, which states that deletion is difficult in open addressing. I am completely stuck at this paragraph:
Deletion from an open-address hash table is difficult. When we delete a key from slot
i, we cannot simply mark that slot as empty by storingNILin it. Doing so might make it impossible to retrieve any keykduring whose insertion we had probed slotiand found it occupied.
I don’t understand this. Please explain it with some examples.
Assume
hash(x) = hash(y) = hash(z) = i. And assumexwas inserted first, thenyand thenz.In open addressing:
table[i] = x,table[i+1] = y,table[i+2] = z.Now, assume you want to delete
x, and set it back toNULL.When later you will search for
z, you will find thathash(z) = iandtable[i] = NULL, and you will return a wrong answer:zis not in the table.To overcome this, you need to set
table[i]with a special marker indicating to the search function to keep looking at indexi+1, because there might be element there which its hash is alsoi.