двоичное дерево исправлено 2.0
This commit is contained in:
parent
0046bde759
commit
c76eb6f91b
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user