[14] adding time for BinarySearchTree()

This commit is contained in:
lukovnikovde 2026-05-06 09:42:03 +00:00
parent bd4d6d48a0
commit a784a4a037

View File

@ -274,48 +274,42 @@ def bst_delete(root, name):
################################################################################################# #################################################################################################
def BinarySearchTree(root): def BinarySearchTree(root, phone_book):
print('=========== TESTING BST_INSERT =============') start_insert = time.perf_counter()
Name = ['Dima', 'Alex', 'Ivan', 'Maxim', 'Olga', 'Lena'] for i in range(len(phone_book)):
for _ in range(10): root = bst_insert(root, phone_book[i][0], phone_book[i][1])
name = Name[rnd.randint(0, len(Name) - 1)] #print(buckest)
phone = str(rnd.randint(0,9)) + str(rnd.randint(0,9)) + str(rnd.randint(0,9)) + '-' + \ end_insert = time.perf_counter()
str(rnd.randint(0,9)) + str(rnd.randint(0,9)) + str(rnd.randint(0,9)) time_insert = end_insert - start_insert
print(name, phone)
root = bst_insert(root, name, phone)
print(root)
print('-----------------------------------------------------\n')
print('============= END TESTING =====================\n\n')
print('============== TESTING BST_FIND =====================') start_find = time.perf_counter()
for _ in range(100):
Name.append('Masha') name = create_name_phone(rnd.randint(0, 999))[0]
for i in range(len(Name)): phone = bst_find(root, name)
name = Name[i] #print(name, ":", phone)
print(name, ":", bst_find(root, name)) end_find = time.perf_counter()
print("======== END TESTING =============\n\n") time_find = end_find - start_find
print('===================== TESTING BST_LIST_ALL =============') start_delete = time.perf_counter()
name_list = bst_list_all(root) for i in range(110):
print(*name_list) if i <= 99: name = f"User_{rnd.randint(0,999):03d}"
print("======== END TESTING =============\n\n") else: name = f"None_{i:03d}"
print('========== TESTING HT_DELETE ==========')
for _ in range(2):
name = Name[rnd.randint(0, len(Name) - 1)]
root = bst_delete(root, name) root = bst_delete(root, name)
end_delete = time.perf_counter()
print(root) time_delete = end_delete - start_delete
print(name, ":", bst_find(root, name))
print("========= END TESTING ==============\n\n")
start_list = time.perf_counter()
name_list = sort_list(bst_list_all(root))
#print(*name_list)
end_list = time.perf_counter()
time_list = end_list - start_list
return (time_insert, time_find, time_delete, time_list)
################################################################################################ ################################################################################################
def main(): def main():
@ -340,6 +334,8 @@ def main():
Time_average_ll_not_sorted = {'insert': 0, 'find': 0, 'delete': 0, 'list': 0} Time_average_ll_not_sorted = {'insert': 0, 'find': 0, 'delete': 0, 'list': 0}
Time_average_ll_sorted = {'insert': 0, 'find': 0, 'delete': 0, 'list': 0} Time_average_ll_sorted = {'insert': 0, 'find': 0, 'delete': 0, 'list': 0}
print("============================================ TESTING LINKEDLIST =====================================\n")
print('Not sorted: ')
for _ in range(replay): for _ in range(replay):
time_ll_not_sorted = LinkedList(None, phone_book_not_sorted) time_ll_not_sorted = LinkedList(None, phone_book_not_sorted)
Time_ll_not_sorted.append({'insert': time_ll_not_sorted[0], 'find': time_ll_not_sorted[1], 'delete': time_ll_not_sorted[2], 'list': time_ll_not_sorted[3]}) Time_ll_not_sorted.append({'insert': time_ll_not_sorted[0], 'find': time_ll_not_sorted[1], 'delete': time_ll_not_sorted[2], 'list': time_ll_not_sorted[3]})
@ -349,6 +345,7 @@ def main():
print(Time_ll_not_sorted[i]) print(Time_ll_not_sorted[i])
print("Average:", Time_average_ll_not_sorted, "\n\n") print("Average:", Time_average_ll_not_sorted, "\n\n")
print('Sorted:')
for _ in range(replay): for _ in range(replay):
time_ll_sorted = LinkedList(None, phone_book_sorted) time_ll_sorted = LinkedList(None, phone_book_sorted)
Time_ll_sorted.append({'insert': time_ll_sorted[0], 'find': time_ll_sorted[1], 'delete': time_ll_sorted[2], 'list': time_ll_sorted[3]}) Time_ll_sorted.append({'insert': time_ll_sorted[0], 'find': time_ll_sorted[1], 'delete': time_ll_sorted[2], 'list': time_ll_sorted[3]})
@ -364,8 +361,10 @@ def main():
Time_ht_sorted = [] Time_ht_sorted = []
Time_average_ht_not_sorted = {'insert': 0, 'find': 0, 'delete': 0, 'list': 0} Time_average_ht_not_sorted = {'insert': 0, 'find': 0, 'delete': 0, 'list': 0}
Time_average_ht_sorted = {'insert': 0, 'find': 0, 'delete': 0, 'list': 0} Time_average_ht_sorted = {'insert': 0, 'find': 0, 'delete': 0, 'list': 0}
print("============================================ TESTING HASHTABLE =====================================\n")
print('Not sorted: ')
for _ in range(replay): for _ in range(replay):
time_ht_not_sorted = HashTable([[] for _ in range(100)], phone_book_not_sorted) time_ht_not_sorted = HashTable([[] for _ in range(100)], phone_book_not_sorted)
Time_ht_not_sorted.append({'insert': time_ht_not_sorted[0], 'find': time_ht_not_sorted[1], 'delete': time_ht_not_sorted[2], 'list': time_ht_not_sorted[3]}) Time_ht_not_sorted.append({'insert': time_ht_not_sorted[0], 'find': time_ht_not_sorted[1], 'delete': time_ht_not_sorted[2], 'list': time_ht_not_sorted[3]})
@ -375,6 +374,7 @@ def main():
print(Time_ht_not_sorted[i]) print(Time_ht_not_sorted[i])
print(f"Average: {Time_average_ht_not_sorted}\n\n") print(f"Average: {Time_average_ht_not_sorted}\n\n")
print('Sorted: ')
for _ in range(replay): for _ in range(replay):
time_ht_sorted = HashTable([[] for _ in range(100)], phone_book_sorted) time_ht_sorted = HashTable([[] for _ in range(100)], phone_book_sorted)
Time_ht_sorted.append({'insert': time_ht_sorted[0], 'find': time_ht_sorted[1], 'delete': time_ht_sorted[2], 'list': time_ht_sorted[3]}) Time_ht_sorted.append({'insert': time_ht_sorted[0], 'find': time_ht_sorted[1], 'delete': time_ht_sorted[2], 'list': time_ht_sorted[3]})
@ -384,7 +384,36 @@ def main():
print(Time_ht_sorted[i]) print(Time_ht_sorted[i])
print(f"Average: {Time_average_ht_sorted}\n\n") print(f"Average: {Time_average_ht_sorted}\n\n")
#BinarySearchTree(None)
Time_bst_not_sorted = []
Time_bst_sorted = []
Time_average_bst_not_sorted = {'insert': 0, 'find': 0, 'delete': 0, 'list': 0}
Time_average_bst_sorted = {'insert': 0, 'find': 0, 'delete': 0, 'list': 0}
print("============================================ TESTING BINARYSEARCHTREE =====================================\n")
print('Not sorted: ')
for _ in range(replay):
time_bst_not_sorted = BinarySearchTree(None, phone_book_not_sorted)
Time_bst_not_sorted.append({'insert': time_bst_not_sorted[0], 'find': time_bst_not_sorted[1], 'delete': time_bst_not_sorted[2], 'list': time_bst_not_sorted[3]})
for i, key in enumerate(Time_average_bst_not_sorted):
Time_average_bst_not_sorted[key] += time_bst_not_sorted[i]/replay
for i in range(replay):
print(Time_bst_not_sorted[i])
print(f"Average: {Time_average_bst_not_sorted}\n\n")
print('Sorted: ')
for _ in range(replay):
time_bst_sorted = BinarySearchTree(None, phone_book_sorted)
Time_bst_sorted.append({'insert': time_bst_sorted[0], 'find': time_bst_sorted[1], 'delete': time_bst_sorted[2], 'list': time_bst_sorted[3]})
for i, key in enumerate(Time_average_bst_sorted):
Time_average_bst_sorted[key] += time_bst_sorted[i]/replay
for i in range(replay):
print(Time_bst_sorted[i])
print(f"Average: {Time_average_bst_sorted}\n\n")
print("=============================================== END TESTING ================================================")
if __name__ == "__main__": if __name__ == "__main__":
main() main()