-
Notifications
You must be signed in to change notification settings - Fork 0
/
doublehash_in_python.py
116 lines (97 loc) · 3.36 KB
/
doublehash_in_python.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class HashTable:
def __init__(self):
self.size = int(input(" Uneti Velicinu tabele (za ovaj zadatak uneti broj 7) : "))
self.num = 3
self.table = list(0 for i in range(self.size))
self.elementCount = 0
self.comparisons = 0
def isFull(self):
if self.elementCount == self.size:
return True
else:
return False
def h1(self, element):
return element % self.size
def h2(self, element):
return 2 + (element % self.num)
def doubleHashing(self, element, position):
posFound = False
limit = 20
i = 2
# start a loop to find the position
newPosition = self.h1(element)
while i <= limit:
newPosition = (newPosition + self.h2(element)) % self.size
if self.table[newPosition] == 0:
posFound = True
break
else:
i += 1
return posFound, newPosition
def insert(self, element):
if self.isFull():
print("Hash Table Full")
return False
posFound = False
position = self.h1(element)
if self.table[position] == 0:
self.table[position] = element
print("Element " + str(element) + " na poziciji " + str(position))
isStored = True
self.elementCount += 1
else:
while not posFound:
print("Kolizija elementa " + str(element) + " na poziciji " + str(
position) + " trazi se nova pozicija.")
posFound, position = self.doubleHashing(element, position)
if posFound:
self.table[position] = element
self.elementCount += 1
return posFound
def search(self, element):
found = False
position = self.h1(element)
self.comparisons += 1
if (self.table[position] == element):
return position
else:
newPosition = position
while i <= limit:
position = (self.h1(element) + self.h2(element)) % self.size
self.comparisons += 1
if self.table[position] == element:
found = True
break
elif self.table[position] == 0:
found = False
break
else:
i += 1
if found:
return position
else:
print("Elementa nema")
return found
def remove(self, element):
position = self.search(element)
if position is not False:
self.table[position] = 0
print("Element " + str(element) + " je obrisan")
self.elementCount -= 1
else:
print("Elementa nema u tabeli")
return
def display(self):
print("\n")
for i in range(self.size):
print(str(i) + " = " + str(self.table[i]))
print("Brojeva u tabeli ima : " + str(self.elementCount))
if __name__ == '__main__':
table1 = HashTable()
table1.insert(45)
table1.insert(35)
table1.insert(17)
table1.insert(25)
table1.insert(18)
table1.display()
print()