2

I have a tree structure such as:

def __init__(self, parent=None, key=None, nodes=[]):
    self.parent = parent    # TrieNode value
    self.key = key          # char/string value
    self.nodes = nodes      # [TrieNode, TrieNode, ....]

Now, I want to get the specific node's children...

I want to yield them one by one in a recursive way...

my code is

def mychildren(self):
    for node in self.nodes:
        yield node
        node.mychildren()

is that correct .... ?

when i write the unit test as

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

I found I could only get A and B nodes. I could NOT get the acnode which is child node of A node

allencharp
  • 1,101
  • 3
  • 14
  • 31
  • Watch out for that [mutable default argument](http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments). – user2357112 Apr 30 '16 at 03:59

0 Answers0