You are on page 1of 4

1. Construya un arbol binario de busqueda (Binary Search Tree).

class nodo:
izq , der, dato = None, None, 0

def __init__(self, dato):


# crea un nodo
self.izq = None
self.der = None
self.dato = dato

class arbolBinario:
def __init__(self):
# inicializa la raiz
self.raiz = None

def agregarNodo(self, dato):


# crea un nuevo nodo y lo devuelve
return nodo(dato)

def insert(self, raiz, dato):


# inserta un dato nuevo en el rbol
if raiz == None:
# si no hay nodos en el rbol lo agrega
return self.agregarNodo(dato)
else:
# si hay nodos en el rbol lo recorre
if dato <= raiz.dato:
# si el dato ingresado es menor que el dato guardado va al subrbol izquierdo
raiz.izq = self.insert(raiz.izq, dato)
else:
# si no, procesa el subrbol derecho
raiz.der = self.insert(raiz.der, dato)
return raiz

2. Construya un hash table, de su preferencia.


# Hash para entero sin cambios
print('Hash para 181 es:', hash(181))

# Hash para decimal


print('Hash para 181.23 es:',hash(181.23))

# Hash para string


print('Hash para algoritmos es:', hash('algoritmos'))

3. Compare Selection Sort, Quick Sort, y Merge Sort (muestre el cdigo).

Selection Sort
def selectionSort(lista):
for fillslot in range(len(lista)-1,0,-1):
positionOfMax=0
for ubicacion in range(1,fillslot+1):
if lista[ubicacion]>lista[positionOfMax]:
positionOfMax = ubicacion

temp = lista[fillslot]
lista[fillslot] = lista[positionOfMax]
lista[positionOfMax] = temp

lista = [1,2,6,5,4,3]
selectionSort(lista)
print(lista)
resultado: [1, 2, 3, 4, 5, 6]
Quick Sort
def quickSort(lista):
quickSortHelper(lista,0,len(lista)-1)

def quickSortHelper(lista,a,z):
if a<z:

splitpoint = partition(lista,a,z)

quickSortHelper(lista,a,splitpoint-1)
quickSortHelper(lista,splitpoint+1,z)

def partition(lista,a,z):
pivotvalue = lista[a]

leftmark = a+1
rightmark = z

done = False
while not done:

while leftmark <= rightmark and lista[leftmark] <= pivotvalue:


leftmark = leftmark + 1

while lista[rightmark] >= pivotvalue and rightmark >= leftmark:


rightmark = rightmark -1

if rightmark < leftmark:


done = True
else:
temp = lista[leftmark]
lista[leftmark] = lista[rightmark]
lista[rightmark] = temp

temp = lista[a]
lista[a] = lista[rightmark]
lista[rightmark] = temp

return rightmark

lista = [1,2,6,5,4,3]
quickSort(lista)
print(lista)

resultado: [1, 2, 3, 4, 5, 6]
Merge Sort
def mergeSort(lista):
print("Division ",lista)
if len(lista)>1:
mid = len(lista)//2
lefthalf = lista[:mid]
righthalf = lista[mid:]

mergeSort(lefthalf)
mergeSort(righthalf)

i=0
j=0
k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
lista[k]=lefthalf[i]
i=i+1
else:
lista[k]=righthalf[j]
j=j+1
k=k+1

while i < len(lefthalf):


lista[k]=lefthalf[i]
i=i+1
k=k+1

while j < len(righthalf):


lista[k]=righthalf[j]
j=j+1
k=k+1
print("union ",lista)

lista = [1,2,6,5,4,3]
mergeSort(lista)
print(lista)

resultado:
Division [1, 2, 6, 5, 4, 3] Division [5]

Division [1, 2, 6] union [5]

Division [1] Division [4, 3]

union [1] Division [4]

Division [2, 6] union [4]

Division [2] Division [3]

union [2] union [3]

Division [6] union [3, 4]

union [6] union [3, 4, 5]

union [2, 6] union [1, 2, 3, 4, 5, 6]

union [1, 2, 6] [1, 2, 3, 4, 5, 6]


Division [5, 4, 3]
4. Construya los algoritmos Breadth first search y Depth first search y
comparelos explicando cmo opera cada uno.

You might also like