
import matplotlib.pyplot as plt
import numpy as np
import os


os.makedirs('docs/data', exist_ok=True)


structures = ['LinkedList', 'HashTable', 'BST']

random_insert = [0.0037545, 0.015088, 0.026280]
sorted_insert = [0.0017544, 0.011369, 4.930788]

random_search = [0.00000962, 0.0001646, 0.0002592]
sorted_search = [0.00000858, 0.00014016, 0.047126]

random_delete = [0.0000079, 0.00009824, 0.00016984]
sorted_delete = [0.00000294, 0.00005878, 0.023013]

x = np.arange(len(structures))
width = 0.35

#график вставка
fig, ax = plt.subplots(figsize=(12, 7))

bars1 = ax.bar(x - width/2, random_insert, width, label='Случайный порядок', color='#3498db')
bars2 = ax.bar(x + width/2, sorted_insert, width, label='Отсортированный порядок', color='#e74c3c')


for bar in bars1:
    height = bar.get_height()
    ax.annotate(f'{height:.4f}', xy=(bar.get_x() + bar.get_width()/2, height),
                xytext=(0, 3), textcoords="offset points", ha='center', va='bottom', fontsize=9)

for bar in bars2:
    height = bar.get_height()
    if height < 1:
        ax.annotate(f'{height:.4f}', xy=(bar.get_x() + bar.get_width()/2, height),
                    xytext=(0, 3), textcoords="offset points", ha='center', va='bottom', fontsize=9)
    else:
        ax.annotate(f'{height:.1f} сек', xy=(bar.get_x() + bar.get_width()/2, height),
                    xytext=(0, 5), textcoords="offset points", ha='center', va='bottom', fontsize=10, fontweight='bold')

ax.set_ylabel('Время (сек)', fontsize=12)
ax.set_title('Время вставки 10000 записей', fontsize=14, fontweight='bold')
ax.set_xticks(x)
ax.set_xticklabels(structures, fontsize=11)
ax.legend(fontsize=11)
ax.set_yscale('log')
ax.grid(True, alpha=0.3, axis='y')

plt.tight_layout()
plt.savefig('docs/data/graph_insert.png', dpi=150, bbox_inches='tight')
plt.close()


# график поиск
fig, ax = plt.subplots(figsize=(12, 7))

bars1 = ax.bar(x - width/2, random_search, width, label='Случайный порядок', color='#3498db')
bars2 = ax.bar(x + width/2, sorted_search, width, label='Отсортированный порядок', color='#e74c3c')

for bar in bars1:
    height = bar.get_height()
    ax.annotate(f'{height:.6f}', xy=(bar.get_x() + bar.get_width()/2, height),
                xytext=(0, 3), textcoords="offset points", ha='center', va='bottom', fontsize=9)

for bar in bars2:
    height = bar.get_height()
    if height < 0.01:
        ax.annotate(f'{height:.6f}', xy=(bar.get_x() + bar.get_width()/2, height),
                    xytext=(0, 3), textcoords="offset points", ha='center', va='bottom', fontsize=9)
    else:
        ax.annotate(f'{height:.4f}', xy=(bar.get_x() + bar.get_width()/2, height),
                    xytext=(0, 3), textcoords="offset points", ha='center', va='bottom', fontsize=9)

ax.set_ylabel('Время (сек)', fontsize=12)
ax.set_title('Время поиска 110 записей', fontsize=14, fontweight='bold')
ax.set_xticks(x)
ax.set_xticklabels(structures, fontsize=11)
ax.legend(fontsize=11)
ax.set_yscale('log')
ax.grid(True, alpha=0.3, axis='y')

plt.tight_layout()
plt.savefig('docs/data/graph_search.png', dpi=150, bbox_inches='tight')
plt.close()


# график удаление
fig, ax = plt.subplots(figsize=(12, 7))

bars1 = ax.bar(x - width/2, random_delete, width, label='Случайный порядок', color='#3498db')
bars2 = ax.bar(x + width/2, sorted_delete, width, label='Отсортированный порядок', color='#e74c3c')

for bar in bars1:
    height = bar.get_height()
    ax.annotate(f'{height:.6f}', xy=(bar.get_x() + bar.get_width()/2, height),
                xytext=(0, 3), textcoords="offset points", ha='center', va='bottom', fontsize=9)

for bar in bars2:
    height = bar.get_height()
    if height < 0.01:
        ax.annotate(f'{height:.6f}', xy=(bar.get_x() + bar.get_width()/2, height),
                    xytext=(0, 3), textcoords="offset points", ha='center', va='bottom', fontsize=9)
    else:
        ax.annotate(f'{height:.4f}', xy=(bar.get_x() + bar.get_width()/2, height),
                    xytext=(0, 3), textcoords="offset points", ha='center', va='bottom', fontsize=9)

ax.set_ylabel('Время (сек)', fontsize=12)
ax.set_title('Время удаления 50 записей', fontsize=14, fontweight='bold')
ax.set_xticks(x)
ax.set_xticklabels(structures, fontsize=11)
ax.legend(fontsize=11)
ax.set_yscale('log')
ax.grid(True, alpha=0.3, axis='y')

plt.tight_layout()
plt.savefig('docs/data/graph_delete.png', dpi=150, bbox_inches='tight')
plt.close()


