python遍历结构_在python中循环遍历树形结构?

您可以使用recursion或者您可以使用iteration。无论哪种方式都没有关系。但是你需要一个搜索树的策略。

这里有一些strategry要经过的曲线图:

的主要思想是向不通过相同的节点/叶两次,这是微不足道的用于去树木,但需要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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值