hash
реализована хэш таблица, с помощью функций отсартировки, удаления, поиска, вставки/обновления. добавлена глубина рекурсиидля bst удаления
This commit is contained in:
parent
706e7b5e7f
commit
132e7e049b
|
|
@ -4,6 +4,7 @@ import csv
|
|||
import os
|
||||
import sys
|
||||
|
||||
sys.setrecursionlimit(30000)
|
||||
|
||||
def ll_insert(head, name, phone):
|
||||
curr = head
|
||||
|
|
@ -12,7 +13,6 @@ def ll_insert(head, name, phone):
|
|||
curr['phone'] = phone
|
||||
return head
|
||||
curr = curr['next']
|
||||
# Вставка в начало (проще и быстрее)
|
||||
new_node = {'name': name, 'phone': phone, 'next': head}
|
||||
return new_node
|
||||
|
||||
|
|
@ -50,3 +50,39 @@ def ll_list_all(head):
|
|||
curr = curr['next']
|
||||
entries.sort(key=lambda x: x[0])
|
||||
return entries
|
||||
|
||||
def _hash(name, bucket_count):
|
||||
h = 0
|
||||
for ch in name:
|
||||
h = (h * 31 + ord(ch)) % bucket_count
|
||||
return h
|
||||
|
||||
|
||||
def ht_create(bucket_count=2000):
|
||||
return [None] * bucket_count
|
||||
|
||||
|
||||
def ht_insert(buckets, name, phone):
|
||||
idx = _hash(name, len(buckets))
|
||||
buckets[idx] = ll_insert(buckets[idx], name, phone)
|
||||
|
||||
|
||||
def ht_find(buckets, name):
|
||||
idx = _hash(name, len(buckets))
|
||||
return ll_find(buckets[idx], name)
|
||||
|
||||
|
||||
def ht_delete(buckets, name):
|
||||
idx = _hash(name, len(buckets))
|
||||
buckets[idx] = ll_delete(buckets[idx], name)
|
||||
|
||||
|
||||
def ht_list_all(buckets):
|
||||
entries = []
|
||||
for head in buckets:
|
||||
curr = head
|
||||
while curr is not None:
|
||||
entries.append((curr['name'], curr['phone']))
|
||||
curr = curr['next']
|
||||
entries.sort(key=lambda x: x[0])
|
||||
return entries
|
||||
Loading…
Reference in New Issue
Block a user