import random
import time
import csv
import sys

sys.setrecursionlimit(20000)


def llist_insert(head, name, phone):
    current = head
    while current is not None:
        if current['name'] == name:
            current['phone'] = phone
            return head
        current = current['next']
    new_node = {'name': name, 'phone': phone, 'next': None}
    if head is None:
        return new_node
    current = head
    while current['next'] is not None:
        current = current['next']
    current['next'] = new_node
    return head


def llist_find(head, name):
    current = head
    while current is not None:
        if current['name'] == name:
            return current['phone']
        current = current['next']
    return None


def llist_delete(head, name):
    if head is None:
        return None
    if head['name'] == name:
        return head['next']
    prev = head
    current = head['next']
    while current is not None:
        if current['name'] == name:
            prev['next'] = current['next']
            return head
        prev = current
        current = current['next']
    return head


def llist_get_all(head):
    entries = []
    current = head
    while current is not None:
        entries.append((current['name'], current['phone']))
        current = current['next']
    entries.sort(key=lambda x: x[0])
    return entries


BUCKET_SIZE = 1000

def ht_create():
    return [None] * BUCKET_SIZE


def ht_insert(table, name, phone):
    idx = hash(name) % len(table)
    table[idx] = llist_insert(table[idx], name, phone)
    return table


def ht_find(table, name):
    idx = hash(name) % len(table)
    return llist_find(table[idx], name)


def ht_delete(table, name):
    idx = hash(name) % len(table)
    table[idx] = llist_delete(table[idx], name)
    return table


def ht_get_all(table):
    all_entries = []
    for head in table:
        current = head
        while current is not None:
            all_entries.append((current['name'], current['phone']))
            current = current['next']
    all_entries.sort(key=lambda x: x[0])
    return all_entries


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_inorder_collect(root, out_list):
    if root is not None:
        bst_inorder_collect(root['left'], out_list)
        out_list.append((root['name'], root['phone']))
        bst_inorder_collect(root['right'], out_list)


def bst_get_all(root):
    result = []
    bst_inorder_collect(root, result)
    return result


def generate_phonebook_entries(n, seed=42):
    random.seed(seed)
    records = []
    for i in range(1, n + 1):
        name = f"User_{i:05d}"
        phone = f"{random.randint(100,999)}-{random.randint(1000,9999)}"
        records.append((name, phone))
    return records


def prepare_datasets(base_records):
    shuffled = base_records.copy()
    random.shuffle(shuffled)
    sorted_records = sorted(base_records, key=lambda x: x[0])
    return shuffled, sorted_records


def run_experiment(struct_funcs, records, mode_name, repeats=5):
    all_results = []
    for rep in range(repeats):
        struct = struct_funcs['create']()

        start = time.perf_counter()
        for name, phone in records:
            struct = struct_funcs['insert'](struct, name, phone)
        insert_time = time.perf_counter() - start

        existing_names = [name for name, _ in records]
        sample_existing = random.sample(existing_names, 100)
        nonexistent = [f"None_{i}" for i in range(10)]
        search_names = sample_existing + nonexistent
        random.shuffle(search_names)

        start = time.perf_counter()
        for name in search_names:
            _ = struct_funcs['find'](struct, name)
        find_time = time.perf_counter() - start

        to_delete = random.sample(existing_names, 50)
        start = time.perf_counter()
        for name in to_delete:
            struct = struct_funcs['delete'](struct, name)
        delete_time = time.perf_counter() - start

        all_results.append({
            'structure': struct_funcs['name'],
            'mode': mode_name,
            'repetition': rep + 1,
            'insert_time': insert_time,
            'find_time': find_time,
            'delete_time': delete_time
        })
    return all_results


def run_benchmark():
    N = 10000
    REPEATS = 5

    base_records = generate_phonebook_entries(N)
    shuffled_records, sorted_records = prepare_datasets(base_records)

    structures = {
        'LinkedList': {
            'name': 'LinkedList',
            'create': lambda: None,
            'insert': llist_insert,
            'find': llist_find,
            'delete': llist_delete,
            'get_all': llist_get_all
        },
        'HashTable': {
            'name': 'HashTable',
            'create': ht_create,
            'insert': ht_insert,
            'find': ht_find,
            'delete': ht_delete,
            'get_all': ht_get_all
        },
        'BST': {
            'name': 'BST',
            'create': lambda: None,
            'insert': bst_insert,
            'find': bst_find,
            'delete': bst_delete,
            'get_all': bst_get_all
        }
    }

    all_results = []

    for struct_name, funcs in structures.items():
        results_random = run_experiment(funcs, shuffled_records, 'random', REPEATS)
        all_results.extend(results_random)
        results_sorted = run_experiment(funcs, sorted_records, 'sorted', REPEATS)
        all_results.extend(results_sorted)

    with open('experiment_results.csv', 'w', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow(['Structure', 'Mode', 'Repeat', 'Insert (sec)', 'Search (sec)', 'Delete (sec)'])
        for r in all_results:
            writer.writerow([
                r['structure'],
                r['mode'],
                r['repetition'],
                f"{r['insert_time']:.6f}",
                f"{r['find_time']:.6f}",
                f"{r['delete_time']:.6f}"
            ])

    print("Experiment finished. Results saved to 'experiment_results.csv'.")


if __name__ == '__main__':
    run_benchmark()