исправлено двоичное дерево 3.0
This commit is contained in:
parent
c76eb6f91b
commit
c32fd836b5
1
konnovaea/docs/data/resurts.csv
Normal file
1
konnovaea/docs/data/resurts.csv
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -88,7 +88,7 @@ def run_linked_experiments(records, mode_name):
|
||||||
|
|
||||||
def run_hash_experiments(records, mode_name):
|
def run_hash_experiments(records, mode_name):
|
||||||
|
|
||||||
print(f"хеш-таблица({mode_name})")
|
print(f"\n хеш-таблица({mode_name})")
|
||||||
|
|
||||||
print("вставка 10000 записей:")
|
print("вставка 10000 записей:")
|
||||||
|
|
||||||
|
|
@ -158,7 +158,7 @@ def run_hash_experiments(records, mode_name):
|
||||||
|
|
||||||
def run_bst_experiments(records, mode_name):
|
def run_bst_experiments(records, mode_name):
|
||||||
|
|
||||||
print(f"двоичное дерево({mode_name})")
|
print(f"\n двоичное дерево({mode_name})")
|
||||||
|
|
||||||
print("вставка 10000 записей:")
|
print("вставка 10000 записей:")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -103,6 +103,7 @@ def bst_insert(root, name, phone):
|
||||||
|
|
||||||
|
|
||||||
def bst_find(root, name):
|
def bst_find(root, name):
|
||||||
|
|
||||||
current = root
|
current = root
|
||||||
while current is not None:
|
while current is not None:
|
||||||
if name == current['name']:
|
if name == current['name']:
|
||||||
|
|
@ -113,41 +114,74 @@ def bst_find(root, name):
|
||||||
current = current['right']
|
current = current['right']
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _bst_find_mine(node):
|
|
||||||
|
def _bst_find_min(node):
|
||||||
|
|
||||||
current = node
|
current = node
|
||||||
while current and current['left'] is not None:
|
while current['left'] is not None:
|
||||||
current = current['left']
|
current = current['left']
|
||||||
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']:
|
parent = None
|
||||||
root['left'] = bst_delete(root['left'], name)
|
current = root
|
||||||
elif name > root['name']:
|
|
||||||
root['right'] = bst_delete(root['right'], name)
|
|
||||||
return root
|
|
||||||
|
|
||||||
if root['left'] is None:
|
while current is not None and current['name'] != name:
|
||||||
return root['right']
|
parent = current
|
||||||
elif root['right'] is None:
|
if name < current['name']:
|
||||||
return root['left']
|
|
||||||
|
|
||||||
current = root['right']
|
|
||||||
while current['left'] is not None:
|
|
||||||
current = current['left']
|
current = current['left']
|
||||||
|
else:
|
||||||
|
current = current['right']
|
||||||
|
|
||||||
root['name'] = current['name']
|
if current is None:
|
||||||
root['phone'] = current['phone']
|
return root
|
||||||
|
|
||||||
root['right'] = bst_delete(root['right'], current['name'])
|
if current['left'] is None and current['right'] is None:
|
||||||
|
if parent is None:
|
||||||
|
return None
|
||||||
|
if parent['left'] == current:
|
||||||
|
parent['left'] = None
|
||||||
|
else:
|
||||||
|
parent['right'] = None
|
||||||
|
return root
|
||||||
|
|
||||||
|
if current['left'] is None:
|
||||||
|
child = current['right']
|
||||||
|
elif current['right'] is None:
|
||||||
|
child = current['left']
|
||||||
|
else:
|
||||||
|
successor_parent = current
|
||||||
|
successor = current['right']
|
||||||
|
while successor['left'] is not None:
|
||||||
|
successor_parent = successor
|
||||||
|
successor = successor['left']
|
||||||
|
|
||||||
|
current['name'] = successor['name']
|
||||||
|
current['phone'] = successor['phone']
|
||||||
|
|
||||||
|
if successor_parent['left'] == successor:
|
||||||
|
successor_parent['left'] = successor['right']
|
||||||
|
else:
|
||||||
|
successor_parent['right'] = successor['right']
|
||||||
|
|
||||||
return root
|
return root
|
||||||
|
|
||||||
|
if parent is None:
|
||||||
|
return child
|
||||||
|
if parent['left'] == current:
|
||||||
|
parent['left'] = child
|
||||||
|
else:
|
||||||
|
parent['right'] = child
|
||||||
|
|
||||||
|
return root
|
||||||
|
|
||||||
|
|
||||||
def bst_list_all(root):
|
def bst_list_all(root):
|
||||||
|
|
||||||
records = []
|
records = []
|
||||||
|
|
||||||
def inorder(node):
|
def inorder(node):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user