Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to remove an entry from a hash table

What is the best way to remove an entry from a hashtable that uses linear probing? One way to do this would be to use a flag to indicate deleted elements? Are there any ways better than this?

like image 785
ashokgelal Avatar asked Nov 10 '08 23:11

ashokgelal


People also ask

How do I remove an item from a hash table?

To remove the Key-value from the Hashtable, you need to use the Remove(Key) method. You cannot remove the hashtable entry with the values. You must use the key inside the Remove() method.

How do I delete linear probing?

Deleting an item from a hash table using linear probing therefore means that we cannot mark the slot as empty: the slot may form part of a linear probe sequence. We will have to mark the slot as "deleted" instead, and modify the search algorithm slightly to continue searching if a deleted slot is found.

How do you clear a Hashtable in PowerShell?

Hash table in the PowerShell session is created temporarily. It is like a variable, when the session is closed, hash table is deleted automatically. If you want to delete all the values from the hash table at once but retaining the hash table variable, you need to use the Clear() method.

What is linear probing in hash table?

Linear probing is a scheme in computer programming for resolving collisions in hash tables, data structures for maintaining a collection of key–value pairs and looking up the value associated with a given key. It was invented in 1954 by Gene Amdahl, Elaine M.


2 Answers

An easy technique is to:

  1. Find and remove the desired element
  2. Go to the next bucket
  3. If the bucket is empty, quit
  4. If the bucket is full, delete the element in that bucket and re-add it to the hash table using the normal means. The item must be removed before re-adding, because it is likely that the item could be added back into its original spot.
  5. Repeat step 2.

This technique keeps your table tidy at the expense of slightly slower deletions.

like image 94
Imbue Avatar answered Sep 20 '22 11:09

Imbue


It depends on how you handle overflow and whether (1) the item being removed is in an overflow slot or not, and (2) if there are overflow items beyond the item being removed, whether they have the hash key of the item being removed or possibly some other hash key. [Overlooking that double condition is a common source of bugs in deletion implementations.]

If collisions overflow into a linked list, it is pretty easy. You're either popping up the list (which may have gone empty) or deleting a member from the middle or end of the linked list. Those are fun and not particularly difficult. There can be other optimizations to avoid excessive memory allocations and freeings to make this even more efficient.

For linear probing, Knuth suggests that a simple approach is to have a way to mark a slot as empty, deleted, or occupied. Mark a removed occupant slot as deleted so that overflow by linear probing will skip past it, but if an insertion is needed, you can fill the first deleted slot that you passed over [The Art of Computer Programming, vol.3: Sorting and Searching, section 6.4 Hashing, p. 533 (ed.2)]. This assumes that deletions are rather rare.

Knuth gives a nice refinment as Algorithm R6.4 [pp. 533-534] that instead marks the cell as empty rather than deleted, and then finds ways to move table entries back closer to their initial-probe location by moving the hole that was just made until it ends up next to another hole.

Knuth cautions that this will move existing still-occupied slot entries and is not a good idea if pointers to the slots are being held onto outside of the hash table. [If you have garbage-collected- or other managed-references in the slots, it is all right to move the slot, since it is the reference that is being used outside of the table and it doesn't matter where the slot that references the same object is in the table.]

like image 25
orcmid Avatar answered Sep 20 '22 11:09

orcmid