Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions ArbolBinario.py
Original file line number Diff line number Diff line change
@@ -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()
64 changes: 64 additions & 0 deletions ArrayEnlist.py
Original file line number Diff line number Diff line change
@@ -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()
69 changes: 69 additions & 0 deletions ListaEnlazada.py
Original file line number Diff line number Diff line change
@@ -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()