forked from UNN/2026-rff_mp
[1] add phonebook structures
This commit is contained in:
parent
82e988c965
commit
849eaf018d
134
raskatovia/docs/data/task1/spravochnik.py
Normal file
134
raskatovia/docs/data/task1/spravochnik.py
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
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):
|
||||
if root is None:
|
||||
return {"name": name, "phone": phone, "left": None, "right": None}
|
||||
if name < root["name"]:
|
||||
root["left"] = bst_insert(root["left"], name, phone)
|
||||
elif name > root["name"]:
|
||||
root["right"] = bst_insert(root["right"], name, phone)
|
||||
else:
|
||||
root["phone"] = phone
|
||||
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_min_node(root):
|
||||
current = root
|
||||
while current["left"] is not None:
|
||||
current = current["left"]
|
||||
return current
|
||||
|
||||
def bst_delete(root, name):
|
||||
if root is None:
|
||||
return None
|
||||
if name < root["name"]:
|
||||
root["left"] = bst_delete(root["left"], name)
|
||||
return root
|
||||
if name > root["name"]:
|
||||
root["right"] = bst_delete(root["right"], name)
|
||||
return root
|
||||
if root["left"] is None:
|
||||
return root["right"]
|
||||
if root["right"] is None:
|
||||
return root["left"]
|
||||
replacement = bst_min_node(root["right"])
|
||||
root["name"] = replacement["name"]
|
||||
root["phone"] = replacement["phone"]
|
||||
root["right"] = bst_delete(root["right"], replacement["name"])
|
||||
return root
|
||||
|
||||
def bst_list_all(root):
|
||||
records = []
|
||||
def walk(node):
|
||||
if node is None:
|
||||
return
|
||||
walk(node["left"])
|
||||
records.append((node["name"], node["phone"]))
|
||||
walk(node["right"])
|
||||
walk(root)
|
||||
return records
|
||||
Loading…
Reference in New Issue
Block a user