While using Linear probing method to implement hashing, when we delete and element, the position of the deleted element is declared as a tombstone/ mark it as deleted. Why can’t we just shift all the elements from the current position until the next empty element is encountered? Won’t this save the time when too many tombstones are encountered later and a rehashing might be required? Am I missing something? Please tell me if I need to clarify my question.
Thanks a lot!
Best explained by counter example:
HASH(A) = 10
HASH(B) = 10
HASH(C) = 12
myHash.insert(A); // A goes to slot 10
10 11 12
+----+----+----+
| A | | |
+----+----+----+
myHash.insert(B); // B goes to slot 10, but that is taken, so it sees 11 is empty and inserts there
10 11 12
+----+----+----+
| A | B | |
+----+----+----+
myHash.insert(C); // C hashes to slot 12 and gets put there
10 11 12
+----+----+----+
| A | B | C |
+----+----+----+
myHash.remove(B); // We remove B, and shift the ones after it back
10 11 12
+----+----+----+
| A | C | |
+----+----+----+
myHash.contains(C) == false; // C hashes to slot 12, but there is nothing there. So therefore the hash does not contain C (but it does CONTRADICTION).
Therefore, by proof of contradiction, simply sliding the succeeding elements upon removal will result in incorrectness.
EDIT: C should have said that it hashes to 12
2