двоичное дерево исправлено 2.0

This commit is contained in:
konnovaea 2026-04-26 20:49:48 +03:00
parent 0046bde759
commit c76eb6f91b

View File

@ -120,22 +120,29 @@ def _bst_find_mine(node):
return current return current
def bst_delete(root, name): def bst_delete(root, name):
if root is None: if root is None:
return None return None
if name < root['name']: if name < root['name']:
root['left'] = bst_delete(root['left'], name) root['left'] = bst_delete(root['left'], name)
elif name > root['name']: elif name > root['name']:
root['right'] = bst_delete(root['right'], name) root['right'] = bst_delete(root['right'], name)
else: return root
if root['left'] is None:
return root['right'] if root['left'] is None:
elif root['right'] is None: return root['right']
return root['left'] elif root['right'] is None:
return root['left']
successor = _bst_find_mine(root['right']) current = root['right']
root['name'] = successor['name'] while current['left'] is not None:
root['phone'] = successor['phone'] current = current['left']
root['right'] = bst_delete(root['right'], successor['name'])
root['name'] = current['name']
root['phone'] = current['phone']
root['right'] = bst_delete(root['right'], current['name'])
return root return root