реализована хэш таблица, с помощью функций отсартировки, удаления, поиска, вставки/обновления. добавлена глубина рекурсиидля bst удаления
This commit is contained in:
novikovsd 2026-05-24 13:21:49 +00:00
parent 706e7b5e7f
commit 132e7e049b

View File

@ -4,6 +4,7 @@ import csv
import os import os
import sys import sys
sys.setrecursionlimit(30000)
def ll_insert(head, name, phone): def ll_insert(head, name, phone):
curr = head curr = head
@ -12,7 +13,6 @@ def ll_insert(head, name, phone):
curr['phone'] = phone curr['phone'] = phone
return head return head
curr = curr['next'] curr = curr['next']
# Вставка в начало (проще и быстрее)
new_node = {'name': name, 'phone': phone, 'next': head} new_node = {'name': name, 'phone': phone, 'next': head}
return new_node return new_node
@ -49,4 +49,40 @@ def ll_list_all(head):
entries.append((curr['name'], curr['phone'])) entries.append((curr['name'], curr['phone']))
curr = curr['next'] curr = curr['next']
entries.sort(key=lambda x: x[0]) 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 return entries