forked from UNN/2026-rff_mp
[14] adding time for BinarySearchTree()
This commit is contained in:
parent
bd4d6d48a0
commit
a784a4a037
|
|
@ -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 =====================')
|
|
||||||
|
|
||||||
Name.append('Masha')
|
|
||||||
for i in range(len(Name)):
|
|
||||||
name = Name[i]
|
|
||||||
print(name, ":", bst_find(root, name))
|
|
||||||
print("======== END TESTING =============\n\n")
|
|
||||||
|
|
||||||
|
|
||||||
print('===================== TESTING BST_LIST_ALL =============')
|
start_find = time.perf_counter()
|
||||||
name_list = bst_list_all(root)
|
for _ in range(100):
|
||||||
print(*name_list)
|
name = create_name_phone(rnd.randint(0, 999))[0]
|
||||||
print("======== END TESTING =============\n\n")
|
phone = bst_find(root, name)
|
||||||
|
#print(name, ":", phone)
|
||||||
|
end_find = time.perf_counter()
|
||||||
|
time_find = end_find - start_find
|
||||||
|
|
||||||
|
|
||||||
print('========== TESTING HT_DELETE ==========')
|
start_delete = time.perf_counter()
|
||||||
|
for i in range(110):
|
||||||
for _ in range(2):
|
if i <= 99: name = f"User_{rnd.randint(0,999):03d}"
|
||||||
name = Name[rnd.randint(0, len(Name) - 1)]
|
else: name = f"None_{i:03d}"
|
||||||
root = bst_delete(root, name)
|
root = bst_delete(root, name)
|
||||||
|
end_delete = time.perf_counter()
|
||||||
|
time_delete = end_delete - start_delete
|
||||||
|
|
||||||
print(root)
|
|
||||||
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]})
|
||||||
|
|
@ -366,6 +363,8 @@ def main():
|
||||||
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()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user