From c8083c34a741136fa59e27278db639687b789851 Mon Sep 17 00:00:00 2001 From: valinyourarea <157547264+valinyourarea@users.noreply.github.com> Date: Tue, 9 Apr 2024 11:29:11 -0600 Subject: [PATCH] Add files via upload --- ArbolBinario.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ ArrayEnlist.py | 64 +++++++++++++++++++++++++++++++++++++++++ ListaEnlazada.py | 69 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 208 insertions(+) create mode 100644 ArbolBinario.py create mode 100644 ArrayEnlist.py create mode 100644 ListaEnlazada.py diff --git a/ArbolBinario.py b/ArbolBinario.py new file mode 100644 index 0000000..2fca4ea --- /dev/null +++ b/ArbolBinario.py @@ -0,0 +1,75 @@ +import networkx as nx +import matplotlib.pyplot as plt + +class NodoArbol: + def __init__(self, valor): + self.valor = valor + self.izquierda = None + self.derecha = None + +class ArbolBusqueda: + def __init__(self): + self.raiz = None + + def insertar_nodo(self, valor): + if self.raiz is None: + self.raiz = NodoArbol(valor) + else: + self._insertar_nodo_recursivo(self.raiz, valor) + + def _insertar_nodo_recursivo(self, nodo, valor): + if valor < nodo.valor: + if nodo.izquierda is None: + nodo.izquierda = NodoArbol(valor) + else: + self._insertar_nodo_recursivo(nodo.izquierda, valor) + elif valor > nodo.valor: + if nodo.derecha is None: + nodo.derecha = NodoArbol(valor) + else: + self._insertar_nodo_recursivo(nodo.derecha, valor) + + def buscar_valor(self, valor): + return self._buscar_valor_recursivo(self.raiz, valor) + + def _buscar_valor_recursivo(self, nodo, valor): + if nodo is None or nodo.valor == valor: + return nodo is not None + if valor < nodo.valor: + return self._buscar_valor_recursivo(nodo.izquierda, valor) + else: + return self._buscar_valor_recursivo(nodo.derecha, valor) + + def _crear_grafo_recursivo(self, G, nodo): + if nodo is not None: + G.add_node(nodo.valor) + if nodo.izquierda is not None: + G.add_edge(nodo.valor, nodo.izquierda.valor) + self._crear_grafo_recursivo(G, nodo.izquierda) + if nodo.derecha is not None: + G.add_edge(nodo.valor, nodo.derecha.valor) + self._crear_grafo_recursivo(G, nodo.derecha) + + def dibujar_arbol(self): + G = nx.DiGraph() + self._crear_grafo_recursivo(G, self.raiz) + pos = nx.spring_layout(G) # Posición de los nodos + nx.draw(G, pos, with_labels=True, arrows=True) + plt.show() + +# Ejemplo +arbol_busqueda = ArbolBusqueda() +arbol_busqueda.insertar_nodo(1) +arbol_busqueda.insertar_nodo(2) +arbol_busqueda.insertar_nodo(33) +arbol_busqueda.insertar_nodo(4) +arbol_busqueda.insertar_nodo(55) +arbol_busqueda.insertar_nodo(7) +arbol_busqueda.insertar_nodo(66) +arbol_busqueda.insertar_nodo(13) +arbol_busqueda.insertar_nodo(77) +arbol_busqueda.insertar_nodo(9) + +# Dibujar árbol +print("Dibujando el árbol de búsqueda:") +arbol_busqueda.dibujar_arbol() diff --git a/ArrayEnlist.py b/ArrayEnlist.py new file mode 100644 index 0000000..ef05ae6 --- /dev/null +++ b/ArrayEnlist.py @@ -0,0 +1,64 @@ +import numpy as np +import time +import matplotlib.pyplot as plt + +class ListaVector: + def __init__(self): + """ + Constructor de la clase ListaVector. + Inicializa una lista vacía utilizando un vector NumPy. + """ + self.vector = np.array([]) + + def agregar_elemento_vector(self, valor): + """ + Agrega un nuevo elemento al final del vector. + Parámetros: + - valor: El valor que se agregará al vector. + """ + inicio = time.time() + self.vector = np.append(self.vector, valor) + fin = time.time() + return fin - inicio + + def eliminar_elemento_vector(self, valor): + """ + Elimina la primera ocurrencia del valor proporcionado en el vector. + Parámetros: + - valor: El valor del elemento que se desea eliminar. + """ + inicio = time.time() + try: + indice = np.where(self.vector == valor)[0][0] + self.vector = np.delete(self.vector, indice) + except IndexError: + print(f"El valor {valor} no está en el vector.") + fin = time.time() + return fin - inicio + + def imprimir_vector(self): + """ + Imprime todos los elementos del vector. + """ + print(self.vector) + + +#ejemplo de uso: +lista_vector = ListaVector() +tiempos_agregar_vector = [] +tiempos_eliminar_vector = [] + +for i in range(1000): + tiempo_agregar_vector = lista_vector.agregar_elemento_vector(i) + tiempos_agregar_vector.append(tiempo_agregar_vector) + + tiempo_eliminar_vector = lista_vector.eliminar_elemento_vector(i) + tiempos_eliminar_vector.append(tiempo_eliminar_vector) + +print("Gráfico de tiempo de agregar y eliminar elementos en Lista Vector con NumPy:") +plt.plot(tiempos_agregar_vector, range(1000), label='Agregar') +plt.plot(tiempos_eliminar_vector, range(1000), label='Eliminar') +plt.xlabel('Tiempo (segundos)') +plt.ylabel('Número de elementos') +plt.legend() +plt.show() diff --git a/ListaEnlazada.py b/ListaEnlazada.py new file mode 100644 index 0000000..b7433de --- /dev/null +++ b/ListaEnlazada.py @@ -0,0 +1,69 @@ +import time +import random +import matplotlib.pyplot as plt + +class NodoLista: + def __init__(self, valor): + self.valor = valor + self.siguiente = None + +class ListaEnlazadaSimple: + def __init__(self): + self.inicio = None + + def agregar_elemento_lista(self, valor): + inicio = time.time() + nuevo_nodo = NodoLista(valor) + if self.inicio is None: + self.inicio = nuevo_nodo + else: + actual = self.inicio + while actual.siguiente: + actual = actual.siguiente + actual.siguiente = nuevo_nodo + fin = time.time() + return fin - inicio + + def eliminar_elemento_lista(self, valor): + inicio = time.time() + actual = self.inicio + anterior = None + while actual and actual.valor != valor: + anterior = actual + actual = actual.siguiente + if actual: + if anterior is None: + self.inicio = actual.siguiente + else: + anterior.siguiente = actual.siguiente + fin = time.time() + return fin - inicio + + def imprimir_elementos_lista(self): + actual = self.inicio + while actual: + print(actual.valor, end=" -> ") + actual = actual.siguiente + print("None") + +# Ejemplo de uso: +lista_enlazada = ListaEnlazadaSimple() +tiempos_agregar_lista = [] +tiempos_eliminar_lista = [] + +for i in range(100): + tiempo_agregar_lista = lista_enlazada.agregar_elemento_lista(random.randint(0, 1000)) + tiempos_agregar_lista.append(tiempo_agregar_lista) + +for i in range(100): + tiempo_eliminar_lista = lista_enlazada.eliminar_elemento_lista(random.randint(0, 1000)) + tiempos_eliminar_lista.append(tiempo_eliminar_lista) + +# Graficar los tiempos acumulados +print("Gráfico de tiempo de agregar y eliminar elementos en lista enlazada:") +plt.plot(range(100), [sum(tiempos_agregar_lista[:i]) for i in range(1, 101)], label='Agregar') +plt.plot(range(100), [sum(tiempos_eliminar_lista[:i]) for i in range(1, 101)], label='Eliminar') +plt.xlabel('Número de elementos') +plt.ylabel('Tiempo acumulado (segundos)') +plt.legend() +plt.show()