From 132e7e049b5bbc0e94ab1e80778e91db40c69ca5 Mon Sep 17 00:00:00 2001 From: novikovsd Date: Sun, 24 May 2026 13:21:49 +0000 Subject: [PATCH] hash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit реализована хэш таблица, с помощью функций отсартировки, удаления, поиска, вставки/обновления. добавлена глубина рекурсиидля bst удаления --- novikovsd/hashtab.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/novikovsd/hashtab.py b/novikovsd/hashtab.py index ef0f54f..c17880f 100644 --- a/novikovsd/hashtab.py +++ b/novikovsd/hashtab.py @@ -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 @@ -49,4 +49,40 @@ def ll_list_all(head): entries.append((curr['name'], curr['phone'])) 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 \ No newline at end of file