Documente Academic
Documente Profesional
Documente Cultură
LABORATORIO N° 12
Alumno(s) Nota
Grupo
Ciclo I
Fecha de entrega
II.- SEGURIDAD:
Advertencia:
En este laboratorio está prohibida la manipulación del
hardware, conexiones eléctricas o de red; así como la
ingestión de alimentos o bebidas.
V.- RECURSOS:
En este laboratorio cada alumno trabajará con un equipo con Windows 10.
VII.- PROCEDIMIENTO:
EJERCICIO DE APLICACIÓN
Crear un programa que lea de un archivo de genere 1000 números aleatorios y los almacene en una lista. Luego
agregar la información a un árbol binario y realizar búsquedas de datos. Medir el tiempo en que demora una
búsqueda.
import random
from time import time
class BSTNode:
'''
Node BST definition
'''
def __init__(root, data):
root.left = None
root.right = None
root.data = data
currentNode=root
if currentNode == None:
return None
else:
if data == currentNode.data:
return currentNode.data
if data < currentNode.data:
return find(currentNode.left,data)
else:
return find(currentNode.right,data)
def buildBSTFromArray(list):
'''
Build Binary Search Tree
from Array
:return:
'''
root = None
def inOrderTraversal(root):
'''
Print value In Order Traversal
:param root:
:return:
'''
if not root:
return
inOrderTraversal(root.left)
print(root.data)
inOrderTraversal(root.right)
listar=[]
for i in range(1000):
numero = random.randint(1,1000)
listar.append(numero)
print('Arbol en base de un arreglo')
tiempoInicio = time()
root = buildBSTFromArray(listar)
inOrderTraversal(root)
tiempoPare= time()
tiempoProcesado = tiempoPare - tiempoInicio
print("Tiempo en el cual se crea nuestro arbol: ", tiempoProcesado)
print(" ")
num = int(input("Ingrese el numero a buscar: "))
tiempo_inicio = time()
dato = find(root, num)
tiempo_Pare = time()
print("Se encontro el numero: ",dato)