исправлено двоичное дерево и исправлены опечатки

This commit is contained in:
konnovaea 2026-04-26 20:41:45 +03:00
parent be927fd028
commit 0046bde759
2 changed files with 53 additions and 11 deletions

View File

@ -51,7 +51,7 @@ def run_linked_experiments(records, mode_name):
for name in non_exist_names: for name in non_exist_names:
ll_find(head, name) ll_find(head, name)
end = time.pref_cointer() end = time.perf_counter()
find_times.append(end - start) find_times.append(end - start)
print(f"поиск {run+1}/5: {find_times[-1]:.6f} сек") print(f"поиск {run+1}/5: {find_times[-1]:.6f} сек")
@ -121,7 +121,7 @@ def run_hash_experiments(records, mode_name):
for name in non_exist_names: for name in non_exist_names:
ht_find(buckets, name) ht_find(buckets, name)
end = time.pref_cointer() end = time.perf_counter()
find_times.append(end - start) find_times.append(end - start)
print(f"поиск {run+1}/5: {find_times[-1]:.6f} сек") print(f"поиск {run+1}/5: {find_times[-1]:.6f} сек")
@ -191,7 +191,7 @@ def run_bst_experiments(records, mode_name):
for name in non_exist_names: for name in non_exist_names:
bst_find(root, name) bst_find(root, name)
end = time.pref_cointer() end = time.perf_counter()
find_times.append(end - start) find_times.append(end - start)
print(f"поиск {run+1}/5: {find_times[-1]:.6f} сек") print(f"поиск {run+1}/5: {find_times[-1]:.6f} сек")
@ -256,3 +256,30 @@ def save_result_to_csv(all_results):
writer = csv.writer(f) writer = csv.writer(f)
writer.writerow(csv_rows) writer.writerow(csv_rows)
print(f"\nрезультаты сохранены") print(f"\nрезультаты сохранены")
def main():
print("эксперименты по замеру производительности")
records_shuffled, records_sorted = generate_test_data(10000)
all_results = []
print("режим: случайный порядок")
all_results.append(run_linked_experiments(records_shuffled, "случайный"))
all_results.append(run_hash_experiments(records_shuffled, "случайный"))
all_results.append(run_bst_experiments(records_shuffled, "случайный"))
print("режим: отсортированный порядок")
all_results.append(run_linked_experiments(records_sorted, "отсортированный"))
all_results.append(run_hash_experiments(records_sorted, "отсортированный"))
all_results.append(run_bst_experiments(records_sorted, "отсортированный"))
save_result_to_csv(all_results)
if __name__== "__main__":
main()

View File

@ -77,16 +77,31 @@ def ht_list_all(buckets):
return records return records
def bst_insert(root, name, phone): def bst_insert(root, name, phone):
new_node = {'name': name, 'phone': phone, 'left': None, 'right': None}
if root is None: if root is None:
return {'name': name, 'phone': phone, 'left': None, 'right': None} return new_node
if name < root['name']:
root['left'] = bst_insert(root['left'], name, phone) current = root
elif name > root['name']: while True:
root['right'] = bst_insert(root['right'], name, phone) if name < current['name']:
else: if current['left'] is None:
root['phone'] = phone current['left'] = new_node
break
current = current['left']
elif name > current['name']:
if current['right'] is None:
current['right'] = new_node
break
current = current['right']
else:
current['phone'] = phone
break
return root return root
def bst_find(root, name): def bst_find(root, name):
current = root current = root
while current is not None: while current is not None: