import csv
import matplotlib.pyplot as plt
from collections import defaultdict


data = defaultdict(list)

with open("results.csv", "r") as f:
    reader = csv.DictReader(f)

    for row in reader:
        key = (row["Structure"], row["Order"], row["Operation"])
        data[key].append(float(row["Time"]))


def avg(key):
    return sum(data[key]) / len(data[key])


structures = ["LinkedList", "HashTable", "BST"]
orders = ["random", "sorted"]
operations = ["insert", "find", "delete"]


for op in operations:
    plt.figure()

    labels = []
    values = []

    for struct in structures:
        for order in orders:
            key = (struct, order, op)
            labels.append(f"{struct}\n{order}")
            values.append(avg(key))

    plt.bar(labels, values)

    plt.title(f"{op} (Histogram)")
    plt.ylabel("Time (sec)")
    plt.xticks(rotation=30)
    plt.grid(axis="y")

    plt.tight_layout()
    plt.show()