Skip to content

Instantly share code, notes, and snippets.

@renzon
Created May 25, 2017 20:21
Show Gist options
  • Save renzon/c696679a50713cc35c083baaa7204068 to your computer and use it in GitHub Desktop.
Save renzon/c696679a50713cc35c083baaa7204068 to your computer and use it in GitHub Desktop.
class Arvore:
def __init__(self, valor, *filhos):
"""Inicializador de árvore
:param valor: object valor que o nó possui
:param filhos: list de filhos desse nó
"""
self.valor = valor
self.filhos = filhos
def __str__(self) -> str:
return f'Árvore com valor {self.valor}'
def __iter__(self):
yield self.valor
for filho in self.filhos:
yield from filho
def tree(self, profundidade=0):
if profundidade:
print('└'+'─' * profundidade, self.valor)
else:
print(self.valor)
for filho in self.filhos:
filho.tree(profundidade + 2)
if __name__ == '__main__':
a1211 = Arvore('1.2.1.1')
a121 = Arvore('1.2.1', a1211)
a13 = Arvore('1.3')
a12 = Arvore('1.2', a121, a13)
a11 = Arvore('1.1', a12)
a31 = Arvore('3.1')
a4 = Arvore('4')
a3 = Arvore('3', a31, a4)
a2 = Arvore('2', a3)
a1 = Arvore('1', a11, a2)
print('Busca em profundidade:')
for v in a1:
print(v)
print('Fazendo logica parecida com comando tree do linux')
print(a1.tree())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment