2026-rff_mp/anikinvd/docs/data/1-st-exercise/phonebook.py

163 lines
4.1 KiB
Python
Raw Normal View History

2026-05-22 17:49:01 +00:00
def llist_insert(head, name, phone):
current = head
while current is not None:
if current['name'] == name:
current['phone'] = phone
return head
current = current['next']
new_node = {'name': name, 'phone': phone, 'next': None}
if head is None:
return new_node
current = head
while current['next'] is not None:
current = current['next']
current['next'] = new_node
return head
def llist_find(head, name):
current = head
while current is not None:
if current['name'] == name:
return current['phone']
current = current['next']
return None
def llist_delete(head, name):
if head is None:
return None
if head['name'] == name:
return head['next']
prev = head
current = head['next']
while current is not None:
if current['name'] == name:
prev['next'] = current['next']
return head
prev = current
current = current['next']
return head
def llist_get_all(head):
entries = []
current = head
while current is not None:
entries.append((current['name'], current['phone']))
current = current['next']
entries.sort(key=lambda x: x[0])
return entries
2026-05-22 17:49:33 +00:00
BUCKET_SIZE = 1000
def ht_create():
return [None] * BUCKET_SIZE
def ht_insert(table, name, phone):
idx = hash(name) % len(table)
table[idx] = llist_insert(table[idx], name, phone)
return table
def ht_find(table, name):
idx = hash(name) % len(table)
return llist_find(table[idx], name)
def ht_delete(table, name):
idx = hash(name) % len(table)
table[idx] = llist_delete(table[idx], name)
return table
def ht_get_all(table):
all_entries = []
for head in table:
current = head
while current is not None:
all_entries.append((current['name'], current['phone']))
current = current['next']
all_entries.sort(key=lambda x: x[0])
return all_entries
2026-05-22 17:50:06 +00:00
def bst_create_node(name, phone):
return {'name': name, 'phone': phone, 'left': None, 'right': None}
def bst_insert(root, name, phone):
if root is None:
return bst_create_node(name, phone)
if name == root['name']:
root['phone'] = phone
elif name < root['name']:
root['left'] = bst_insert(root['left'], name, phone)
else:
root['right'] = bst_insert(root['right'], name, phone)
return root
def bst_find(root, name):
if root is None:
return None
if name == root['name']:
return root['phone']
elif name < root['name']:
return bst_find(root['left'], name)
else:
return bst_find(root['right'], name)
def bst_find_min(node):
while node['left'] is not None:
node = node['left']
return node
def bst_delete(root, name):
if root is None:
return None
if name < root['name']:
root['left'] = bst_delete(root['left'], name)
elif name > root['name']:
root['right'] = bst_delete(root['right'], name)
else:
if root['left'] is None:
return root['right']
if root['right'] is None:
return root['left']
min_node = bst_find_min(root['right'])
root['name'] = min_node['name']
root['phone'] = min_node['phone']
root['right'] = bst_delete(root['right'], min_node['name'])
return root
def bst_inorder_collect(root, out_list):
if root is not None:
bst_inorder_collect(root['left'], out_list)
out_list.append((root['name'], root['phone']))
bst_inorder_collect(root['right'], out_list)
def bst_get_all(root):
result = []
bst_inorder_collect(root, result)
return result
2026-05-22 17:49:01 +00:00
if __name__ == '__main__':
2026-05-22 17:50:06 +00:00
root = None
root = bst_insert(root, "Charlie", "111-111")
root = bst_insert(root, "Alice", "222-222")
root = bst_insert(root, "Bob", "333-333")
root = bst_insert(root, "Alice", "444-444")
print(bst_find(root, "Alice"))
print(bst_find(root, "Bob"))
print(bst_get_all(root))
root = bst_delete(root, "Bob")
print(bst_get_all(root))