[1] implement binary search tree
This commit is contained in:
parent
0c2e487472
commit
319171fe2c
|
|
@ -80,22 +80,90 @@ def ht_list_all(table):
|
||||||
return all_records
|
return all_records
|
||||||
|
|
||||||
|
|
||||||
|
def bst_create_node(name, phone):
|
||||||
|
return {'name': name, 'phone': phone, 'left': None, 'right': None}
|
||||||
|
|
||||||
|
def bst_insert(root, name, phone):
|
||||||
|
if root is None:
|
||||||
|
return bst_create_node(name, phone)
|
||||||
|
|
||||||
|
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(node):
|
||||||
|
while node['left'] is not None:
|
||||||
|
node = node['left']
|
||||||
|
return node
|
||||||
|
|
||||||
|
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['left']
|
||||||
|
|
||||||
|
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 inorder_traverse(node):
|
||||||
|
if node is None:
|
||||||
|
return
|
||||||
|
inorder_traverse(node['left'])
|
||||||
|
result.append((node['name'], node['phone']))
|
||||||
|
inorder_traverse(node['right'])
|
||||||
|
|
||||||
|
inorder_traverse(root)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("=== Тестирование связного списка ===")
|
print("=== Тестирование связного списка ===")
|
||||||
head = None
|
head = None
|
||||||
head = ll_insert(head, "Ivan", "123-456")
|
head = ll_insert(head, "Ivan", "123-456")
|
||||||
head = ll_insert(head, "Boris", "789-012")
|
head = ll_insert(head, "Boris", "789-012")
|
||||||
head = ll_insert(head, "Anna", "345-678")
|
|
||||||
head = ll_insert(head, "Ivan", "111-222")
|
|
||||||
print("LinkedList:", ll_list_all(head))
|
print("LinkedList:", ll_list_all(head))
|
||||||
|
|
||||||
print("\n=== Тестирование хеш-таблицы ===")
|
print("\n=== Тестирование хеш-таблицы ===")
|
||||||
ht = ht_create()
|
ht = ht_create()
|
||||||
ht = ht_insert(ht, "Ivan", "123-456")
|
ht = ht_insert(ht, "Ivan", "123-456")
|
||||||
ht = ht_insert(ht, "Boris", "789-012")
|
ht = ht_insert(ht, "Boris", "789-012")
|
||||||
ht = ht_insert(ht, "Anna", "345-678")
|
print("HashTable:", ht_list_all(ht))
|
||||||
ht = ht_insert(ht, "Ivan", "111-222")
|
|
||||||
print("HashTable entries:", ht_list_all(ht))
|
print("\n=== Тестирование BST ===")
|
||||||
print("Find Ivan:", ht_find(ht, "Ivan"))
|
bst_root = None
|
||||||
ht = ht_delete(ht, "Boris")
|
bst_root = bst_insert(bst_root, "Ivan", "123-456")
|
||||||
print("After delete Boris:", ht_list_all(ht))
|
bst_root = bst_insert(bst_root, "Boris", "789-012")
|
||||||
|
bst_root = bst_insert(bst_root, "Anna", "345-678")
|
||||||
|
bst_root = bst_insert(bst_root, "Ivan", "111-222")
|
||||||
|
print("BST entries:", bst_list_all(bst_root))
|
||||||
|
print("Find Ivan:", bst_find(bst_root, "Ivan"))
|
||||||
|
bst_root = bst_delete(bst_root, "Boris")
|
||||||
|
print("After delete Boris:", bst_list_all(bst_root))
|
||||||
Loading…
Reference in New Issue
Block a user