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

Method insert_data

data_structures/hashing/hash_table.py:247–277  ·  view source on GitHub ↗

insert_data is used for inserting a single element at a time in the HashTable. Examples: >>> ht = HashTable(3) >>> ht.insert_data(5) >>> ht.keys() {2: 5} >>> ht = HashTable(5) >>> ht.insert_data(30) >>> ht.insert_data(50)

(self, data)

Source from the content-addressed store, hash-verified

245 self.insert_data(value)
246
247 def insert_data(self, data):
248 """
249 insert_data is used for inserting a single element at a time in the HashTable.
250
251 Examples:
252
253 >>> ht = HashTable(3)
254 >>> ht.insert_data(5)
255 >>> ht.keys()
256 {2: 5}
257 >>> ht = HashTable(5)
258 >>> ht.insert_data(30)
259 >>> ht.insert_data(50)
260 >>> ht.keys()
261 {0: 30, 1: 50}
262 """
263 key = self.hash_function(data)
264
265 if self.values[key] is None:
266 self._set_value(key, data)
267
268 elif self.values[key] == data:
269 pass
270
271 else:
272 collision_resolution = self._collision_resolution(key, data)
273 if collision_resolution is not None:
274 self._set_value(collision_resolution, data)
275 else:
276 self.rehashing()
277 self.insert_data(data)
278
279
280if __name__ == "__main__":

Callers 2

bulk_insertMethod · 0.95
rehashingMethod · 0.95

Calls 4

hash_functionMethod · 0.95
_set_valueMethod · 0.95
_collision_resolutionMethod · 0.95
rehashingMethod · 0.95

Tested by

no test coverage detected