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

1

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.