我有一个树形结构,例如:
def __init__(self, parent=None, key=None, nodes=[]):
self.parent = parent # TrieNode value
self.key = key # char/string value
self.nodes = nodes # [TrieNode, TrieNode, ....]
现在,我想获得特定节点的孩子......
我想以递归的方式逐一放弃它们......
我的代码是
def mychildren(self):
for node in self.nodes:
yield node
node.mychildren()
是正确的吗??
当我将单元测试编写为
时rootnode = TrieNode(None, '')
anode = TrieNode(rootnode,'a')
bnode = TrieNode(rootnode,'b')
rootnode.setchildren([anode, bnode])
acnode = TrieNode(anode, 'c')
anode.setchildren([acnode])
for i in rootnode.mychildren():
print i.key
我发现我只能获得A和B节点。我无法获得作为A节点的子节点的acnode