forked from UNN/2026-rff_mp
[9] finish with adding BST(Binar Search Tree)
This commit is contained in:
parent
bb28c3dd2f
commit
49e91066d4
|
|
@ -236,3 +236,85 @@ print("add Boris: ", root)
|
||||||
root = bst_insert(root, 'Eva', '321-123')
|
root = bst_insert(root, 'Eva', '321-123')
|
||||||
print("add Eva: ", root)
|
print("add Eva: ", root)
|
||||||
print("====== END TEST =======\n\n\n")
|
print("====== END TEST =======\n\n\n")
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
print("====== START FIND TEST ======")
|
||||||
|
print("search by Ivan`s phone: ", bst_find(root, 'Ivan'))
|
||||||
|
print("search by Eva`s phone: ", bst_find(root,'Eva'))
|
||||||
|
print("====== END TEST ====== \n\n\n")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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']
|
||||||
|
if root['right'] is None:
|
||||||
|
return root['rihgt']
|
||||||
|
|
||||||
|
min_node = 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 inorder(node):
|
||||||
|
if node is None:
|
||||||
|
return
|
||||||
|
inorder(node['left'])
|
||||||
|
result.append((node['name'], node['phone']))
|
||||||
|
inorder(node['right'])
|
||||||
|
inorder(root)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
print("====== GLOBAL TEST TREES ======")
|
||||||
|
root = None
|
||||||
|
|
||||||
|
root = bst_insert(root, "Ivan", "123-456")
|
||||||
|
print("add Ivan: ", root)
|
||||||
|
root = bst_insert(root, "Boris", "789-012")
|
||||||
|
print("add Boris: ", root)
|
||||||
|
root = bst_insert(root, "Anna", "345-678")
|
||||||
|
print("add Anna: ", root)
|
||||||
|
root = bst_insert(root, "Ivan", "111-222") # обновление
|
||||||
|
print("update Ivan: ", root)
|
||||||
|
|
||||||
|
print("Find Ivan`s phone: ",bst_find(root, "Ivan")) # 111-222
|
||||||
|
print("Find Peter`s phone: ",bst_find(root, "Petr")) # None
|
||||||
|
|
||||||
|
root = bst_delete(root, "Boris")
|
||||||
|
print("Del Boris")
|
||||||
|
print("Find Boris: ",bst_find(root, "Boris")) # None
|
||||||
|
|
||||||
|
print("Find ALL: ",bst_list_all(root)) # [('Anna','345-678'), ('Ivan','111-222')]
|
||||||
|
|
||||||
|
|
||||||
|
print("====== END TEST ======")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user