Answer the question
In order to leave comments, you need to log in
How to grow a hash table?
A simple task from problem solving with algorithms and data structures.
There is a hash table with a certain size - equal to a prime number.
With a certain amount of padding, it is necessary to increase its size in order to avoid excessive collisions.
Actually how to calculate this fullness?
How to increase? (my guess is two times and looking for the nearest prime number)
And what to do with the elements that are already in the table? (hash again by new size?)
For clarity, the beginning of the table:
class Map:
def __init__(self, size=11):
self.size = size
self.slots = [None] * self.size
self.data = [None] * self.size
def put(self, key, data):
hashvalue = self.hashfunction(key, len(self.slots))
if self.slots[hashvalue] == None:
self.slots[hashvalue] = key
self.data[hashvalue] = data
else:
if self.slots[hashvalue] == key:
self.data[hashvalue] = data
else:
nextslot = self.rehash(key, len(self.slots))
while self.slots[nextslot] != None and \
self.slots[nextslot] != key:
nextslot = self.rehash(nextslot,len(self.slots))
if self.slots[nextslot] == None:
self.slots[nextslot] = key
self.data[nextslot] = data
else:
self.data[nextslot] = data
Answer the question
In order to leave comments, you need to log in
Filling usually take 70-75% of the size of the table. You can also try to count the number of steps in the rehash loop, and if it is more than a critical value for some element (for example, 3 or 5), then it's time.
"Multiply by 2 and take the next prime number" is fine.
Of course, you will have to hash again - how could it be without it :)
By the way, is it right that rehash does not know about the original key? After all, it turns out that if rehash gave the same results for two keys, then the entire subsequent chain will match for them, and the chances will increase that new elements will fall into this particular chain.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question