forked from UNN/2026-rff_mp
84 lines
1.8 KiB
Python
84 lines
1.8 KiB
Python
|
|
bst_code = ''
|
|
|
|
|
|
|
|
def bst_insert(root, name, phone):
|
|
|
|
if root is None:
|
|
return {'name': name, 'phone': phone, 'left': None, 'right': None}
|
|
|
|
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(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)
|
|
elif name > root['name']:
|
|
root['right'] = bst_delete(root['right'], name)
|
|
else:
|
|
|
|
if root['left'] is None:
|
|
return root['right']
|
|
elif root['right'] is None:
|
|
return root['left']
|
|
else:
|
|
|
|
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_list_all(root):
|
|
|
|
result = []
|
|
|
|
def in_order(node):
|
|
if node is not None:
|
|
in_order(node['left'])
|
|
result.append((node['name'], node['phone']))
|
|
in_order(node['right'])
|
|
|
|
in_order(root)
|
|
return result
|
|
|
|
|
|
with open('/mnt/agents/output/lab1/src/bst.py', 'w', encoding='utf-8') as f:
|
|
f.write(bst_code)
|
|
|
|
print("✅ bst.py создан")
|