{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "c533959c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import LinkedList as ll\n",
    "import HashTable as ht\n",
    "import BinaryTree as bt\n",
    "import time \n",
    "import random as rand\n",
    "import numpy as np \n",
    "import csv\n",
    "import sys\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15cd6183",
   "metadata": {},
   "source": [
    "## Данные для обработки"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "88611f78",
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 10000\n",
    "sys.setrecursionlimit(10000) \n",
    "records_sorted = [(f\"User_{i:05d}\", f\"+7999{i:07d}\") for i in range(N)] \n",
    "records_shuffled = records_sorted.copy()\n",
    "rand.shuffle(records_shuffled)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fd1b8cd",
   "metadata": {},
   "source": [
    "## Исследование для LinkedList"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "083d49d0",
   "metadata": {},
   "source": [
    "### Добавление всех элементов произвольного кортежа\n",
    "- **data_ll_sh** -  Структура произвольных данных (только последний замер)\n",
    "- **time_ll_insert_sh** - Замер времени работы 10000 элементов (5 замеров) \n",
    "- **heads_ll_sh** - Массив голов для массив для произвольного массива"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "11634fa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_ll_insert_sh = [] \n",
    "heads_ll_sh = []\n",
    "for n in range(5):\n",
    "    head = None\n",
    "    data_ll_sh = []\n",
    "    start = time.perf_counter()\n",
    "    for i in range(N):\n",
    "        head = ll.ll_insert(head, records_shuffled[i][0], records_shuffled[i][1])\n",
    "        data_ll_sh.append(head)\n",
    "    end = time.perf_counter()\n",
    "    heads_ll_sh.append(head)\n",
    "    time_ll_insert_sh.append(end - start)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a5f161e",
   "metadata": {},
   "source": [
    "### Добавление всех элементов сортированного кортежа\n",
    "- **data_ll_so** - Структура отсортированных данных (только последний замер)\n",
    "- **time_ll_insert_so** - Замер времени работы 10000 элементов (5 замеров) \n",
    "- **heads_ll_so** - Массив голов для массив для сортированного массива"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "9eab4641",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_ll_insert_so = [] \n",
    "heads_ll_so = []\n",
    "for n in range(5):\n",
    "    head = None\n",
    "    data_ll_so = []\n",
    "    start = time.perf_counter()\n",
    "    for i in range(N):\n",
    "        head = ll.ll_insert(head, records_sorted[i][0], records_sorted[i][1])\n",
    "        data_ll_so.append(head)\n",
    "    end = time.perf_counter()\n",
    "    heads_ll_so.append(head)\n",
    "    time_ll_insert_so.append(end - start)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5862d31b",
   "metadata": {},
   "source": [
    "### Поиск элементов в произвольном массиве\n",
    "- **time_ll_find_sh** - Време поиска в произвольном массиве (для 5 замеров)\n",
    "- **find_ll_sh** - массив найденных данных в произвольном массиве (только последний замер)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "aac6cd23",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_ll_find_sh = []\n",
    "for n in range(5):\n",
    "    find_ll_sh = []\n",
    "    start = time.perf_counter()\n",
    "    for m in range(100): # замер для 100 случайных узлов \n",
    "        i = rand.randint(0, N-1)\n",
    "        str_find = records_shuffled[i][0]\n",
    "        find_ll_sh.append(ll.ll_find(data_ll_sh[0], str_find))\n",
    "    for m in range(10): # недоступные данные\n",
    "        str_find = f\"Node_{m}\"\n",
    "        find_ll_sh.append(ll.ll_find(data_ll_sh[0], str_find))\n",
    "    end = time.perf_counter()\n",
    "    time_ll_find_sh.append(end - start)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "651aac23",
   "metadata": {},
   "source": [
    "### Поиск элементов в отсортированном массиве\n",
    "- **time_ll_find_so** - Време поиска в отсортированном массиве (для 5 замеров)\n",
    "- **find_ll_so** - Массив найденных данных в отсортированном массиве (только последний замер)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "5e5ae537",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_ll_find_so = []\n",
    "for n in range(5):\n",
    "    find_ll_so = []\n",
    "    start = time.perf_counter()\n",
    "    for m in range(100): # замер для 100 случайных узлов \n",
    "        i = rand.randint(0, N-1)\n",
    "        str_find = records_sorted[i][0]\n",
    "        find_ll_so.append(ll.ll_find(data_ll_sh[0], str_find))\n",
    "    for m in range(10): # недоступные данные \n",
    "        str_find = f\"Node_{m}\"\n",
    "        find_ll_so.append(ll.ll_find(data_ll_sh[0], str_find))\n",
    "    end = time.perf_counter()\n",
    "    time_ll_find_so.append(end - start)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1f70be9",
   "metadata": {},
   "source": [
    "### Удаление элементов в произвольном массиве\n",
    "- **time_ll_delete_sh** - Време поиска в произвольном массиве (для 5 замеров)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cdf8a70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.05640505500014115,\n",
       " 0.038319764000334544,\n",
       " 0.02700047700000141,\n",
       " 0.03155651599990961,\n",
       " 0.03664214699983859]"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time_ll_delete_sh = []\n",
    "for n in range(5):\n",
    "    current_head = heads_ll_sh[n]\n",
    "\n",
    "    start = time.perf_counter()\n",
    "    for m in range(50):  \n",
    "        i = rand.randint(0, N-1)\n",
    "        str_delete = records_shuffled[i][0]\n",
    "        current_head = ll.ll_delete(current_head, str_delete)\n",
    "    end = time.perf_counter()\n",
    "\n",
    "    time_ll_delete_sh.append(end - start)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d6156e9",
   "metadata": {},
   "source": [
    "### Удаление элементов в сортированном массиве\n",
    "- **time_ll_delete_so** - Време поиска в произвольном массиве (для 5 замеров)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "575e375c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.029801202000271587,\n",
       " 0.018522201000450877,\n",
       " 0.01715149899973767,\n",
       " 0.018071766000502976,\n",
       " 0.0195203829998718]"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time_ll_delete_so = []\n",
    "for n in range(5):\n",
    "    current_head = heads_ll_so[n]\n",
    "\n",
    "    start = time.perf_counter()\n",
    "    for m in range(50):  \n",
    "        i = rand.randint(0, N-1)\n",
    "        str_delete = records_sorted[i][0]\n",
    "        current_head = ll.ll_delete(current_head, str_delete)\n",
    "    end = time.perf_counter()\n",
    "\n",
    "    time_ll_delete_so.append(end - start)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a95a40b",
   "metadata": {},
   "source": [
    "## Исследование BinaryTree"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54c92d21",
   "metadata": {},
   "source": [
    "### Добавление всех элементов произвольного кортежа\n",
    "- **data_bt_sh** -  Структура произвольных данных (только последний замер)\n",
    "- **time_bt_insert_sh** - Замер времени работы 10000 элементов (5 замеров) \n",
    "- **heads_bt_sh** - Массив голов для массив для произвольного массива"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "939aa900",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_bt_insert_sh = [] \n",
    "heads_bt_sh = []\n",
    "for n in range(5):\n",
    "    head = None\n",
    "    data_bt_sh = []\n",
    "    start = time.perf_counter()\n",
    "    for i in range(N):\n",
    "        head = bt.bst_insert(head, records_shuffled[i][0], records_shuffled[i][1])\n",
    "        data_bt_sh.append(head)\n",
    "    end = time.perf_counter()\n",
    "    heads_bt_sh.append(head)\n",
    "    time_bt_insert_sh.append(end - start)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e91b5893",
   "metadata": {},
   "source": [
    "### Добавление всех элементов сортированного кортежа\n",
    "- **data_bt_so** -  Структура сортированных данных (только последний замер)\n",
    "- **time_bt_insert_so** - Замер времени работы 10000 элементов (5 замеров) \n",
    "- **heads_bt_so** - Массив голов для массив для сортированного массива"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "d17b8108",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_bt_insert_so = [] \n",
    "heads_bt_so = []\n",
    "for n in range(5):\n",
    "    head = None\n",
    "    data_bt_so = []\n",
    "    start = time.perf_counter()\n",
    "    #head = bt.bst_insert(head, records_sorted[i][0],records_sorted[i][1])\n",
    "    head = bt.bst_insert_sort(records_sorted, 0, len(records_sorted) - 1)\n",
    "    data_bt_so.append(head)\n",
    "    end = time.perf_counter()\n",
    "    heads_bt_so.append(head)\n",
    "    time_bt_insert_so.append(end - start)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e8a3f9e",
   "metadata": {},
   "source": [
    "### Поиск элементов в произвольном массиве\n",
    "- **time_bt_find_sh** - Време поиска в произвольном массиве (для 5 замеров)\n",
    "- **find_bt_sh** - массив найденных данных в произвольном массиве (только последний замер)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "4352b11d",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_bt_find_sh = []\n",
    "for n in range(5):\n",
    "    find_bt_sh = []\n",
    "    start = time.perf_counter()\n",
    "    for m in range(100): # замер для 100 случайных узлов \n",
    "        i = rand.randint(0, N-1)\n",
    "        str_find = records_shuffled[i][0]\n",
    "        find_bt_sh.append(bt.bst_find(data_bt_sh[0], str_find))\n",
    "    for m in range(10): # недоступные данные\n",
    "        str_find = f\"Node_{m}\"\n",
    "        find_bt_sh.append(bt.bst_find(data_bt_sh[0], str_find))\n",
    "    end = time.perf_counter()\n",
    "    time_bt_find_sh.append(end - start)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8db5208b",
   "metadata": {},
   "source": [
    "### Поиск элементов в отсортированном массиве\n",
    "- **time_bt_find_so** - Време поиска в сортированном массиве (для 5 замеров)\n",
    "- **find_bt_so** - массив найденных данных в сортированном массиве (только последний замер)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "7941e689",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_bt_find_so = []\n",
    "for n in range(5):\n",
    "    find_bt_so = []\n",
    "    start = time.perf_counter()\n",
    "    for m in range(100): # замер для 100 случайных узлов \n",
    "        i = rand.randint(0, N-1)\n",
    "        str_find = records_sorted[i][0]\n",
    "        find_bt_so.append(bt.bst_find(data_bt_so[0], str_find))\n",
    "    for m in range(10): # недоступные данные\n",
    "        str_find = f\"Node_{m}\"\n",
    "        find_bt_so.append(bt.bst_find(data_bt_so[0], str_find))\n",
    "    end = time.perf_counter()\n",
    "    time_bt_find_so.append(end - start)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffbe3dfe",
   "metadata": {},
   "source": [
    "### Удаление элементов в произвольном массиве\n",
    "- **time_bt_delete_sh** - Време поиска в произвольном массиве (для 5 замеров)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "4043a9dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_bt_delete_sh = []\n",
    "for n in range(5):\n",
    "    current_head = heads_bt_sh[n]\n",
    "\n",
    "    start = time.perf_counter()\n",
    "    for m in range(50):  \n",
    "        i = rand.randint(0, N-1)\n",
    "        str_delete = records_shuffled[i][0]\n",
    "        current_head = bt.bst_delete(current_head, str_delete)\n",
    "    end = time.perf_counter()\n",
    "\n",
    "    time_bt_delete_sh.append(end - start)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7db94391",
   "metadata": {},
   "source": [
    "### Удаление элементов в сортированном массиве\n",
    "- **time_bt_delete_so** - Време поиска в произвольном массиве (для 5 замеров)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "7ab6136c",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_bt_delete_so = []\n",
    "for n in range(5):\n",
    "    current_head = heads_bt_so[n]\n",
    "\n",
    "    start = time.perf_counter()\n",
    "    for m in range(50):  \n",
    "        i = rand.randint(0, N-1)\n",
    "        str_delete = records_sorted[i][0]\n",
    "        current_head = bt.bst_delete(current_head, str_delete)\n",
    "    end = time.perf_counter()\n",
    "\n",
    "    time_bt_delete_so.append(end - start)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bf5b406",
   "metadata": {},
   "source": [
    "## Исследование HashTable"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75586bbc",
   "metadata": {},
   "source": [
    "### Добавление всех элементов произвольного кортежа\n",
    "- **data_ht_sh** -  Структура произвольных данных (только последний замер)\n",
    "- **time_ht_insert_sh** - Замер времени работы 10000 элементов (5 замеров) \n",
    "- **heads_ht_sh** - Массив голов для массив для произвольного массива"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "cb1788d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_ht_insert_sh = [] \n",
    "for n in range(5):\n",
    "    buckets = ht.create_ht(size = N * 4)\n",
    "    data_ht_sh = []\n",
    "    start = time.perf_counter()\n",
    "    for i in range(N):\n",
    "        buckets = ht.ht_insert(buckets, records_shuffled[i][0], records_shuffled[i][1])\n",
    "        data_ht_sh.append(buckets)\n",
    "    end = time.perf_counter()\n",
    "    time_ht_insert_sh.append(end - start)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bb2aa16",
   "metadata": {},
   "source": [
    "### Добавление всех элементов сортированного кортежа\n",
    "- **data_ht_so** -  Структура сортированных данных (только последний замер)\n",
    "- **time_ht_insert_so** - Замер времени работы 10000 элементов (5 замеров) \n",
    "- **heads_ht_so** - Массив голов для массив для сортированного массива"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "4cb524e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_ht_insert_so = [] \n",
    "for n in range(5):\n",
    "    buckets = ht.create_ht(size = N * 4)\n",
    "    data_ht_so = []\n",
    "    start = time.perf_counter()\n",
    "    for i in range(N):\n",
    "        buckets = ht.ht_insert(buckets, records_sorted[i][0], records_sorted[i][1])\n",
    "        data_ht_so.append(buckets)\n",
    "    end = time.perf_counter()\n",
    "    time_ht_insert_so.append(end - start)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d79016f",
   "metadata": {},
   "source": [
    "### Поиск элементов в произвольном массиве\n",
    "- **time_ht_find_sh** - Време поиска в произвольном массиве (для 5 замеров)\n",
    "- **find_ht_sh** - массив найденных данных в произвольном массиве (только последний замер)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "45cec102",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_ht_find_sh = []\n",
    "for n in range(5):\n",
    "    find_ht_sh = []\n",
    "    start = time.perf_counter()\n",
    "    for m in range(100): # замер для 100 случайных узлов \n",
    "        i = rand.randint(0, N-1)\n",
    "        str_find = records_shuffled[i][0]\n",
    "        find_ht_sh.append(ht.ht_find(data_ht_sh[0], str_find))\n",
    "    for m in range(10): # недоступные данные\n",
    "        str_find = f\"Node_{m}\"\n",
    "        find_ht_sh.append(ht.ht_find(data_ht_sh[0], str_find))\n",
    "    end = time.perf_counter()\n",
    "    time_ht_find_sh.append(end - start)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f11dbad",
   "metadata": {},
   "source": [
    "### Поиск элементов в отсортированном массиве\n",
    "- **time_ht_find_so** - Време поиска в сортированном массиве (для 5 замеров)\n",
    "- **find_ht_so** - массив найденных данных в сортированном массиве (только последний замер)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "19e7a19a",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_ht_find_so = []\n",
    "for n in range(5):\n",
    "    find_ht_so = []\n",
    "    start = time.perf_counter()\n",
    "    for m in range(100): # замер для 100 случайных узлов \n",
    "        i = rand.randint(0, N-1)\n",
    "        str_find = records_shuffled[i][0]\n",
    "        find_ht_so.append(ht.ht_find(data_ht_so[0], str_find))\n",
    "    for m in range(10): # недоступные данные\n",
    "        str_find = f\"Node_{m}\"\n",
    "        find_ht_so.append(ht.ht_find(data_ht_so[0], str_find))\n",
    "    end = time.perf_counter()\n",
    "    time_ht_find_so.append(end - start)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e39fa07c",
   "metadata": {},
   "source": [
    "### Удаление элементов в произвольном массиве\n",
    "- **time_ht_delete_sh** - Време поиска в произвольном массиве (для 5 замеров)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "90a9613d",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_ht_delete_sh = []\n",
    "for n in range(5):\n",
    "    current_head = data_ht_sh[0]\n",
    "\n",
    "    start = time.perf_counter()\n",
    "    for m in range(50):  \n",
    "        i = rand.randint(0, N-1)\n",
    "        str_delete = records_shuffled[i][0]\n",
    "        current_head = ht.ht_delete(current_head, str_delete)\n",
    "    end = time.perf_counter()\n",
    "    time_ht_delete_sh.append(end - start)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb9fcf01",
   "metadata": {},
   "source": [
    "### Удаление элементов в сортированном массиве\n",
    "- **time_ht_delete_so** - Време поиска в произвольном массиве (для 5 замеров)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "dfe01fe5",
   "metadata": {},
   "outputs": [],
   "source": [
    "time_ht_delete_so = []\n",
    "for n in range(5):\n",
    "    current_head = data_ht_so[0]\n",
    "\n",
    "    start = time.perf_counter()\n",
    "    for m in range(50):  \n",
    "        i = rand.randint(0, N-1)\n",
    "        str_delete = records_shuffled[i][0]\n",
    "        current_head = ht.ht_delete(current_head, str_delete)\n",
    "    end = time.perf_counter()\n",
    "    time_ht_delete_so.append(end - start)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0ce9bbd",
   "metadata": {},
   "source": [
    "## Создание CSV файла\n",
    "Запишем все рание полученные данные в CSV файл"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "ce249772",
   "metadata": {},
   "outputs": [],
   "source": [
    "results = [\n",
    "    [\"Структура\", \"Режим\", \"Операция\", \"Время (сек)\"],\n",
    "    [\"LinkedList\", \"случайный\", \"вставка\", sum(time_ll_insert_sh)/len(time_ll_insert_sh)],\n",
    "    [\"LinkedList\", \"сортированный\", \"вставка\", sum(time_ll_insert_so)/len(time_ll_insert_so)],\n",
    "    [\"LinkedList\", \"случайный\", \"поиск\", sum(time_ll_find_sh)/len(time_ll_find_sh)],\n",
    "    [\"LinkedList\", \"сортированный\", \"поиск\", sum(time_ll_find_so)/len(time_ll_find_so)],\n",
    "    [\"LinkedList\", \"случайный\", \"удаление\", sum(time_ll_delete_sh)/len(time_ll_delete_sh)],\n",
    "    [\"LinkedList\", \"сортированный\", \"удаление\", sum(time_ll_delete_so)/len(time_ll_delete_so)],\n",
    "    [\"HashTable\", \"случайный\", \"вставка\", sum(time_ht_insert_sh)/len(time_ht_insert_sh)],\n",
    "    [\"HashTable\", \"сортированный\", \"вставка\", sum(time_ht_insert_so)/len(time_ht_insert_so)],\n",
    "    [\"HashTable\", \"случайный\", \"поиск\", sum(time_ht_find_sh)/len(time_ht_find_sh)],\n",
    "    [\"HashTable\", \"сортированный\", \"поиск\", sum(time_ht_find_so)/len(time_ht_find_so)],\n",
    "    [\"HashTable\", \"случайный\", \"удаление\", sum(time_ht_delete_sh)/len(time_ht_delete_sh)],\n",
    "    [\"HashTable\", \"сортированный\", \"удаление\", sum(time_ht_delete_so)/len(time_ht_delete_so)],\n",
    "    [\"BinaryTree\", \"случайный\", \"вставка\", sum(time_bt_insert_sh)/len(time_bt_insert_sh)],\n",
    "    [\"BinaryTree\", \"сортированный\", \"вставка\", sum(time_bt_insert_so)/len(time_bt_insert_so)],\n",
    "    [\"BinaryTree\", \"случайный\", \"поиск\", sum(time_bt_find_sh)/len(time_bt_find_sh)],\n",
    "    [\"BinaryTree\", \"сортированный\", \"поиск\", sum(time_bt_find_so)/len(time_bt_find_so)],\n",
    "    [\"BinaryTree\", \"случайный\", \"удаление\", sum(time_bt_delete_sh)/len(time_bt_delete_sh)],\n",
    "    [\"BinaryTree\", \"сортированный\", \"удаление\", sum(time_bt_delete_so)/len(time_bt_delete_so)],\n",
    "]\n",
    "\n",
    "with open(\"results.csv\", \"w\", newline=\"\") as f:\n",
    "    writer = csv.writer(f)\n",
    "    writer.writerows(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69023ea2",
   "metadata": {},
   "source": [
    "## Построение графиков и сравнение данных между собой\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "09467dd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "t_ll = [sum(time_ll_insert_sh)/len(time_ll_insert_sh), sum(time_ll_insert_so)/len(time_ll_insert_so), sum(time_ll_find_sh)/len(time_ll_find_sh), sum(time_ll_find_so)/len(time_ll_find_so), sum(time_ll_delete_sh)/len(time_ll_delete_sh), sum(time_ll_delete_so)/len(time_ll_delete_so)]\n",
    "t_ht = [sum(time_ht_insert_sh)/len(time_ht_insert_sh), sum(time_ht_insert_so)/len(time_ht_insert_so), sum(time_ht_find_sh)/len(time_ht_find_sh), sum(time_ht_find_so)/len(time_ht_find_so), sum(time_ht_delete_sh)/len(time_ht_delete_sh), sum(time_ht_delete_so)/len(time_ht_delete_so)]\n",
    "t_bt = [sum(time_bt_insert_sh)/len(time_bt_insert_sh), sum(time_bt_insert_so)/len(time_bt_insert_so), sum(time_bt_find_sh)/len(time_bt_find_sh), sum(time_bt_find_so)/len(time_bt_find_so), sum(time_bt_delete_sh)/len(time_bt_delete_sh), sum(time_bt_delete_so)/len(time_bt_delete_so)]\n",
    "categoris = [\"рандомное добавление\", \"отсортированное добавление\", \"рандомный поиск\", \"отсортированный поиск\", \"рандомное удаление\", \"отсортированное удаление\"]\n",
    "n = np.linspace(1,5, 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "823d083c",
   "metadata": {},
   "source": [
    "### Графики 5 замеров"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "bdb0dfda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 319108 characters]",
      "text/plain": [
       "<Figure size 2000x1500 with 6 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(\"Графики изначальных замеров\", figsize = (20,15))\n",
    "\n",
    "plt.subplot(3,2,1)\n",
    "\n",
    "plt.scatter(n, time_ll_insert_sh, color = \"#88D94C\", label = \"LinkedList\")\n",
    "plt.scatter(n, time_bt_insert_sh, color = \"#FF43A4\", label = \"BinaryTree\")\n",
    "plt.scatter(n, time_ht_insert_sh, color = \"#EEDC82\", label = \"HashTable\")\n",
    "plt.axhline(t_ll[0], color = \"#88D94C\", linestyle = \"--\", label = f\"Среднее для LinkedList = {t_ll[0]:.6f}\")\n",
    "plt.axhline(t_bt[0], color = \"#FF43A4\", linestyle = \"--\", label = f\"Среднее для BinaryTree = {t_bt[0]:.6f}\")\n",
    "plt.axhline(t_ht[0], color = \"#EEDC82\", linestyle = \"--\", label = f\"Среднее для HashTable = {t_ht[0]:.6f}\")\n",
    "\n",
    "plt.legend()\n",
    "plt.title(f\"Время добавление случайных данных\")\n",
    "\n",
    "plt.ylabel(\"Время(сек)\")\n",
    "plt.xlabel(\"Попытки\")\n",
    "plt.xticks(np.arange(1, 6, 1))\n",
    "\n",
    "plt.grid(alpha = 0.5, linestyle = \"--\")\n",
    "\n",
    "plt.subplot(3,2,2)\n",
    "\n",
    "plt.scatter(n, time_ll_insert_so, color = \"#88D94C\", label = \"LinkedList\")\n",
    "plt.scatter(n, time_bt_insert_so, color = \"#FF43A4\", label = \"BinaryTree\")\n",
    "plt.scatter(n, time_ht_insert_so, color = \"#EEDC82\", label = \"HashTable\")\n",
    "plt.axhline(t_ll[1], color = \"#88D94C\", linestyle = \"--\", label = f\"Среднее для LinkedList = {t_ll[1]:.6f}\")\n",
    "plt.axhline(t_bt[1], color = \"#FF43A4\", linestyle = \"--\", label = f\"Среднее для BinaryTree = {t_bt[1]:.6f}\")\n",
    "plt.axhline(t_ht[1], color = \"#EEDC82\", linestyle = \"--\", label = f\"Среднее для HashTable = {t_ht[1]:.6f}\")\n",
    "\n",
    "plt.legend()\n",
    "plt.title(f\"Время добавление сортированных данных\")\n",
    "\n",
    "plt.ylabel(\"Время(сек)\")\n",
    "plt.xlabel(\"Попытки\")\n",
    "plt.xticks(np.arange(1, 6, 1))\n",
    "\n",
    "\n",
    "plt.grid(alpha = 0.5, linestyle = \"--\")\n",
    "\n",
    "plt.subplot(3,2,3)\n",
    "\n",
    "plt.scatter(n, time_ll_find_sh, color = \"#88D94C\", label = \"LinkedList\")\n",
    "plt.scatter(n, time_bt_find_sh, color = \"#FF43A4\", label = \"BinaryTree\")\n",
    "plt.scatter(n, time_ht_find_sh, color = \"#EEDC82\", label = \"HashTable\")\n",
    "plt.axhline(t_ll[2], color = \"#88D94C\", linestyle = \"--\", label = f\"Среднее для LinkedList = {t_ll[2]:.6f}\")\n",
    "plt.axhline(t_bt[2], color = \"#FF43A4\", linestyle = \"--\", label = f\"Среднее для BinaryTree = {t_bt[2]:.6f}\")\n",
    "plt.axhline(t_ht[2], color = \"#EEDC82\", linestyle = \"--\", label = f\"Среднее для HashTable = {t_ht[2]:.6f}\")\n",
    "\n",
    "plt.legend()\n",
    "plt.title(f\"Время поиска случайных данных\")\n",
    "\n",
    "plt.ylabel(\"Время(сек)\")\n",
    "plt.xlabel(\"Попытки\")\n",
    "plt.xticks(np.arange(1, 6, 1))\n",
    "\n",
    "\n",
    "plt.grid(alpha = 0.5, linestyle = \"--\")\n",
    "\n",
    "\n",
    "plt.subplot(3,2,4)\n",
    "\n",
    "plt.scatter(n, time_ll_find_so, color = \"#88D94C\", label = \"LinkedList\")\n",
    "plt.scatter(n, time_bt_find_so, color = \"#FF43A4\", label = \"BinaryTree\")\n",
    "plt.scatter(n, time_ht_find_so, color = \"#EEDC82\", label = \"HashTable\")\n",
    "plt.axhline(t_ll[3], color = \"#88D94C\", linestyle = \"--\", label = f\"Среднее для LinkedList = {t_ll[3]:.6f}\")\n",
    "plt.axhline(t_bt[3], color = \"#FF43A4\", linestyle = \"--\", label = f\"Среднее для BinaryTree = {t_bt[3]:.6f}\")\n",
    "plt.axhline(t_ht[3], color = \"#EEDC82\", linestyle = \"--\", label = f\"Среднее для HashTable = {t_ht[3]:.6f}\")\n",
    "\n",
    "plt.legend()\n",
    "plt.title(f\"Время поиска сортированных данных\")\n",
    "\n",
    "plt.ylabel(\"Время(сек)\")\n",
    "plt.xlabel(\"Попытки\")\n",
    "plt.xticks(np.arange(1, 6, 1))\n",
    "\n",
    "\n",
    "plt.grid(alpha = 0.5, linestyle = \"--\")\n",
    "\n",
    "plt.subplot(3,2,5)\n",
    "\n",
    "plt.scatter(n, time_ll_delete_sh, color = \"#88D94C\", label = \"LinkedList\")\n",
    "plt.scatter(n, time_bt_delete_sh, color = \"#FF43A4\", label = \"BinaryTree\")\n",
    "plt.scatter(n, time_ht_delete_sh, color = \"#EEDC82\", label = \"HashTable\")\n",
    "plt.axhline(t_ll[4], color = \"#88D94C\", linestyle = \"--\", label = f\"Среднее для LinkedList = {t_ll[4]:.6f}\")\n",
    "plt.axhline(t_bt[4], color = \"#FF43A4\", linestyle = \"--\", label = f\"Среднее для BinaryTree = {t_bt[4]:.6f}\")\n",
    "plt.axhline(t_ht[4], color = \"#EEDC82\", linestyle = \"--\", label = f\"Среднее для HashTable = {t_ht[4]:.6f}\")\n",
    "\n",
    "plt.legend()\n",
    "plt.title(f\"Время удаления для случайных данных\")\n",
    "\n",
    "plt.ylabel(\"Время(сек)\")\n",
    "plt.xlabel(\"Попытки\")\n",
    "plt.xticks(np.arange(1, 6, 1))\n",
    "\n",
    "\n",
    "plt.grid(alpha = 0.5, linestyle = \"--\")\n",
    "\n",
    "plt.subplot(3,2,6)\n",
    "\n",
    "plt.scatter(n, time_ll_delete_so, color = \"#88D94C\", label = \"LinkedList\")\n",
    "plt.scatter(n, time_bt_delete_so, color = \"#FF43A4\", label = \"BinaryTree\")\n",
    "plt.scatter(n, time_ht_delete_so, color = \"#EEDC82\", label = \"HashTable\")\n",
    "plt.axhline(t_ll[5], color = \"#88D94C\", linestyle = \"--\", label = f\"Среднее для LinkedList = {t_ll[5]:.6f}\")\n",
    "plt.axhline(t_bt[5], color = \"#FF43A4\", linestyle = \"--\", label = f\"Среднее для BinaryTree = {t_bt[5]:.6f}\")\n",
    "plt.axhline(t_ht[5], color = \"#EEDC82\", linestyle = \"--\", label = f\"Среднее для HashTable = {t_ht[5]:.6f}\")\n",
    "\n",
    "plt.legend()\n",
    "plt.title(f\"Время удаления для сортированных данных\")\n",
    "\n",
    "plt.ylabel(\"Время(сек)\")\n",
    "plt.xlabel(\"Попытки\")\n",
    "plt.xticks(np.arange(1, 6, 1))\n",
    "\n",
    "\n",
    "plt.grid(alpha = 0.5, linestyle = \"--\")\n",
    "\n",
    "plt.savefig('analysis.png')\n",
    "plt.tight_layout() \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2076283d",
   "metadata": {},
   "source": [
    "### Сравнение усреднённых данных"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "d06c4bb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 181540 characters]",
      "text/plain": [
       "<Figure size 2000x1500 with 9 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(\"Усреднённые графики\", figsize = (20,15))\n",
    "\n",
    "plt.subplot(3,3,1)\n",
    "plt.bar(categoris[0], t_ll[0], color = \"#EEDC82\")\n",
    "plt.bar(categoris[1], t_ll[1], color = \"#88D94C\")\n",
    "\n",
    "plt.title(f\"Время добавление для LinkedList\")\n",
    "\n",
    "plt.ylabel(\"время работы(секунды)\")\n",
    "\n",
    "plt.subplot(3,3,2)\n",
    "plt.bar(categoris[2], t_ll[2], color = \"#EEDC82\")\n",
    "plt.bar(categoris[3], t_ll[3], color = \"#88D94C\")\n",
    "\n",
    "plt.title(f\"Время поиска элементов в LinkedList\")\n",
    "\n",
    "plt.ylabel(\"время работы(секунды)\")\n",
    "\n",
    "\n",
    "plt.subplot(3,3,3)\n",
    "plt.bar(categoris[4], t_ll[4], color = \"#EEDC82\")\n",
    "plt.bar(categoris[5], t_ll[5], color = \"#88D94C\")\n",
    "\n",
    "plt.title(f\"Время удаления элементов в LinkedList\")\n",
    "\n",
    "plt.ylabel(\"время работы(секунды)\")\n",
    "\n",
    "plt.subplot(3,3,4)\n",
    "plt.bar(categoris[0], t_ht[0], color = \"#EEDC82\")\n",
    "plt.bar(categoris[1], t_ht[1], color = \"#88D94C\")\n",
    "\n",
    "plt.title(f\"Время добавление для HashTable\")\n",
    "\n",
    "plt.ylabel(\"время работы(секунды)\")\n",
    "\n",
    "plt.subplot(3,3,5)\n",
    "plt.bar(categoris[2], t_ht[2], color = \"#EEDC82\")\n",
    "plt.bar(categoris[3], t_ht[3], color = \"#88D94C\")\n",
    "\n",
    "plt.title(f\"Время поиска элементов в HashTable\")\n",
    "\n",
    "plt.ylabel(\"время работы(секунды)\")\n",
    "\n",
    "\n",
    "plt.subplot(3,3,6)\n",
    "plt.bar(categoris[4], t_bt[4], color = \"#EEDC82\")\n",
    "plt.bar(categoris[5], t_bt[5], color = \"#88D94C\")\n",
    "\n",
    "plt.title(f\"Время удаления элементов в HashTable\")\n",
    "\n",
    "plt.ylabel(\"время работы(секунды)\")\n",
    "\n",
    "plt.subplot(3,3,7)\n",
    "plt.bar(categoris[0], t_bt[0], color = \"#EEDC82\")\n",
    "plt.bar(categoris[1], t_bt[1], color = \"#88D94C\")\n",
    "\n",
    "plt.title(f\"Время добавление для BinaryTree\")\n",
    "\n",
    "plt.ylabel(\"время работы(секунды)\")\n",
    "\n",
    "plt.subplot(3,3,8)\n",
    "plt.bar(categoris[2], t_bt[2], color = \"#EEDC82\")\n",
    "plt.bar(categoris[3], t_bt[3], color = \"#88D94C\")\n",
    "\n",
    "plt.title(f\"Время поиска элементов в BinaryTree\")\n",
    "\n",
    "plt.ylabel(\"время работы(секунды)\")\n",
    "\n",
    "\n",
    "plt.subplot(3,3,9)\n",
    "plt.bar(categoris[4], t_bt[4], color = \"#EEDC82\")\n",
    "plt.bar(categoris[5], t_bt[5], color = \"#88D94C\")\n",
    "\n",
    "plt.title(f\"Время удаления элементов в BinaryTree\")\n",
    "\n",
    "plt.ylabel(\"время работы(секунды)\")\n",
    "\n",
    "plt.savefig('analysis_midle.png')\n",
    "plt.tight_layout() \n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77155f07",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
