Question: Rewrite/Simplify the above Hashtable class. We will take a key and insert it into a list representing the hashtable using the following hash function index

Rewrite/Simplify the above Hashtable class. We will take a key and insert it into a list representing the hashtable using the following hash function

index = key % (length of hashtable) 

You may assume there will be space for the key. If a collision occurs, you should use double hashing to determine where the key should be placed. The secondary hash function is:

step = q - (key % q) 

where q will be the second prime number parameter of the hashtable

The 'None' value will be used to represent empty positions in the hashtable.

 class HashTable: def __init__(self, s=13): self.size = s 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 #replace else: nextslot = self.rehash(hashvalue,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 #replace def hashfunction(self,key,size): return key%size def rehash(self,oldhash,size): return (oldhash+1)%size def __str__(self): return str(self.slots)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!