You are on page 1of 8

BSCS-402 DATA STRUCTURE

LAB 10
OBJECT: Implement Hash table ADT and resolve collision using linear,
quadratic probing and separate chaining .

S.no Class_Hashtable.PY
1 class HashMapChain:
2
3     def __init__(self, hash : int = 10):
4         self.hash = hash
5         self.map = self.createMap(hash)
6     
7     def createMap(self,hash):
8         array = []
9         for i in range(hash):
10             array.append([])
11         return array
12
13     def func(self,key):
14         return key % self.hash
15
16
    def keyIndex(self, key : str):
17
        index = 0
18
        for i in key:
19
            index += ord(i)
20
        return self.func(index)
21
22
23     def __setitem__(self,key, value):
24         key_value = [key,value]
25         for pair in self.map[self.keyIndex(key)]:
26             if key == pair[0]:
27                 pair[1] = value
28                 return True
29         self.map[self.keyIndex(key)].append(key_value)
30         return True
31
32     def __str__(self):
33         string = "\tModulus "+str(self.hash) +"\n"
34         for i,data in enumerate(self.map):
35             string += str(i) + "\t"+ str(data) +"\n"
36         return string
37    def __getitem__(self,key):
38         for pair in self.map[self.keyIndex(key)]:
39             if key == pair[0]:
40                 return pair[1]
41         return None
42         
43     def __delitem__(self,key):
44         for i,pair in enumerate(self.map[self.keyIndex(key)]):
            if key == pair[0]:
                self.map[self.keyIndex(key)].pop(i)

For Quadratic.
S.no Class_Hashtable.PY
1 class HashMapQuadratic:
2
3 def __init__(self, hash : int = 10):
4 self.hash = hash
5 self.map = self.createMap(hash)
6
7 def createMap(self,hash):
8 array = [None]*hash
9 return array
10
11 def func(self,key):
12 return key % self.hash
13
14 def keyIndex(self, key : str, plus : int):
15 index = 0
16 for i in key:
17 index += ord(i)
18 index += plus**2
19 return self.func(index)
20
21 def __setitem__(self,key, value):
22 i=0
23 key_value = [key,value]
24 while (self.map[self.keyIndex(key,i)] != None and
25 self.map[self.keyIndex(key,i)][0] != key):
26 i+=1
27 self.map[self.keyIndex(key,i)] = key_value
28 return True
29
30 def __str__(self):
31 string = "\tModulus "+str(self.hash) +"\n"
32 for i,data in enumerate(self.map):
33 string += str(i) + "\t"+ str(data) +"\n"
34 return string
35
36 def __getitem__(self,key):
37 i=0
38 while (self.keyIndex(key,i) < len(self.map)):
39 if self.map[self.keyIndex(key,i)][0] == key:
40 return self.map[self.keyIndex(key,i)]
41 else:
42 i=+1
43 self.map[self.keyIndex(key,i)]
44 return None

        

For Linear.
S.no Class_Hashtable.PY
1 class HashMapLinear:
2
3 def __init__(self, hash : int = 10):
4 self.hash = hash
5 self.map = self.createMap(hash)
6
7 def createMap(self,hash):
8 array = [None]*hash
9 return array
10
11 def func(self,key):
12 return key % self.hash
13
14 def keyIndex(self, key : str, plus : int):
15 index = 0
16 for i in key:
17 index += ord(i)
18 index += plus
19 return self.func(index)
20
21 def __setitem__(self,key, value):
22 i=0
23 key_value = [key,value]
24 while (self.map[self.keyIndex(key,i)] != None and
25 self.map[self.keyIndex(key,i)][0] != key):
26 i+=1
27 self.map[self.keyIndex(key,i)] = key_value
28 return True
29
30 def __str__(self):
31 string = "\tModulus "+str(self.hash) +"\n"
32 for i,data in enumerate(self.map):
33 string += str(i) + "\t"+ str(data) +"\n"
34 return string
35
36 def __getitem__(self,key):
37 i=0
38 while (self.keyIndex(key,i) < len(self.map)):
39 if self.map[self.keyIndex(key,i)][0] == key:
40 return self.map[self.keyIndex(key,i)]
41 else:
42 i=+1
self.map[self.keyIndex(key,i)]
return None

       
Assessment 1 (HashMapChain):

Result:
Assessment 2 (HashMapLinear):
Result:

Assessment 3 (HashMapQuadratic)
Result:

You might also like