Хеш-таблица

This commit is contained in:
tseremonnikovaaa 2026-05-22 22:59:13 +03:00
parent 07fe035d63
commit 951190aa68

View File

@ -54,3 +54,33 @@ def ll_collect_all(head):
current = current['next']
records.sort(key=lambda x: x[0])
return records
def hash_function(name, size):
total = 0
for ch in name:
total = (total * 31 + ord(ch)) % size
return total
def ht_create(size=2000):
return [None] * size
def ht_insert(buckets, name, phone):
idx = hash_function(name, len(buckets))
buckets[idx] = ll_insert(buckets[idx], name, phone)
def ht_find(buckets, name):
idx = hash_function(name, len(buckets))
return ll_find(buckets[idx], name)
def ht_delete(buckets, name):
idx = hash_function(name, len(buckets))
buckets[idx] = ll_delete(buckets[idx], name)
def ht_collect_all(buckets):
all_records = []
for bucket in buckets:
current = bucket
while current is not None:
all_records.append((current['name'], current['phone']))
current = current['next']
all_records.sort(key=lambda x: x[0])
return all_records