@@ -52,6 +52,30 @@ def balanced_factor(self):
52
52
)
53
53
54
54
def hash_function (self , key ):
55
+ """
56
+ Generates hash for the given key value
57
+
58
+ Examples:
59
+
60
+ Creating HashTable with size 5
61
+ >>> ht = HashTable(5)
62
+ >>> ht.hash_function(10)
63
+ 0
64
+ >>> ht.hash_function(20)
65
+ 0
66
+ >>> ht.hash_function(4)
67
+ 4
68
+ >>> ht.hash_function(18)
69
+ 3
70
+ >>> ht.hash_function(-18)
71
+ 2
72
+ >>> ht.hash_function(18.5)
73
+ 3.5
74
+ >>> ht.hash_function(0)
75
+ 0
76
+ >>> ht.hash_function(-0)
77
+ 0
78
+ """
55
79
return key % self .size_table
56
80
57
81
def _step_by_step (self , step_ord ):
@@ -105,10 +129,99 @@ def bulk_insert(self, values):
105
129
i += 1
106
130
107
131
def _set_value (self , key , data ):
132
+ """
133
+ _set_value functions allows to update value at a particular hash
134
+
135
+ Examples:
136
+ 1. _set_value in HashTable of size 5
137
+ >>> ht = HashTable(5)
138
+ >>> ht.insert_data(10)
139
+ >>> ht.insert_data(20)
140
+ >>> ht.insert_data(30)
141
+ >>> ht._set_value(0,15)
142
+ >>> ht.keys()
143
+ {0: 15, 1: 20, 2: 30}
144
+
145
+ 2. _set_value in HashTable of size 2
146
+ >>> ht = HashTable(2)
147
+ >>> ht.insert_data(17)
148
+ >>> ht.insert_data(18)
149
+ >>> ht.insert_data(99)
150
+ >>> ht._set_value(3,15)
151
+ >>> ht.keys()
152
+ {3: 15, 2: 17, 4: 99}
153
+
154
+ 3. _set_value in HashTable when hash is not present
155
+ >>> ht = HashTable(2)
156
+ >>> ht.insert_data(17)
157
+ >>> ht.insert_data(18)
158
+ >>> ht.insert_data(99)
159
+ >>> ht._set_value(0,15)
160
+ >>> ht.keys()
161
+ {3: 18, 2: 17, 4: 99, 0: 15}
162
+
163
+ 4. _set_value in HashTable when multiple hash are not present
164
+ >>> ht = HashTable(2)
165
+ >>> ht.insert_data(17)
166
+ >>> ht.insert_data(18)
167
+ >>> ht.insert_data(99)
168
+ >>> ht._set_value(0,15)
169
+ >>> ht._set_value(1,20)
170
+ >>> ht.keys()
171
+ {3: 18, 2: 17, 4: 99, 0: 15, 1: 20}
172
+ """
108
173
self .values [key ] = data
109
174
self ._keys [key ] = data
110
175
111
176
def _collision_resolution (self , key , data = None ):
177
+ """
178
+ This method is a type of open addressing which is used for handling collision.
179
+
180
+ In this implementation the concept of linear probing has been used.
181
+
182
+ The hash table is searched sequentially from the original location of the
183
+ hash, if the new hash/location we get is already occupied we check for the next
184
+ hash/location.
185
+
186
+ references:
187
+ - https://en.wikipedia.org/wiki/Linear_probing
188
+
189
+ Examples:
190
+ 1. The collision will be with keys 18 & 99, so new hash will be created for 99
191
+ >>> ht = HashTable(3)
192
+ >>> ht.insert_data(17)
193
+ >>> ht.insert_data(18)
194
+ >>> ht.insert_data(99)
195
+ >>> ht.keys()
196
+ {2: 17, 0: 18, 1: 99}
197
+
198
+ 2. The collision will be with keys 17 & 101, so new hash
199
+ will be created for 101
200
+ >>> ht = HashTable(4)
201
+ >>> ht.insert_data(17)
202
+ >>> ht.insert_data(18)
203
+ >>> ht.insert_data(99)
204
+ >>> ht.insert_data(101)
205
+ >>> ht.keys()
206
+ {1: 17, 2: 18, 3: 99, 0: 101}
207
+
208
+ 2. The collision will be with all keys, so new hash will be created for all
209
+ >>> ht = HashTable(1)
210
+ >>> ht.insert_data(17)
211
+ >>> ht.insert_data(18)
212
+ >>> ht.insert_data(99)
213
+ >>> ht.keys()
214
+ {2: 17, 3: 18, 4: 99}
215
+
216
+ 3. Trying to insert float key in hash
217
+ >>> ht = HashTable(1)
218
+ >>> ht.insert_data(17)
219
+ >>> ht.insert_data(18)
220
+ >>> ht.insert_data(99.99)
221
+ Traceback (most recent call last):
222
+ ...
223
+ TypeError: list indices must be integers or slices, not float
224
+ """
112
225
new_key = self .hash_function (key + 1 )
113
226
114
227
while self .values [new_key ] is not None and self .values [new_key ] != key :
0 commit comments