import csv

from structures.LinkedList import ll_insert, ll_find, ll_delete
from structures.HashTable import ht_insert, ht_find, ht_delete
from structures.BinaryTree import bst_insert, bst_find, bst_delete

from util.randomNames import generate_test_data
from util.timeTester import run_test


def main():
    structures = [
        ("LinkedList", ll_insert, ll_find, ll_delete),
        ("HashTable", ht_insert, ht_find, ht_delete),
        ("BST", bst_insert, bst_find, bst_delete)
    ]

    modes = [
        ("random", False),
        ("sorted", True)
    ]

    results = []

    N = 10000
    REPEATS = 5

    for mode_name, is_sorted in modes:
        base_records = generate_test_data(N, is_sorted)

        for struct_name, ins, fnd, dele in structures:
            for i in range(REPEATS):

                stats = run_test(base_records, ins, fnd, dele)

                results.append([
                    struct_name,
                    mode_name,
                    "insert",
                    stats["insert"]
                ])

                results.append([
                    struct_name,
                    mode_name,
                    "find",
                    stats["find"]
                ])

                results.append([
                    struct_name,
                    mode_name,
                    "delete",
                    stats["delete"]
                ])

    with open("results.csv", "w", newline="") as f:
        writer = csv.writer(f)
        writer.writerow(["Structure", "Order", "Operation", "Time"])
        writer.writerows(results)

    print("Готово: записано", len(results), "строк")


if __name__ == "__main__":
    main()