您可以使用recursion
,也可以使用iteration
. 无论哪种方式都无所谓。但是您需要一种搜索树的策略。
以下是一些通过图表的策略:
主要思想是不要两次通过同一个节点/叶子,这对于树来说是微不足道的,但coloring
对于图来说却需要:
您可以使用一些设计模式,例如visitor
模式,您可以添加一个方法.visit()
来TreeHierarchyClass
访问其子节点,并添加一个方法来按名称查找节点。
例子:
# imagine we got this class
class TreeHierarchyClass(object):
def __init__(self, value):
self.children = []
self.value = value
if self.value == 13:
self.name = 'the lucky one.'
def add(self, value):
self.children.append(type(self)(value))
您可以通过以下方式访问所有节点:
def visit(tree):
visited = set()
nonvisited = set()
nonvisited.update(tree.children)
while nonvisited:
item = nonvisited.pop()
# already seen
if item in visited:
continue
# mark item
visited.add(item)
yield item
# add children
nonvisited.update(item.children)
让我们构建一个示例树结构:
root = TreeHierarchyClass(0)
for i in range(10):
root.add(i)
for i in range(10):
root.children[1].add(i + 10)
现在让我们找到一些项目:
def find(name):
for item in visit(root):
print 'checking item with value %d' % item.value,
if getattr(item, 'name', None) == name:
print '- found it.'
break
else:
print '- nope, keep searching.'
else:
print 'Sorry, not found.'
find('the lucky one.')
find('the lost one.')
这个例子将打印:
>>> find('the lucky one.')
checking item with value 7 - nope, keep searching.
checking item with value 0 - nope, keep searching.
checking item with value 1 - nope, keep searching.
checking item with value 12 - nope, keep searching.
checking item with value 2 - nope, keep searching.
checking item with value 9 - nope, keep searching.
checking item with value 19 - nope, keep searching.
checking item with value 3 - nope, keep searching.
checking item with value 11 - nope, keep searching.
checking item with value 4 - nope, keep searching.
checking item with value 14 - nope, keep searching.
checking item with value 5 - nope, keep searching.
checking item with value 6 - nope, keep searching.
checking item with value 15 - nope, keep searching.
checking item with value 8 - nope, keep searching.
checking item with value 16 - nope, keep searching.
checking item with value 13 - found it.
>>> find('the lost one.')
checking item with value 7 - nope, keep searching.
checking item with value 0 - nope, keep searching.
checking item with value 1 - nope, keep searching.
checking item with value 12 - nope, keep searching.
checking item with value 2 - nope, keep searching.
checking item with value 9 - nope, keep searching.
checking item with value 19 - nope, keep searching.
checking item with value 3 - nope, keep searching.
checking item with value 11 - nope, keep searching.
checking item with value 4 - nope, keep searching.
checking item with value 14 - nope, keep searching.
checking item with value 5 - nope, keep searching.
checking item with value 6 - nope, keep searching.
checking item with value 15 - nope, keep searching.
checking item with value 8 - nope, keep searching.
checking item with value 16 - nope, keep searching.
checking item with value 13 - nope, keep searching.
checking item with value 17 - nope, keep searching.
checking item with value 10 - nope, keep searching.
checking item with value 18 - nope, keep searching.
Sorry, not found.