Hash Table: Why deletion is difficult in open addressing scheme

后端 未结 3 1645
时光说笑
时光说笑 2021-01-31 04:18

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

3条回答
  •  悲&欢浪女
    2021-01-31 04:57

    Deletion from a linear probed open addressed hash table is simple. There was pseudo code for it on the Wikipedia Hash Table page for years. I don't know why is isn't there any more, but here is a permalink back to when it was: Old Wikipedia Hash Table page, and here for your convenience is the pseudocode:

    function remove(key)
     i := find_slot(key)
     if slot[i] is unoccupied
         return   // key is not in the table
     j := i
     loop
         j := (j+1) modulo num_slots
         if slot[j] is unoccupied
             exit loop
         k := hash(slot[j].key) modulo num_slots
         if (j > i and (k <= i or k > j)) or
            (j < i and (k <= i and k > j)) (note 2)
             slot[i] := slot[j]
             i := j
     mark slot[i] as unoccupied
    

    There is also a ref on that page to some real code. I believe this has exactly the same performance characteristic as insertion.

    This method of deletion is better than the much used 'mark deleted and occasionally rehash everything' because the above method is constant time rather than amortized constant time. If you have a hash table of a million items you are adding and deleting from, in the 'mark deleted' method, an occasional add or delete is going to take a million times longer than the ones before and after it - which is not a good performance characteristic.

提交回复
热议问题