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

    new_node = {'name': name, 'phone': phone, 'next': None}
    if head is None:
        return new_node

    curr = head
    while curr['next'] is not None:
        curr = curr['next']
    curr['next'] = new_node
    return head


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


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


def linked_list_collect_all(head):
    records = []
    curr = head
    while curr is not None:
        records.append((curr['name'], curr['phone']))
        curr = curr['next']
    records.sort(key=lambda pair: pair[0])
    return records



#HASH
def _hash_bucket_index(key, table_size):
    return hash(key) % table_size


def hash_table_create(bucket_count=10):
    return [None] * bucket_count


def hash_table_put(table, name, phone):
    idx = _hash_bucket_index(name, len(table))
    table[idx] = linked_list_add(table[idx], name, phone)
    return table


def hash_table_get(table, name):
    idx = _hash_bucket_index(name, len(table))
    return linked_list_find(table[idx], name)


def hash_table_remove(table, name):
    idx = _hash_bucket_index(name, len(table))
    table[idx] = linked_list_remove(table[idx], name)
    return table


def hash_table_collect_all(table):
    all_records = []
    for head in table:
        curr = head
        while curr is not None:
            all_records.append((curr['name'], curr['phone']))
            curr = curr['next']
    all_records.sort(key=lambda pair: pair[0])
    return all_records


#BST
def _bst_new_node(name, phone):
    return {'name': name, 'phone': phone, 'left': None, 'right': None}


def bst_add(root, name, phone):
    """Insert or update. Returns (possibly new) root."""
    if root is None:
        return _bst_new_node(name, phone)

    if name == root['name']:
        root['phone'] = phone
    elif name < root['name']:
        root['left'] = bst_add(root['left'], name, phone)
    else:
        root['right'] = bst_add(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_minimum(node):
    while node['left'] is not None:
        node = node['left']
    return node


def bst_remove(root, name):
    if root is None:
        return None

    if name < root['name']:
        root['left'] = bst_remove(root['left'], name)
    elif name > root['name']:
        root['right'] = bst_remove(root['right'], name)
    else:
        if root['left'] is None:
            return root['right']
        if root['right'] is None:
            return root['left']

        successor = _bst_find_minimum(root['right'])
        root['name'] = successor['name']
        root['phone'] = successor['phone']
        root['right'] = bst_remove(root['right'], successor['name'])
    return root


def bst_collect_inorder(root):
    result = []
    def inorder(node):
        if node is None:
            return
        inorder(node['left'])
        result.append((node['name'], node['phone']))
        inorder(node['right'])
    inorder(root)
    return result




#Benchmarking
import random
import time
import csv
import os
import sys

sys.setrecursionlimit(20000)

def generate_test_data(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_ordered_and_shuffled(records):
    shuffled = records.copy()
    random.shuffle(shuffled)
    sorted_records = sorted(records, key=lambda x: x[0])
    return shuffled, sorted_records

def measure_operations(struct_ops, records, mode_name, repeats=5):
    results = []
    for rep in range(repeats):
        ds = struct_ops['create']()

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

        existing_names = [name for name, _ in records]
        sample_existing = random.sample(existing_names, 100)
        nonexistent = [f"Missing_{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_ops['find'](ds, name)
        find_time = time.perf_counter() - start

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

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

def run_full_benchmark():
    N = 10000
    base_records = generate_test_data(N)
    shuffled, sorted_records = prepare_ordered_and_shuffled(base_records)

    structures = {
        'LinkedList': {
            'name': 'LinkedList',
            'create': lambda: None,
            'insert': linked_list_add,
            'find': linked_list_find,
            'delete': linked_list_remove,
        },
        'HashTable': {
            'name': 'HashTable',
            'create': lambda: hash_table_create(100),
            'insert': hash_table_put,
            'find': hash_table_get,
            'delete': hash_table_remove,
        },
        'BST': {
            'name': 'BST',
            'create': lambda: None,
            'insert': bst_add,
            'find': bst_find,
            'delete': bst_remove,
        }
    }

    all_results = []
    for name, ops in structures.items():
        print(f"Benchmarking {name} on random order...")
        all_results.extend(measure_operations(ops, shuffled, 'random', repeats=5))
        print(f"Benchmarking {name} on sorted order...")
        all_results.extend(measure_operations(ops, sorted_records, 'sorted', repeats=5))

    os.makedirs('docs/data', exist_ok=True)
    csv_path = 'docs/data/experiment_results.csv'
    with open(csv_path, '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(f"Experiment finished. Results saved to {csv_path}")

if __name__ == '__main__':
    run_full_benchmark()