2026-rff_mp/raskatovia/docs/data/task1/spravochnik.py
2026-05-18 15:01:39 +03:00

154 lines
4.3 KiB
Python

def sort_records(records):
return sorted(records, key=lambda item: item[0])
def ll_insert(head, name, phone):
new_node = {"name": name, "phone": phone, "next": None}
if head is None:
return new_node
current = head
while current is not None:
if current["name"] == name:
current["phone"] = phone
return head
if current["next"] is None:
break
current = current["next"]
current["next"] = new_node
return head
def ll_find(head, name):
current = head
while current is not None:
if current["name"] == name:
return current["phone"]
current = current["next"]
return None
def ll_delete(head, name):
if head is None:
return None
if head["name"] == name:
return head["next"]
current = head
while current["next"] is not None:
if current["next"]["name"] == name:
current["next"] = current["next"]["next"]
return head
current = current["next"]
return head
def ll_list_all(head):
records = []
current = head
while current is not None:
records.append((current["name"], current["phone"]))
current = current["next"]
return sort_records(records)
def ht_create(size=101):
return [None] * size
def get_bucket_index(name, size):
total = 0
for symbol in name:
total += ord(symbol)
return total % size
def ht_insert(buckets, name, phone):
index = get_bucket_index(name, len(buckets))
buckets[index] = ll_insert(buckets[index], name, phone)
def ht_find(buckets, name):
index = get_bucket_index(name, len(buckets))
return ll_find(buckets[index], name)
def ht_delete(buckets, name):
index = get_bucket_index(name, len(buckets))
buckets[index] = ll_delete(buckets[index], name)
def ht_list_all(buckets):
records = []
for bucket in buckets:
current = bucket
while current is not None:
records.append((current["name"], current["phone"]))
current = current["next"]
return sort_records(records)
def bst_insert(root, name, phone):
new_node = {"name": name, "phone": phone, "left": None, "right": None}
if root is None:
return new_node
current = root
while True:
if name < current["name"]:
if current["left"] is None:
current["left"] = new_node
break
current = current["left"]
elif name > current["name"]:
if current["right"] is None:
current["right"] = new_node
break
current = current["right"]
else:
current["phone"] = phone
break
return root
def bst_find(root, name):
current = root
while current is not None:
if name == current["name"]:
return current["phone"]
if name < current["name"]:
current = current["left"]
else:
current = current["right"]
return None
def bst_delete(root, name):
parent = None
current = root
while current is not None and current["name"] != name:
parent = current
if name < current["name"]:
current = current["left"]
else:
current = current["right"]
if current is None:
return root
if current["left"] is not None and current["right"] is not None:
replacement_parent = current
replacement = current["right"]
while replacement["left"] is not None:
replacement_parent = replacement
replacement = replacement["left"]
current["name"] = replacement["name"]
current["phone"] = replacement["phone"]
parent = replacement_parent
current = replacement
if current["left"] is not None:
child = current["left"]
else:
child = current["right"]
if parent is None:
return child
if parent["left"] is current:
parent["left"] = child
else:
parent["right"] = child
return root
def bst_list_all(root):
records = []
stack = []
current = root
while current is not None or stack:
while current is not None:
stack.append(current)
current = current["left"]
current = stack.pop()
records.append((current["name"], current["phone"]))
current = current["right"]
return records