Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Data Structure Hash Table Your hash functions will hash strings into 16-bit

ID: 3819209 • Letter: C

Question

C++ Data Structure

Hash Table

Your hash functions will hash strings into 16-bit (not 32-bit) int s. This is important, because were going to keep a table of the number of collisions for each hash value. With 16-bit int s there are only 65.536 possible hash values. so this table will easily fit in memory. If we used 32-bit int s then there would be 4,294,967.296 possble hashes. a more troublesome amount. For C you can get a 16-bit unsigned int type by doing #include cstdint uint16_t x; x has exactly 16 bits and is signed For each of the possible hash functions your program should Create a vector int hashes of size 65.536 Process the list of words. and for each word, compute its hash h Increment the entry in the table for that hash: hashes.at(h)++ When finished, use Pearson'sX test to determine the probability that the resulting distribution is uniformly distributed. (See below for the deets.) Hash functions to test The hash (non)functions you should test are: String length (modulo 16 First character 16 Additive checksum (add all characters together), modulo 2 Remainder (use a modulo of 65413. this is the first prime that is smaller than the table size). This will also test the base-256 mod 216 hash function Multiplicative (using the scheme described in class/in the lecture notes) Any other hash schemes you can think up. (ld suggest making your hash testing function take a function pointer. so that you can just write all the above hash functions and then test them one by one.)

Explanation / Answer

#include<cstdinnt>

uint16t_x;

class HashTable:
def __init__(self):
self.size = 11
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 get(self,key):
startslot = self.hashfunction(key,len(self.slots))

data = None
stop = False
found = False
position = startslot
while self.slots[position] != None and
not found and not stop:
if self.slots[position] == key:
found = True
data = self.data[position]
else:
position=self.rehash(position,len(self.slots))
if position == startslot:
stop = True
return data

def __getitem__(self,key):
return self.get(key)

def __setitem__(self,key,data):
self.put(key,data)

H=HashTable()
H[54]="cat"
H[26]="dog"
H[93]="lion"
H[17]="tiger"
H[77]="bird"
H[31]="cow"
H[44]="goat"
H[55]="pig"
H[20]="chicken"
print(H.slots)
print(H.data)

print(H[20])

print(H[17])
H[20]='duck'
print(H[20])
print(H[99])