Hash table idea

The idea behind hash tables is as follows.

A hash table consists of a list of a specific size. Let’s call the size of the list N. An element must be stored in the hash table with a particular “key”, for instance a string. A so-called “hash function” is used to turn that key into a number (how exactly it does that I will not go into here, just assume that there is a hash function that may turn any string into some number, whereby different strings tends to get different numbers). Let’s call this number H. The element is then stored in the list at index H%N (H modulo N).

Consequently, to detect whether an element with a particular key exists in the hash table, you translate that key with the hash function to its associated number H, and check whether there is something stored in the hash table at H%N.

Of course, there are many issues with this procedure. It is very well possible that two different keys end up being hashed to the same number. It is even more likely that two different hash numbers end up at the same index in the hash table. Moreover, even if it would be guaranteed that all hash numbers translate to different indices, at some point the hash table will be completely filled up, and where are new elements going to be stored then?

The issues mentioned are solved as follows: