Question
onsider the HashTable class below which stores the keys and the associated data in two parallel arrays. This HashTable class uses the linear probing technique
onsider the HashTable class below which stores the keys and the associated data in two parallel arrays. This HashTable class uses the linear probing technique to handle collisions:
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): hash_value = self.get_hash_value(key) if self.slots[hash_value] == None: self.slots[hash_value] = key self.data[hash_value] = data else: if self.slots[hash_value] == key: self.data[hash_value] = data #replace else: next_slot = self.rehash(hash_value) while self.slots[next_slot] != None and \ self.slots[next_slot] != key: next_slot = self.rehash(next_slot) if self.slots[next_slot] == None: self.slots[next_slot] = key self.data[next_slot] = data else: self.data[next_slot] = data #replace def get_hash_value(self, key): return key % self.size def rehash(self, old_hash): return (old_hash + 1) % self.size def __str__(self): return str(self.slots) + str(self.data)
Based on the class above, define the QuadraticHashTable class. This class stores only the keys (no associated data) and uses quadratic probing to handle collisions and uses the following hash function:
index = key % (size_of_hashtable)
You may assume there is space in the HashTable for the key. If a collision occurs, use quadratic probing to determine where the key should be placed. The None value is used to represent available slots in the hashtable.
Note: you may choose use the LinearHashTable class developed in the previous question as a starting point for this question
Submit the entire QuadraticHashTable class.
For example:
Test | Result |
---|---|
my_hash_table = QuadraticHashTable() my_hash_table.put(26) my_hash_table.put(54) my_hash_table.put(94) my_hash_table.put(17) my_hash_table.put(31) my_hash_table.put(77) my_hash_table.put(13) my_hash_table.put(39) print(my_hash_table) | [26, 13, 54, 94, 17, 31, None, None, None, 39, None, None, 77] |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started