python code. Below is the code for parallel list implementation of dictionary ADT. Modify the code below to handle collision using double hashing technique. Use the following hash functions: h(k) = k mod n, where is the number of slots in the hash table h’(k) = 7 - (k mod 7) and handles collisions by placing an item in the first available cell of the series A[(i + f(j)) mod N] for j = 1, 2, 3, ..., where f(j) = j*h’(k). import os class Dictionary: def __init__(self, size): self.keys = [None] * size self.values = [None] * size self.n = 0 self.size = size
use python code.
Below is the code for parallel list implementation of dictionary ADT. Modify the code below to
handle collision using double hashing technique. Use the following hash functions:
h(k) = k mod n, where is the number of slots in the hash table
h’(k) = 7 - (k mod 7)
and handles collisions by placing an item in the first available cell of the series
A[(i + f(j)) mod N] for j = 1, 2, 3, ..., where f(j) = j*h’(k).
import os
class Dictionary:
def __init__(self, size):
self.keys = [None] * size
self.values = [None] * size
self.n = 0
self.size = size
def __str__(self):
keys_str = "{"
if not self.empty():
for i in range(0, self.size - 1):
keys_str += "({},{}),".format(self.keys[i], self.values[i])
keys_str += "({},{})".format(self.keys[self.size - 1], self.values[self.size - 1])
return keys_str + "}"
def empty(self):
return self.n == 0
def dictionary_size(self):
return self.n
def hash(self, key):
return key % self.size
def insert_item(self, key, item):
if self.n == self.size:
print("Dictionary Overflow")
else:
index = self.hash(key)
self.keys[index] = key
self.values[index] = item
self.n += 1
def remove_item(self, key):
index = self.hash(key)
if self.keys[index] is None:
print("Can't remove the item. Key not found...")
else:
self.keys[index] = None
self.values[index] = None
self.n -= 1
def find_item(self, key):
index = self.hash(key)
if self.keys[index] is None:
return -1
else:
return self.values[index]
dictionary = Dictionary(5)
dictionary.insert_item(1, 2)
dictionary.insert_item(2, 6)
dictionary.insert_item(3, 9)
dictionary.insert_item(1, 10)
print(dictionary)
print(dictionary.find_item(3))
print(dictionary.find_item(1))
print(dictionary.remove_item(1))
print(dictionary)
Step by step
Solved in 2 steps