implemented hash table
This commit is contained in:
parent
72d2269634
commit
fb36061d9a
|
|
@ -51,3 +51,41 @@ def ll_list_all(head):
|
||||||
node = node['next']
|
node = node['next']
|
||||||
result.sort(key=lambda x: x[0])
|
result.sort(key=lambda x: x[0])
|
||||||
return result
|
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
|
||||||
Loading…
Reference in New Issue
Block a user