Hash Table Chain

When we have a hash table with a chain:

I'm just wondering if saving a list affects each key depending on the time it takes to search, insert, and delete in the hash table?

+3
source share
3 answers

In theory: yes, since in the average case you will need to go through half the chain to find if the element is in the chain or not.

In practice, there are probably not many differences, since the chains are usually very short, and the increased complexity of the code will also cost several cycles, mainly in the case of “insertion”.

: , " " -. , - - . , , . :

struct hashnode **this;
for (this=& table[slot] ; *this; this = &(*this)->link) {
    if ((*this)->hash != the_hash) continue;
    if (compare ((*this)->payload , the_value)) continue;
    break;
 }
 /* at this point "this" points to the pointer that points to the wanted element,
    or to the NULL-pointer where it should be inserted.

    For the sorted-list example, you should instead break out of the loop
    if the compare function returns > 0, and handle that special case here.

 */
+2

, , , . ( ) , , , , .

+1

, . O (1) - - , , .

In practice, this is not so. You will always have (for a sufficiently large dataset) collisions. And collisions will mean a lot of work during the search, regardless of whether you use a chain or some other conflict resolution technique.

That's why it is very important to choose a good hash function, well designed / written, and a good match of the data that you will use as a key for your hash table. In practice, different types of data will hash better with different hash functions.

0
source

All Articles