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
60 changes: 60 additions & 0 deletions List_array_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import numpy as np
import time
import matplotlib.pyplot as plt

class ArrayLinkedList:
def __init__(self):

self.array_list = np.array([])

def add_item(self, value):
"""
Adds a new item to the end of the list.

Parameters:
- value: The value to be added to the list.
"""
start = time.time()
self.array_list = np.append(self.array_list, value)
end = time.time()
return end - start

def remove_item(self, value):

start = time.time()
try:
index = np.where(self.array_list == value)[0][0] # Finds the index of the first occurrence
self.array_list = np.delete(self.array_list, index) # Removes the value at that index
except IndexError:
print(f"The value {value} is not in the list.")
end = time.time()
return end - start

def print_list(self):
"""
Prints all elements of the list.
"""
print(self.array_list)


# Example of usage:
array_linked_list = ArrayLinkedList()
add_times = []
remove_times = []

for i in range(1000):
add_time = array_linked_list.add_item(i)
add_times.append(add_time)

for i in range(1000):
remove_time = array_linked_list.remove_item(i)
remove_times.append(remove_time)


print("Graph of time to add and remove items in Array Linked List with NumPy:")
plt.plot(range(1000), add_times, label='Add')
plt.plot(range(1000), remove_times, label='Remove')
plt.xlabel('Number of items')
plt.ylabel('Time (seconds)')
plt.legend()
plt.show()
74 changes: 74 additions & 0 deletions binary_tree_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

class NodoArbol:
def __init__(self, value):
self.value = value
self.left = None
self.right = None


#Funcion para crear el árbol

class ArbolBinarioBusqueda:
def __init__(self):
self.root = None

def insertar(self, value):
if self.root is None:
self.root = NodoArbol(value)
else:
self._insertar_recursivo(self.root, value)

def _insertar_recursivo(self, node, value):
if value < node.value:
if node.left is None:
node.left = NodoArbol(value)
else:
self._insertar_recursivo(node.left, value)
elif value > node.value:
if node.right is None:
node.right = NodoArbol(value)
else:
self._insertar_recursivo(node.right, value)

def buscar(self, value):
return self._buscar_recursivo(self.root, value)

def _buscar_recursivo(self, node, value):
if node is None or node.value == value:
return node is not None
if value < node.value:
return self._buscar_recursivo(node.left, value)
else:
return self._buscar_recursivo(node.right, value)

def _imprimir_recursivo(self, node, space):
if node is not None:
space += 5
self._imprimir_recursivo(node.right, space)
print()
for _ in range(5, space):
print(end=" ")
print(node.value)
self._imprimir_recursivo(node.left, space)

def imprimir(self):
self._imprimir_recursivo(self.root, 0)


# Ejemplo de uso
#Insertar valores al arbol
arbol = ArbolBinarioBusqueda()
arbol.insertar(20)
arbol.insertar(9)
arbol.insertar(15)
arbol.insertar(9)
arbol.insertar(1)
arbol.insertar(12)
arbol.insertar(14)
arbol.insertar(23)
arbol.insertar(19)
arbol.insertar(1)

# Mostrar el árbol binario
print("Árbol binario:")
arbol.imprimir()
1 change: 1 addition & 0 deletions example_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Este es un archivo de ejemplo
95 changes: 95 additions & 0 deletions lista_ligada_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import time
import matplotlib.pyplot as plt

class Node:
def __init__(self, value):

self.value = value
self.next = None # Link to the next node


class LinkedList:
def __init__(self):
"""
Constructor for the LinkedList class.
Initializes an empty linked list.
"""
self.head = None # Reference to the first node in the list

def add_element(self, value):
"""
Adds a new element to the end of the linked list.

Parameters:
- value: The value to be added to the list.
"""
start = time.time()
new_node = Node(value) # Creates a new node with the provided value
if self.head is None: # If the list is empty, the new node becomes the head
self.head = new_node
else:
# Traverse the list to find the last node
current = self.head
while current.next:
current = current.next
# Adds the new node to the end of the list
current.next = new_node
end = time.time()
return end - start

def remove_element(self, value):
"""
Removes the first node containing the provided value from the linked list.

Parameters:
- value: The value of the node to be removed.
"""
start = time.time()
current = self.head # Starts from the head of the list
previous = None # Stores the node before the current one
# Finds the node with the specified value
while current and current.value != value:
previous = current
current = current.next
if current: # If a node with the specified value is found
if previous is None: # If the node to be removed is the head
self.head = current.next # Updates the head of the list
else:
# Removes the current node by updating the link of the previous node
previous.next = current.next
end = time.time()
return end - start

def print_list(self):
"""
Prints the values of all nodes in the linked list.
"""
current = self.head # Starts from the head of the list
while current:
print(current.value, end=" -> ") # Prints the value of the node
current = current.next # Moves to the next node
print("None")


# Example of usage:
linked_list = LinkedList()
add_times = []
remove_times = []

for i in range(1000):
add_time = linked_list.add_element(i)
add_times.append(add_time)

for i in range(1000):
remove_time = linked_list.remove_element(i)
remove_times.append(remove_time)


#show all
print("Graph of time to add and remove elements in Linked List:")
plt.plot(range(1000), add_times, label='Add')
plt.plot(range(1000), remove_times, label='Remove')
plt.xlabel('Number of elements')
plt.ylabel('Time (seconds)')
plt.legend()
plt.show()