2026-05-23 10:23:41 +00:00
|
|
|
import random as rnd
|
2026-05-23 16:22:09 +00:00
|
|
|
|
2026-05-23 10:23:41 +00:00
|
|
|
|
|
|
|
|
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
|
2026-05-23 13:12:52 +00:00
|
|
|
if current['next'] is None:
|
|
|
|
|
break
|
2026-05-23 10:23:41 +00:00
|
|
|
current = current['next']
|
|
|
|
|
|
2026-05-23 13:12:52 +00:00
|
|
|
current['next'] = new_node
|
|
|
|
|
return head
|
2026-05-23 10:23:41 +00:00
|
|
|
|
2026-05-23 11:47:03 +00:00
|
|
|
def ll_find(head, name):
|
|
|
|
|
current = head
|
|
|
|
|
while current is not None:
|
2026-05-23 13:12:52 +00:00
|
|
|
if current['name'] == name:
|
2026-05-23 11:47:03 +00:00
|
|
|
return current['phone']
|
2026-05-23 13:12:52 +00:00
|
|
|
current = current['next']
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2026-05-23 11:47:03 +00:00
|
|
|
def ll_delete(head, name):
|
|
|
|
|
if head is None:
|
|
|
|
|
return None
|
2026-05-23 13:12:52 +00:00
|
|
|
if head['name'] == name:
|
2026-05-23 11:47:03 +00:00
|
|
|
return head['next']
|
|
|
|
|
|
2026-05-23 13:12:52 +00:00
|
|
|
current = head
|
2026-05-23 11:47:03 +00:00
|
|
|
while current['next'] is not None:
|
|
|
|
|
if current['next']['name'] == name:
|
2026-05-23 13:12:52 +00:00
|
|
|
current['next'] = current['next']['next']
|
2026-05-23 11:47:03 +00:00
|
|
|
return head
|
2026-05-23 13:12:52 +00:00
|
|
|
current = current['next']
|
|
|
|
|
return head
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sort_records(lst):
|
|
|
|
|
n = len(lst)
|
|
|
|
|
for i in range(n):
|
|
|
|
|
for j in range(0, n-i-1):
|
|
|
|
|
if lst[j][0] > lst[j + 1][0]:
|
|
|
|
|
lst[j], lst[j + 1] = lst[j + 1], lst[j]
|
|
|
|
|
return lst
|
|
|
|
|
|
|
|
|
|
|
2026-05-23 11:47:03 +00:00
|
|
|
def ll_list_all(head):
|
2026-05-23 13:12:52 +00:00
|
|
|
record = []
|
|
|
|
|
current = head
|
2026-05-23 11:47:03 +00:00
|
|
|
while current is not None:
|
2026-05-23 13:12:52 +00:00
|
|
|
record += [(current['name'], current['phone'])]
|
|
|
|
|
current = current['next']
|
|
|
|
|
res = sort_records(record)
|
2026-05-23 13:31:09 +00:00
|
|
|
return res
|
2026-05-23 16:22:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def def_hash(name, size):
|
|
|
|
|
hash_value = 0
|
|
|
|
|
for char in name:
|
|
|
|
|
hash_value += ord(char)
|
|
|
|
|
final_idx = hash_value % size
|
|
|
|
|
return final_idx
|
|
|
|
|
|
2026-05-23 16:49:23 +00:00
|
|
|
|
2026-05-23 16:22:09 +00:00
|
|
|
def ht_insert(buckets, name, phone):
|
|
|
|
|
size = len(buckets)
|
|
|
|
|
idx = def_hash(name, size)
|
|
|
|
|
ll_insert(buckets[idx], name, phone)
|
|
|
|
|
|
2026-05-23 16:49:23 +00:00
|
|
|
|
|
|
|
|
def ht_find(buckets, name):
|
|
|
|
|
size = len(buckets)
|
|
|
|
|
idx = def_hash(name, size)
|
|
|
|
|
return ll_find(buckets[idx], name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ht_delete(buckets, name):
|
|
|
|
|
size = len(buckets)
|
|
|
|
|
idx = def_hash(name, size)
|
|
|
|
|
buckets[idx] = ll_delete(buckets[idx], name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ht_list_all(buckets):
|
|
|
|
|
res = []
|
|
|
|
|
for head in buckets:
|
|
|
|
|
current = head
|
|
|
|
|
while current is not None:
|
|
|
|
|
res += [(current['name'], current['phone'])]
|
|
|
|
|
current = current['next']
|
2026-05-23 21:22:14 +00:00
|
|
|
return sort_records(res)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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']:
|
|
|
|
|
current['phone'] = phone
|
|
|
|
|
break
|
|
|
|
|
elif name < current['name']:
|
|
|
|
|
if current['left'] is None:
|
|
|
|
|
current['left'] = new_node
|
|
|
|
|
break
|
|
|
|
|
current = current['left']
|
|
|
|
|
else:
|
|
|
|
|
if current['right'] is None:
|
|
|
|
|
current['right'] = new_node
|
|
|
|
|
break
|
|
|
|
|
current = current['right']
|
|
|
|
|
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def bst_find(root, name):
|
|
|
|
|
current = root
|
|
|
|
|
while current is not None:
|
|
|
|
|
if name == current['name']:
|
|
|
|
|
return current['phone']
|
|
|
|
|
elif name < current['name']:
|
|
|
|
|
current = current['left']
|
|
|
|
|
else:
|
|
|
|
|
current = current['right']
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def bst_delete(root, name):
|
|
|
|
|
if root is None:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
p_node = None
|
|
|
|
|
current = root
|
|
|
|
|
|
|
|
|
|
while current is not None and current['name'] != name:
|
|
|
|
|
p_node = current
|
|
|
|
|
if name < current['name']:
|
|
|
|
|
current = current['left']
|
|
|
|
|
else:
|
|
|
|
|
current = current['right']
|
|
|
|
|
|
|
|
|
|
if current is None:
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
|
|
if current['left'] is None and current['right'] is None:
|
|
|
|
|
if p_node is None:
|
|
|
|
|
return None
|
|
|
|
|
if p_node['left'] is current:
|
|
|
|
|
p_node['left'] = None
|
|
|
|
|
else:
|
|
|
|
|
p_node['right'] = None
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
|
|
if current['left'] is None:
|
|
|
|
|
c_node = current['right']
|
|
|
|
|
elif current['right'] is None:
|
|
|
|
|
c_node = current['left']
|
|
|
|
|
else:
|
|
|
|
|
succ_parent = current
|
|
|
|
|
succ = current['right']
|
|
|
|
|
while succ['left']:
|
|
|
|
|
succ_parent = succ
|
|
|
|
|
succ = succ['left']
|
|
|
|
|
|
|
|
|
|
current['name'], current['phone'] = succ['name'], succ['phone']
|
|
|
|
|
|
|
|
|
|
if succ_parent['left'] is succ:
|
|
|
|
|
succ_parent['left'] = succ['right']
|
|
|
|
|
else:
|
|
|
|
|
succ_parent['right'] = succ['right']
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
|
|
if p_node is None:
|
|
|
|
|
return c_node
|
|
|
|
|
if p_node['left'] is current:
|
|
|
|
|
p_node['left'] = c_node
|
|
|
|
|
else:
|
|
|
|
|
p_node['right'] = c_node
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def bst_list_all(root):
|
|
|
|
|
record = []
|
|
|
|
|
|
|
|
|
|
def helper(node):
|
|
|
|
|
if node is not None:
|
|
|
|
|
helper(node['left'])
|
|
|
|
|
nonlocal record
|
|
|
|
|
record += [(node['name'], node['phone'])]
|
|
|
|
|
helper(node['right'])
|
|
|
|
|
|
|
|
|
|
helper(root)
|
|
|
|
|
return record
|