2018-03-20 23:48:58 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-09-28 21:41:04 +00:00
|
|
|
from .hash_table import HashTable
|
2018-03-20 23:48:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class QuadraticProbing(HashTable):
|
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
Basic Hash Table example with open addressing using Quadratic Probing
|
2018-03-20 23:48:58 +00:00
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2018-03-20 23:48:58 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2020-03-16 10:19:13 +00:00
|
|
|
def _collision_resolution(self, key, data=None):
|
2023-10-26 15:21:45 +00:00
|
|
|
"""
|
|
|
|
Quadratic probing is an open addressing scheme used for resolving
|
|
|
|
collisions in hash table.
|
|
|
|
|
|
|
|
It works by taking the original hash index and adding successive
|
|
|
|
values of an arbitrary quadratic polynomial until open slot is found.
|
|
|
|
|
|
|
|
Hash + 1², Hash + 2², Hash + 3² .... Hash + n²
|
|
|
|
|
|
|
|
reference:
|
|
|
|
- https://en.wikipedia.org/wiki/Quadratic_probing
|
|
|
|
e.g:
|
|
|
|
1. Create hash table with size 7
|
|
|
|
>>> qp = QuadraticProbing(7)
|
|
|
|
>>> qp.insert_data(90)
|
|
|
|
>>> qp.insert_data(340)
|
|
|
|
>>> qp.insert_data(24)
|
|
|
|
>>> qp.insert_data(45)
|
|
|
|
>>> qp.insert_data(99)
|
|
|
|
>>> qp.insert_data(73)
|
|
|
|
>>> qp.insert_data(7)
|
|
|
|
>>> qp.keys()
|
|
|
|
{11: 45, 14: 99, 7: 24, 0: 340, 5: 73, 6: 90, 8: 7}
|
|
|
|
|
|
|
|
2. Create hash table with size 8
|
|
|
|
>>> qp = QuadraticProbing(8)
|
|
|
|
>>> qp.insert_data(0)
|
|
|
|
>>> qp.insert_data(999)
|
|
|
|
>>> qp.insert_data(111)
|
|
|
|
>>> qp.keys()
|
|
|
|
{0: 0, 7: 999, 3: 111}
|
|
|
|
|
|
|
|
3. Try to add three data elements when the size is two
|
|
|
|
>>> qp = QuadraticProbing(2)
|
|
|
|
>>> qp.insert_data(0)
|
|
|
|
>>> qp.insert_data(999)
|
|
|
|
>>> qp.insert_data(111)
|
|
|
|
>>> qp.keys()
|
|
|
|
{0: 0, 4: 999, 1: 111}
|
|
|
|
|
|
|
|
4. Try to add three data elements when the size is one
|
|
|
|
>>> qp = QuadraticProbing(1)
|
|
|
|
>>> qp.insert_data(0)
|
|
|
|
>>> qp.insert_data(999)
|
|
|
|
>>> qp.insert_data(111)
|
|
|
|
>>> qp.keys()
|
|
|
|
{4: 999, 1: 111}
|
|
|
|
"""
|
|
|
|
|
2018-03-20 23:48:58 +00:00
|
|
|
i = 1
|
2019-10-05 05:14:13 +00:00
|
|
|
new_key = self.hash_function(key + i * i)
|
2018-03-20 23:48:58 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
while self.values[new_key] is not None and self.values[new_key] != key:
|
2018-03-20 23:48:58 +00:00
|
|
|
i += 1
|
2019-10-05 05:14:13 +00:00
|
|
|
new_key = (
|
|
|
|
self.hash_function(key + i * i)
|
|
|
|
if not self.balanced_factor() >= self.lim_charge
|
|
|
|
else None
|
|
|
|
)
|
2018-03-20 23:48:58 +00:00
|
|
|
|
|
|
|
if new_key is None:
|
|
|
|
break
|
|
|
|
|
|
|
|
return new_key
|
2023-10-26 15:21:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|