Created
March 31, 2017 06:05
-
-
Save codigosdeprogra/12be086b79730718d8fe530d300983b7 to your computer and use it in GitHub Desktop.
Arbol binario de busqueda en Python 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Autor: Luis García | |
| #Carrera: Ingeniería en Computación | |
| import os | |
| class node(): | |
| def __init__(self, dato): | |
| self.left = None | |
| self.right = None | |
| self.dato = dato | |
| class arbol(): | |
| def __init__(self): | |
| self.root = None | |
| def insert(self, a, dato): | |
| if a == None: | |
| a = node(dato) | |
| else: | |
| d = a.dato | |
| if dato < d: | |
| a.left = self.insert(a.left, dato) | |
| else: | |
| a.right = self.insert(a.right, dato) | |
| return a | |
| def inorder(self, a): | |
| if a == None: | |
| return None | |
| else: | |
| self.inorder(a.left) | |
| print(a.dato) | |
| self.inorder(a.right) | |
| def preorder(self, a): | |
| if a == None: | |
| return None | |
| else: | |
| print(a.dato) | |
| self.preorder(a.left) | |
| self.preorder(a.right) | |
| def postorder(self, a): | |
| if a == None: | |
| return None | |
| else: | |
| self.postorder(a.left) | |
| self.postorder(a.right) | |
| print(a.dato) | |
| def buscar(self, dato, a): | |
| if a == None: | |
| return None | |
| else: | |
| if dato == a.dato: | |
| return a.dato | |
| else: | |
| if dato < a.dato: | |
| return self.buscar(dato, a.left) | |
| else: | |
| return self.buscar(dato, a.right) | |
| tree = arbol() | |
| while True: | |
| os.system("cls") | |
| print("Arbol ABB") | |
| opc = input("\n1.-Insertar nodo \n2.-Inorden \n3.-Preorden \n4.-Postorden \n5.-Buscar \n6.-Salir \n\nElige una opcion -> ") | |
| if opc == '1': | |
| nodo = input("\nIngresa el nodo -> ") | |
| if nodo.isdigit(): | |
| nodo = int(nodo) | |
| tree.root = tree.insert(tree.root, nodo) | |
| else: | |
| print("\nIngresa solo digitos...") | |
| elif opc == '2': | |
| if tree.root == None: | |
| print("Vacio") | |
| else: | |
| tree.inorder(tree.root) | |
| elif opc == '3': | |
| if tree.root == None: | |
| print("Vacio") | |
| else: | |
| tree.preorder(tree.root) | |
| elif opc == '4': | |
| if tree.root == None: | |
| print("Vacio") | |
| else: | |
| tree.postorder(tree.root) | |
| elif opc == '5': | |
| nodo = input("\nIngresa el nodo a buscar -> ") | |
| if nodo.isdigit(): | |
| nodo = int(nodo) | |
| if tree.buscar(nodo, tree.root) == None: | |
| print("\nNodo no encontrado...") | |
| else: | |
| print("\nNodo encontrado -> ",tree.buscar(nodo, tree.root), " si existe...") | |
| else: | |
| print("\nIngresa solo digitos...") | |
| elif opc == '6': | |
| print("\nElegiste salir...\n") | |
| os.system("pause") | |
| break | |
| else: | |
| print("\nElige una opcion correcta...") | |
| print() | |
| os.system("pause") | |
| print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excelente representacion de un arbol binario