-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chained_Hash_Table.py
67 lines (59 loc) · 1.49 KB
/
Chained_Hash_Table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Chained_Hash_Table:
def __init__(self, size):
self.table = [[0] for i in range(size)]
self.size = size
def hash_function(self, key):
index = key % self.size
return index
def insert(self, key):
index = self.hash_function(key)
temp = index
if (self.table[index][0] == 0):
self.table[index][0] = key
print("Insertion Successful")
else:
self.table[index].append(key)
print("Insertion Successful")
def search(self, key):
index = self.hash_function(key)
i = 0
while (i<len(self.table[index])):
if (self.table[index][i]==key):
print("Search Successfull")
return (index,i)
i += 1
if (i==len(self.table[index])):
print("Search Unsuccessfull, key not found")
def delete(self, key):
index = self.hash_function(key)
i = 0
while (i<len(self.table[index])):
if (self.table[index][i]==key):
del self.table[index][i]
print("Deletion Successfull")
break
i += 1
if (i==len(self.table[index])):
print("Deletion Unsuccessfull, key not found")
def traverse(self):
print(self.table)
# Driver Code
Table = Chained_Hash_Table(10)
Table.insert(5)
Table.traverse()
Table.insert(2)
Table.traverse()
Table.insert(35)
Table.traverse()
Table.insert(22)
Table.traverse()
Table.insert(222)
Table.traverse()
Table.insert(2222)
Table.traverse()
Table.delete(222)
Table.traverse()
Table.delete(22)
Table.traverse()
Table.search(222)
Table.search(5)