From ffa9eade3115728e8a1701d5039373f502dac10f Mon Sep 17 00:00:00 2001 From: Call Me Maker <133881086+Playmaker3334@users.noreply.github.com> Date: Thu, 4 Apr 2024 20:00:39 -0600 Subject: [PATCH 1/2] Add files via upload --- ArbolBinario.ipynb | 214 +++++++++++++++++++++++++++++++ Listaligada_vs_array_numpy.ipynb | 165 ++++++++++++++++++++++++ 2 files changed, 379 insertions(+) create mode 100644 ArbolBinario.ipynb create mode 100644 Listaligada_vs_array_numpy.ipynb diff --git a/ArbolBinario.ipynb b/ArbolBinario.ipynb new file mode 100644 index 0000000..ebf2981 --- /dev/null +++ b/ArbolBinario.ipynb @@ -0,0 +1,214 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "source": [ + "!pip install plotly\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "TYtRusUh1Z28", + "outputId": "99f64ea9-72fd-490a-898f-3d7349a47dcb" + }, + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Requirement already satisfied: plotly in /usr/local/lib/python3.10/dist-packages (5.15.0)\n", + "Requirement already satisfied: tenacity>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from plotly) (8.2.3)\n", + "Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from plotly) (24.0)\n" + ] + } + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "lU-3p2Ym066s" + }, + "outputs": [], + "source": [ + "class Nodo:\n", + " def __init__(self, valor):\n", + " self.valor = valor\n", + " self.izquierda = None\n", + " self.derecha = None\n", + "\n", + "class ArbolBinario:\n", + " def __init__(self):\n", + " self.raiz = None\n", + "\n", + " def insertar(self, valor):\n", + " if self.raiz is None:\n", + " self.raiz = Nodo(valor)\n", + " else:\n", + " self._insertar_recursivo(valor, self.raiz)\n", + "\n", + " def _insertar_recursivo(self, valor, nodo):\n", + " if valor < nodo.valor:\n", + " if nodo.izquierda is None:\n", + " nodo.izquierda = Nodo(valor)\n", + " else:\n", + " self._insertar_recursivo(valor, nodo.izquierda)\n", + " else:\n", + " if nodo.derecha is None:\n", + " nodo.derecha = Nodo(valor)\n", + " else:\n", + " self._insertar_recursivo(valor, nodo.derecha)\n", + "\n", + " def buscar(self, valor):\n", + " return self._buscar_recursivo(valor, self.raiz)\n", + "\n", + " def _buscar_recursivo(self, valor, nodo):\n", + " if nodo is None:\n", + " return False\n", + " if nodo.valor == valor:\n", + " return True\n", + " elif valor < nodo.valor:\n", + " return self._buscar_recursivo(valor, nodo.izquierda)\n", + " else:\n", + " return self._buscar_recursivo(valor, nodo.derecha)\n" + ] + }, + { + "cell_type": "code", + "source": [ + "import time\n", + "import plotly.graph_objects as go\n", + "\n", + "def medir_tiempo_insercion(arbol, valores):\n", + " tiempos = []\n", + " for valor in valores:\n", + " inicio = time.time()\n", + " arbol.insertar(valor)\n", + " fin = time.time()\n", + " tiempos.append((fin - inicio) * 1e6)\n", + " return tiempos\n", + "\n", + "def medir_tiempo_busqueda(arbol, valores):\n", + " tiempos = []\n", + " for valor in valores:\n", + " inicio = time.time()\n", + " arbol.buscar(valor)\n", + " fin = time.time()\n", + " tiempos.append((fin - inicio) * 1e6)\n", + " return tiempos\n" + ], + "metadata": { + "id": "FGKhQ1bB1Brk" + }, + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def graficar_tiempos(tiempos_insercion, tiempos_busqueda):\n", + " fig = go.Figure()\n", + " fig.add_trace(go.Scatter(y=tiempos_insercion, mode='lines+markers', name='Tiempo Inserción'))\n", + " fig.add_trace(go.Scatter(y=tiempos_busqueda, mode='lines+markers', name='Tiempo Búsqueda'))\n", + " fig.update_layout(title='Tiempos de Operaciones en Árbol Binario (microsegundos)',\n", + " xaxis_title='Operación #',\n", + " yaxis_title='Tiempo (microsegundos)',\n", + " template='plotly_dark')\n", + " fig.show()\n" + ], + "metadata": { + "id": "7WYaurEu1IbT" + }, + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Instanciar el árbol\n", + "arbol = ArbolBinario()\n", + "\n", + "# Lista de valores para insertar en el árbol\n", + "valores_para_insertar = [5, 2, 8, 1, 3, 7, 10]\n", + "\n", + "# Medir tiempo de inserción\n", + "tiempos_insercion = medir_tiempo_insercion(arbol, valores_para_insertar)\n", + "\n", + "# Lista de valores para buscar en el árbol\n", + "valores_para_buscar = [7, 3, 10]\n", + "\n", + "# Medir tiempo de búsqueda\n", + "tiempos_busqueda = medir_tiempo_busqueda(arbol, valores_para_buscar)\n", + "\n", + "# Graficar los tiempos\n", + "graficar_tiempos(tiempos_insercion, tiempos_busqueda)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 542 + }, + "id": "f61xQpj01h9v", + "outputId": "b5b3fcf9-b32b-4080-e229-4526b2de3b0e" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/html": [ + "\n", + "
\n", + "\n", + "