python 深度遍歷
阿新 • • 發佈:2019-01-04
class Node:
def __init__(self, value):
self._value = value
self._children = []
def __repr__(self):
return 'Node(!{r})'.format(r=self._value)
def add_child(self, node):
self._children.append(node)
def __iter__(self):
# print("iteration")
return iter(self._children)
def depth_first(self):
yield self
for c in self:
yield from c.depth_first()
if __name__ == '__main__':
root = Node(0)
child1 = Node(1)
child2 = Node(2)
root.add_child(child1)
root.add_child(child2)
child1.add_child(Node(1.1 ))
child1.add_child(Node(1.2))
child2.add_child(Node(2.1))
for ch in root.depth_first():
print(ch)
發現一個python深度遍歷的巧妙寫法