3

I'm trying to write a proper remove function for my hash table using linear probing collision resolution method.

I run a tester that removes several records from my hash table. At certain point, my find function cannot find an element to delete. However, it supposed to be still in the table. I think that I'm doing my rehash in remove() in a wrong way.

Class HashTable contains member table, an array of structures of type Record

template <class TYPE>
struct Record{
    string key;
    TYPE value = NULL;
    Record(){
        key = "";
        value = 0;
    }
    Record(const char* key_, TYPE value_){

        key = key_;
        value = value_;
    }
    Record(string key_, TYPE value_){

        key = key_;
        value = value_;
    }

};

template <class TYPE>
bool HashTable<TYPE>::remove(const char* key){ 
    int tvalue; //temporary unused value to make find work

    if (find(key, tvalue))
    {
        table[lastFoundIdx].key = "";  //lastFoundIdx - index of element that contains a key
        table[lastFoundIdx].value = 0;
        numRec--;                        //reduce number of records in table
        int startIdx = lastFoundIdx;     //rehash will stat form start Index
        int i;
        if (lastFoundIdx == maxSize - 1) 
            i = 0;
        else
            i = lastFoundIdx + 1;

        while (table[i].key != ""&&i != maxSize){   // rehash starts here
            std::size_t h1 = std::hash<std::string>()(table[i].key);
            int hash = h1%maxSize;
            if (hash < i&&hash >= startIdx){
                table[startIdx].key = table[i].key;
                table[startIdx].value = table[i].value;
                table[i].key = "";
                table[i].value = 0;
                startIdx = i;
            }
            i++;
        }
        return true;
    }
    else return false;
}

Here's also my find function which seems to be working fine but I could be wrong

template <class TYPE>
    bool HashTable<TYPE>::find(const char* key, TYPE& value){
        std::size_t h1 = std::hash<std::string>()(key);
        int hash = h1%maxSize;
        int startIdx = hash;
        while (table[hash].key != "" && table[hash].key != key){
            hash = (hash + 1) % maxSize;
            if (hash == startIdx)
                return false;

        }
        if (table[hash].key == "")
            return false;
        else{
            lastFoundIdx = hash;
            value = table[hash].value;
            return true;
        }
    }

Could you please help me to determine if I'm doing remove in a right way for linear probing?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Yochert
  • 85
  • 9
  • What is the type of `table`? – AndyG Mar 30 '15 at 15:08
  • @AndyG Templated Hash Table using Linear probing for collision resolution. When testing it initialized like this HashTable hashtable(hashtableMax); – Yochert Mar 30 '15 at 15:12
  • @Yochert why are you rehashing after you find the key you are looking for? You should simply find the record, delete it and that's it... – Pandrei Mar 30 '15 at 15:14
  • @Pandrei Hash function can provide same value for two different keys, then record will be stored in next available spot of the table. So when I am deleting a record I need to figure out if any of the records after it were supposed to put in this spot. – Yochert Mar 30 '15 at 15:17
  • @AndyG Sorry, type of table is an array of structures type Record – Yochert Mar 30 '15 at 15:18
  • @Yochert something is not right with your approach: when using linear probing as a collision resolution, you simply increase the index in the table until you find a free position, if the position returned by the hash function is already occupied. When you remove a record, assuming the hash function returns index i and you find the record on index i+2, you simply remove the record located at that index, but you cannot say anything about the record at index i+3, nor do you have to do anything about the ones at indexes i and i+1 – Pandrei Mar 30 '15 at 15:33
  • 1
    @Pandrei For example "cat" and "dog" have same hash values = 0. I put them one by one in the table[]. so "cat" goes to table[0], but "dog" cannot go there because it's occupied already so it goes table[1]. When I remove "cat" , table[0] is empty, and I need to move dog to that position because it's hash value is 0. Otherwise, if I will look for a "dog" Find function will go to table[0] which is empty and will return that there is no "dog" in the table, but it is. – Yochert Mar 30 '15 at 15:41
  • @Yochert fair point. – Pandrei Mar 30 '15 at 15:46

0 Answers0