5

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!

asked Aug 23, 2013 at 17:45

1 Answer 1

7

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

answered Aug 23, 2013 at 18:02
2
  • 1
    @BobFincheirmer You example doesn't explain what happens if we remove A and then check hash.contains(B). Commented Aug 23, 2013 at 19:17
  • @duros It would work in that situation, but the point of the counter-example is to show it does not work in all situations Commented Aug 26, 2013 at 15:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.