def modif_sw(arbol): file = abrir('starwars') busc = input('Ingrese nombre del personaje a modificar: ') val = busqueda_arbol(arbol, busc) if val is not None: print('1) Nombre') print('2) Altura') print('3) Peso') mod = input('Elija campo a modificar: ') sw = leer(file, pos) if mod == '1': nom = input('Ingrese el nuevo nombre: ') dato = Personaje(nom, sw.altura, sw.peso) modificar(file, pos, dato) sw = leer(file, pos) arbol = insertar_nodo(arbol, sw.nombre, pos) elif mod == '2': alt = int(input('Ingrese la nueva altura: ')) dato = Personaje(sw.nombre, alt, sw.peso) modificar(file, pos, dato) sw = leer(file, pos) arbol = insertar_nodo(arbol, sw.nombre, pos) elif mod == '3': peso = int(input('Ingrese el nuevo peso: ')) dato = Personaje(sw.nombre, sw.altura, peso) modificar(file, pos, dato) sw = leer(file, pos) arbol = insertar_nodo(arbol, sw.nombre, pos) else: print('ERROR') else: print('El personaje no existe') cerrar(file)
def alta_sw(arbol): file = abrir('starwars') nom = input('Ingrese un nombre: ') alt = int(input('Ingrese su altura: ')) peso = int(input('Ingrese su peso: ')) dato = Personaje(nom, alt, peso) insertar_nodo(arbol, dato.nombre) guardar(file, dato) cerrar(file) datos.append([nom, alt, peso])
def punto1(): arbol = None # 1 personajes = [ 'Dar Vader', 'Yoda', 'Han Solo', 'Leia Organa', 'C-3PO', 'Obi-Wan Kenobi', 'Chewbacca', 'R-2D2', 'Luke Skywalker', 'Greedo' ] pos = 0 for i in range(len(personajes)): dato = [personajes[pos], pos] # nombre personaje | nrr arbol = insertar_nodo(arbol, dato) pos += 1 # 2 print('Listado ordenado alfabéticamente de manera ascendente') inorden(arbol) print() # 3 pos = busqueda_arbol_vec(arbol, 'Yod') if pos is not None: print(pos.info[0], 'se encuentra en la posicion', pos.info[1]) else: print('Yoda NO se encuentra en el arbol') print() # 4 // el nivel de obi wan kenobi aparece dentro de la funcion por_nivel() print('Barrido por nivel') por_nivel(arbol) print() # 5 x = 'C-3PO' arbol, x = eliminar_nodo_vec(arbol, x) print('Se eliminó:', x) print() print('Nuevo arbol sin C-3PO') inorden(arbol) print() # 6 x = 'Dar Vader' arbol, x = eliminar_nodo_vec(arbol, x) print('Dar Vader esta mal cargado, modificando...') print() arbol = insertar_nodo_vec(arbol, ['Darth Vader', 0]) print('NUEVO ARBOL') inorden(arbol)
self.especie = especie self.año_nacimiento = año_nacimiento self.color_sable = color_sable self.ranking = ranking self.maestro = maestro # a arbol_nombre = None arbol_ranking = None arbol_especie = None file = abrir('jedis') pos = 0 while pos < len(file): jedi = leer(file, pos) arbol_nombre = insertar_nodo(arbol_nombre, jedi[0], pos) arbol_ranking = insertar_nodo(arbol_ranking, jedi[1], pos) arbol_especie = insertar_nodo(arbol_especie , jedi[2], pos) pos += 1 cerrar(file) # lee archivo jedis print('NOMBRE | RANKING | ESPECIE | MAESTRO | COLOR DE SABLE | ORIGEN | AÑO DE NACIMIENTO | ALTURA') arch = abrir('jedis') for i in range(0, len(arch)): print(leer(arch, i)) cerrar(arch) print() # b print('INORDEN POR NOMBRE Y RANKING')
from TDA_ArbolBin_AVL import insertar_nodo, altura, cortar_por_nivel, contar_nodos from TDA_ArbolBin_AVL import preorden print('EJERCICIO 12') print() arbol = None # a for i in range(1, 1024): arbol = insertar_nodo(arbol, i) cantidad = [0] contar_nodos(arbol, cantidad) print('Cantidad de nodos:', cantidad[0]) print('Altura del arbol:', altura(arbol)) print() bosque = [] cortar_por_nivel(arbol, bosque) print('Cortando el arbol...') print('Tamaño del bosque:', len(bosque)) print() for arbol in bosque: print('Raiz del arbol:', arbol.info) cantidad = [0] #preorden(arbol) contar_nodos(arbol, cantidad) print('Cantidad de nodos del arbol:', cantidad[0]) print()
['C-3PO', 1.67, 72], ['Yoda', 0.9, 47], ['Boba Fett', 1.83, 78], ['Han Solo', 1.77, 83], ['Jabba el Hutt', 3.9, 531], ['Darth Maul', 1.78, 74]] # se guarda datos en archivo file = abrir('starwars') for dato in datos: x = Personaje(dato[0], dato[1], dato[2]) guardar(file, x) # a # se inserta nombres en arbol pos = 0 while pos < len(datos): personaje = leer(file, pos) arbol = insertar_nodo(arbol, personaje.nombre, pos) #print(personaje.nombre, 'anadido en la posicion:', pos) pos += 1 cerrar(file) print('Arbol de personajes:') por_nivel(arbol) print() # b print('1) Alta') print('2) Baja') print('3) Modificacion') print('4) Salir') control = input('Que desea hacer: ') print()
from TDA_ArbolBin_AVL import insertar_nodo, eliminar_nodo, busqueda_arbol, contar_ocurrencias, par_impar from TDA_ArbolBin_AVL import inorden, postorden, preorden, por_nivel from random import randint print('EJERCICIO 1') print() arbol = None for i in range(0, 10): arbol = insertar_nodo(arbol, randint(0, 100)) # a print('Barrido inorden') inorden(arbol) print() print('Barrido preorden') preorden(arbol) print() print('Barrido postorden') postorden(arbol) print() print('Barrido por nivel') por_nivel(arbol) print() # b buscado = int(input('Ingrese valor a buscar: ')) pos = busqueda_arbol(arbol, buscado) if pos is not None: print('El numero se encuentra en el arbol') else:
# guardar(file, l1) # l1 = Libro('012', 'pyhton', 'nuevo', 'uader', 1000) # guardar(file, l1) # l1 = Libro('033', 'datos', 'nuevo', 'uader', 890) # guardar(file, l1) arbol_isbn = None arbol_titulo = None arbol_autores = None # a b y c pos = 0 while pos < len(file): libro = leer(file, pos) print(libro.isbn, libro.cant, libro.titulo) arbol_isbn = insertar_nodo(arbol_isbn, libro.isbn, pos) arbol_titulo = insertar_nodo(arbol_titulo, libro.titulo, pos) for autor in libro.autores.split(';'): arbol_autores = insertar_nodo(arbol_autores, autor, pos) pos += 1 cerrar(file) print('') pos = busqueda_arbol(arbol_isbn, '567') if pos is not None: file = abrir('libros') libro = leer(file, pos.nrr) cerrar(file) print(libro.isbn, libro.cant, libro.titulo, libro.autores) print() file = abrir('libros')
arbol = None arbol_nombre = None arbol_numero = None arbol_tipo = None file = abrir('pokemon') for poke in datos: x = Pokemon(poke[0], poke[1], poke[2], poke[3]) guardar(file, x) # a pos = 0 while pos < len(datos): poke = leer(file, pos) arbol = insertar_nodo( arbol, [poke.nombre, poke.numero, poke.tipo, poke.debilidad], pos) arbol_nombre = insertar_nodo(arbol_nombre, poke.nombre, pos) arbol_numero = insertar_nodo(arbol_numero, poke.numero, pos) arbol_tipo = insertar_nodo(arbol_tipo, poke.tipo, pos) pos += 1 cerrar(file) # b busc = input('Busqueda por nombre del pokemon. Ingrese: ') busqueda_proximidad(arbol_nombre, busc) print() # c, d y e file = abrir('pokemon') print('Pokemones de tipo: agua, fuego, planta o electrico') inorden_tipo(arbol_nombre, file)
print('EJERCICIO 5') print() class Personaje(): def __init__(self, nombre): self.nombre = nombre arbol = None arbol_superheroes = None arbol_villanos = None # a arbol = insertar_nodo(arbol, 'Batman', True) arbol = insertar_nodo(arbol, 'Joker', False) arbol = insertar_nodo(arbol, 'Iron Man', True) arbol = insertar_nodo(arbol, 'Lex Luthor', False) arbol = insertar_nodo(arbol, 'Doctor Extraño', True) arbol = insertar_nodo(arbol, 'Magneto', False) arbol = insertar_nodo(arbol, 'Capitán America', True) arbol = insertar_nodo(arbol, 'Thanos', False) print('PERSONAJE | BANDO') inorden_marvel(arbol) print() # b print('Villanos listados alfabeticamente') inorden_villanos(arbol)