Created
June 13, 2018 21:56
-
-
Save Fhernd/d0e666b1eafba8823d0ccceb03a8bd88 to your computer and use it in GitHub Desktop.
Protocolo de iteración. OrtizOL.
This file contains 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
class Nodo: | |
def __init__(self, valor): | |
self.valor = valor | |
self._hijos = [] | |
def __repr__(self): | |
return 'Nodo({!r})'.format(self.valor) | |
def agregar_hijo(self, nodo): | |
self._hijos.append(nodo) | |
def __iter__(self): | |
return iter(self._hijos) | |
def profundidad_primero(self): | |
yield self | |
for c in self: | |
yield from c.profundidad_primero() | |
if __name__ == '__main__': | |
raiz = Nodo(0) | |
hijo1 = Nodo(1) | |
hijo2 = Nodo(2) | |
raiz.agregar_hijo(hijo1) | |
raiz.agregar_hijo(hijo2) | |
hijo1.agregar_hijo(Nodo(3)) | |
hijo1.agregar_hijo(Nodo(4)) | |
hijo2.agregar_hijo(Nodo(5)) | |
for hijo in raiz.profundidad_primero(): | |
print(hijo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment