2026-09-02 22:45:10 +00:00
|
|
|
|
import random
|
|
|
|
|
|
|
|
|
|
|
|
# Sv Spisok
|
|
|
|
|
|
ll_head = None
|
|
|
|
|
|
|
|
|
|
|
|
def ll_insert(name, phone):
|
|
|
|
|
|
global ll_head
|
|
|
|
|
|
cur = ll_head
|
|
|
|
|
|
while cur is not None:
|
|
|
|
|
|
if cur['name'] == name:
|
|
|
|
|
|
cur['phone'] = phone
|
|
|
|
|
|
return
|
|
|
|
|
|
cur = cur['next']
|
|
|
|
|
|
new_node = {'name': name, 'phone': phone, 'next': ll_head}
|
|
|
|
|
|
ll_head = new_node
|
|
|
|
|
|
|
|
|
|
|
|
def ll_find(name):
|
|
|
|
|
|
cur = ll_head
|
|
|
|
|
|
while cur is not None:
|
|
|
|
|
|
if cur['name'] == name:
|
|
|
|
|
|
return cur['phone']
|
|
|
|
|
|
cur = cur['next']
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def ll_delete(name):
|
|
|
|
|
|
global ll_head
|
|
|
|
|
|
if ll_head is None:
|
|
|
|
|
|
return
|
|
|
|
|
|
if ll_head['name'] == name:
|
|
|
|
|
|
ll_head = ll_head['next']
|
|
|
|
|
|
return
|
|
|
|
|
|
prev = ll_head
|
|
|
|
|
|
cur = ll_head['next']
|
|
|
|
|
|
while cur is not None:
|
|
|
|
|
|
if cur['name'] == name:
|
|
|
|
|
|
prev['next'] = cur['next']
|
|
|
|
|
|
return
|
|
|
|
|
|
prev = cur
|
|
|
|
|
|
cur = cur['next']
|
|
|
|
|
|
|
|
|
|
|
|
def ll_list_all():
|
|
|
|
|
|
records = []
|
|
|
|
|
|
cur = ll_head
|
|
|
|
|
|
while cur is not None:
|
|
|
|
|
|
records.append((cur['name'], cur['phone']))
|
|
|
|
|
|
cur = cur['next']
|
|
|
|
|
|
records.sort(key=lambda x: x[0])
|
|
|
|
|
|
return records
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# HESH
|
|
|
|
|
|
BUCKET_COUNT = 10
|
|
|
|
|
|
buckets = [None] * BUCKET_COUNT
|
|
|
|
|
|
|
|
|
|
|
|
def hash_func(name):
|
|
|
|
|
|
s = 0
|
|
|
|
|
|
for ch in name:
|
|
|
|
|
|
s += ord(ch)
|
|
|
|
|
|
return s % BUCKET_COUNT
|
|
|
|
|
|
|
|
|
|
|
|
def ht_insert(name, phone):
|
|
|
|
|
|
global buckets
|
|
|
|
|
|
idx = hash_func(name)
|
|
|
|
|
|
cur = buckets[idx]
|
|
|
|
|
|
while cur is not None:
|
|
|
|
|
|
if cur['name'] == name:
|
|
|
|
|
|
cur['phone'] = phone
|
|
|
|
|
|
return
|
|
|
|
|
|
cur = cur['next']
|
|
|
|
|
|
new_node = {'name': name, 'phone': phone, 'next': buckets[idx]}
|
|
|
|
|
|
buckets[idx] = new_node
|
|
|
|
|
|
|
|
|
|
|
|
def ht_find(name):
|
|
|
|
|
|
idx = hash_func(name)
|
|
|
|
|
|
cur = buckets[idx]
|
|
|
|
|
|
while cur is not None:
|
|
|
|
|
|
if cur['name'] == name:
|
|
|
|
|
|
return cur['phone']
|
|
|
|
|
|
cur = cur['next']
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def ht_delete(name):
|
|
|
|
|
|
global buckets
|
|
|
|
|
|
idx = hash_func(name)
|
|
|
|
|
|
head = buckets[idx]
|
|
|
|
|
|
if head is None:
|
|
|
|
|
|
return
|
|
|
|
|
|
if head['name'] == name:
|
|
|
|
|
|
buckets[idx] = head['next']
|
|
|
|
|
|
return
|
|
|
|
|
|
prev = head
|
|
|
|
|
|
cur = head['next']
|
|
|
|
|
|
while cur is not None:
|
|
|
|
|
|
if cur['name'] == name:
|
|
|
|
|
|
prev['next'] = cur['next']
|
|
|
|
|
|
return
|
|
|
|
|
|
prev = cur
|
|
|
|
|
|
cur = cur['next']
|
|
|
|
|
|
|
|
|
|
|
|
def ht_list_all():
|
|
|
|
|
|
all_records = []
|
|
|
|
|
|
for head in buckets:
|
|
|
|
|
|
cur = head
|
|
|
|
|
|
while cur is not None:
|
|
|
|
|
|
all_records.append((cur['name'], cur['phone']))
|
|
|
|
|
|
cur = cur['next']
|
|
|
|
|
|
all_records.sort(key=lambda x: x[0])
|
|
|
|
|
|
return all_records
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-09-02 22:49:04 +00:00
|
|
|
|
|
|
|
|
|
|
# BTS
|
|
|
|
|
|
bst_root = None
|
|
|
|
|
|
|
|
|
|
|
|
def bst_create_node(name, phone):
|
|
|
|
|
|
return {'name': name, 'phone': phone, 'left': None, 'right': None}
|
|
|
|
|
|
|
|
|
|
|
|
def bst_insert(name, phone):
|
|
|
|
|
|
global bst_root
|
|
|
|
|
|
if bst_root is None:
|
|
|
|
|
|
bst_root = bst_create_node(name, phone)
|
|
|
|
|
|
return
|
|
|
|
|
|
def insert_rec(node):
|
|
|
|
|
|
if name == node['name']:
|
|
|
|
|
|
node['phone'] = phone
|
|
|
|
|
|
elif name < node['name']:
|
|
|
|
|
|
if node['left'] is None:
|
|
|
|
|
|
node['left'] = bst_create_node(name, phone)
|
|
|
|
|
|
else:
|
|
|
|
|
|
insert_rec(node['left'])
|
|
|
|
|
|
else:
|
|
|
|
|
|
if node['right'] is None:
|
|
|
|
|
|
node['right'] = bst_create_node(name, phone)
|
|
|
|
|
|
else:
|
|
|
|
|
|
insert_rec(node['right'])
|
|
|
|
|
|
insert_rec(bst_root)
|
|
|
|
|
|
|
|
|
|
|
|
def bst_find(name):
|
|
|
|
|
|
def find_rec(node):
|
|
|
|
|
|
if node is None:
|
|
|
|
|
|
return None
|
|
|
|
|
|
if name == node['name']:
|
|
|
|
|
|
return node['phone']
|
|
|
|
|
|
elif name < node['name']:
|
|
|
|
|
|
return find_rec(node['left'])
|
|
|
|
|
|
else:
|
|
|
|
|
|
return find_rec(node['right'])
|
|
|
|
|
|
return find_rec(bst_root)
|
|
|
|
|
|
|
|
|
|
|
|
def find_min(node):
|
|
|
|
|
|
while node['left'] is not None:
|
|
|
|
|
|
node = node['left']
|
|
|
|
|
|
return node
|
|
|
|
|
|
|
|
|
|
|
|
def bst_delete(name):
|
|
|
|
|
|
global bst_root
|
|
|
|
|
|
def delete_rec(node):
|
|
|
|
|
|
if node is None:
|
|
|
|
|
|
return None
|
|
|
|
|
|
if name < node['name']:
|
|
|
|
|
|
node['left'] = delete_rec(node['left'])
|
|
|
|
|
|
elif name > node['name']:
|
|
|
|
|
|
node['right'] = delete_rec(node['right'])
|
|
|
|
|
|
else:
|
|
|
|
|
|
if node['left'] is None:
|
|
|
|
|
|
return node['right']
|
|
|
|
|
|
if node['right'] is None:
|
|
|
|
|
|
return node['left']
|
|
|
|
|
|
min_node = find_min(node['right'])
|
|
|
|
|
|
node['name'] = min_node['name']
|
|
|
|
|
|
node['phone'] = min_node['phone']
|
|
|
|
|
|
node['right'] = delete_rec(node['right'])
|
|
|
|
|
|
return node
|
|
|
|
|
|
bst_root = delete_rec(bst_root)
|
|
|
|
|
|
|
|
|
|
|
|
def bst_list_all():
|
|
|
|
|
|
result = []
|
|
|
|
|
|
def inorder(node):
|
|
|
|
|
|
if node is None:
|
|
|
|
|
|
return
|
|
|
|
|
|
inorder(node['left'])
|
|
|
|
|
|
result.append((node['name'], node['phone']))
|
|
|
|
|
|
inorder(node['right'])
|
|
|
|
|
|
inorder(bst_root)
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
2026-09-02 22:45:10 +00:00
|
|
|
|
#Test
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
print("=== Тест связного списка ===")
|
|
|
|
|
|
ll_insert("Kiril", "123-456")
|
|
|
|
|
|
ll_insert("Pavel", "789-012")
|
|
|
|
|
|
ll_insert("Alina", "345-678")
|
|
|
|
|
|
ll_insert("Kolia", "111-222") # обновление
|
|
|
|
|
|
print("Все записи (список):", ll_list_all())
|
|
|
|
|
|
print("Поиск Kiril:", ll_find("Kiril"))
|
|
|
|
|
|
ll_delete("Pavel")
|
|
|
|
|
|
print("После удаления Pavel:", ll_list_all())
|
|
|
|
|
|
|
|
|
|
|
|
print("\n=== Тест хеш-таблицы ===")
|
|
|
|
|
|
ht_insert("Kiril", "123-456")
|
|
|
|
|
|
ht_insert("Pavel", "789-012")
|
|
|
|
|
|
ht_insert("Alina", "345-678")
|
|
|
|
|
|
ht_insert("Kolia", "111-222")
|
|
|
|
|
|
print("Все записи (хеш):", ht_list_all())
|
2026-09-02 22:49:04 +00:00
|
|
|
|
print("Поиск Kiril:", ht_find("Kiril"))
|
2026-09-02 22:45:10 +00:00
|
|
|
|
ht_delete("Kolia")
|
|
|
|
|
|
print("После удаления Kolia:", ht_list_all())
|
2026-09-02 22:49:04 +00:00
|
|
|
|
|
|
|
|
|
|
print("\n=== Тест BST ===")
|
|
|
|
|
|
bst_insert("Kiril", "123-456")
|
|
|
|
|
|
bst_insert("Pavel", "789-012")
|
|
|
|
|
|
bst_insert("Alina", "345-678")
|
|
|
|
|
|
bst_insert("Kolia", "111-222")
|
|
|
|
|
|
print("Все записи (BST):", bst_list_all())
|
|
|
|
|
|
print("Поиск Pavel:", bst_find("Pavel"))
|
|
|
|
|
|
bst_delete("Pavel")
|
|
|
|
|
|
print("После удаления Pavel:", bst_list_all())
|