implemented hash table

This commit is contained in:
not why 2026-05-25 00:42:24 +03:00
parent 72d2269634
commit fb36061d9a

View File

@ -50,4 +50,42 @@ def ll_list_all(head):
result.append((node['name'], node['phone']))
node = node['next']
result.sort(key=lambda x: x[0])
return result
# 2. ХЕШ-ТАБЛИЦА (цепочки через связный список)
HT_SIZE = 1024 # число корзин (степень двойки)
def ht_create(size=HT_SIZE):
return [None] * size
def _ht_hash(name, size):
h = 5381
for ch in name:
h = ((h << 5) + h) ^ ord(ch)
return h % size
def ht_insert(buckets, name, phone):
idx = _ht_hash(name, len(buckets))
buckets[idx] = ll_insert(buckets[idx], name, phone)
def ht_find(buckets, name):
idx = _ht_hash(name, len(buckets))
return ll_find(buckets[idx], name)
def ht_delete(buckets, name):
idx = _ht_hash(name, len(buckets))
buckets[idx] = ll_delete(buckets[idx], name)
def ht_list_all(buckets):
result = []
for head in buckets:
result.extend(ll_list_all(head))
result.sort(key=lambda x: x[0])
return result