MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / _collision_resolution

Method _collision_resolution

data_structures/hashing/hash_table.py:179–237  ·  view source on GitHub ↗

This method is a type of open addressing which is used for handling collision. In this implementation the concept of linear probing has been used. The hash table is searched sequentially from the original location of the hash, if the new hash/location we get is alr

(self, key, data=None)

Source from the content-addressed store, hash-verified

177
178 @abstractmethod
179 def _collision_resolution(self, key, data=None):
180 """
181 This method is a type of open addressing which is used for handling collision.
182
183 In this implementation the concept of linear probing has been used.
184
185 The hash table is searched sequentially from the original location of the
186 hash, if the new hash/location we get is already occupied we check for the next
187 hash/location.
188
189 references:
190 - https://en.wikipedia.org/wiki/Linear_probing
191
192 Examples:
193 1. The collision will be with keys 18 & 99, so new hash will be created for 99
194 >>> ht = HashTable(3)
195 >>> ht.insert_data(17)
196 >>> ht.insert_data(18)
197 >>> ht.insert_data(99)
198 >>> ht.keys()
199 {2: 17, 0: 18, 1: 99}
200
201 2. The collision will be with keys 17 & 101, so new hash
202 will be created for 101
203 >>> ht = HashTable(4)
204 >>> ht.insert_data(17)
205 >>> ht.insert_data(18)
206 >>> ht.insert_data(99)
207 >>> ht.insert_data(101)
208 >>> ht.keys()
209 {1: 17, 2: 18, 3: 99, 0: 101}
210
211 2. The collision will be with all keys, so new hash will be created for all
212 >>> ht = HashTable(1)
213 >>> ht.insert_data(17)
214 >>> ht.insert_data(18)
215 >>> ht.insert_data(99)
216 >>> ht.keys()
217 {2: 17, 3: 18, 4: 99}
218
219 3. Trying to insert float key in hash
220 >>> ht = HashTable(1)
221 >>> ht.insert_data(17)
222 >>> ht.insert_data(18)
223 >>> ht.insert_data(99.99)
224 Traceback (most recent call last):
225 ...
226 TypeError: list indices must be integers or slices, not float
227 """
228 new_key = self.hash_function(key + 1)
229
230 while self.values[new_key] is not None and self.values[new_key] != key:
231 if self.values.count(None) > 0:
232 new_key = self.hash_function(new_key + 1)
233 else:
234 new_key = None
235 break
236

Callers 1

insert_dataMethod · 0.95

Calls 2

hash_functionMethod · 0.95
countMethod · 0.80

Tested by

no test coverage detected