Re: [Tutor] printing tree structure
karma wrote: Hi all , I have a nested list in the structure [root,[leftSubtree],[RightSubtree]] that I want to print out. I was thinking that a recursive solution would work here, but so far I can't quite get it working. This is what I have so far: Can someone suggest whether this is suited to a recursive solution and if so, what am I doing wrong. Thanks L = ['a', ['b', ['d', [], []], ['e', [], []]], ['c', ['f', [], []], []]] def printTree(L): for i in L: if isinstance(i,str): print 'Root: ', i else: print '--Subtree: ', i printTree(i) printTree(L) Root: a --Subtree: ['b', ['d', [], []], ['e', [], []]] Root: b --Subtree: ['d', [], []] Root: d --Subtree: [] --Subtree: [] --Subtree: ['e', [], []] # this shouldn't be here Root: e --Subtree: [] --Subtree: [] --Subtree: ['c', ['f', [], []], []] Root: c --Subtree: ['f', [], []] Root: f --Subtree: [] --Subtree: [] --Subtree: [] Using tabs in Python source code is never a good idea, and mixing tabs and spaces is likely to cause real problems, sometimes hard to debug. That's not the problem here, but I wanted to mention it anyway. The problem you have is the hierarchy doesn't show in the printout. There are a few ways to indicate it, but the simplest is indentation, the same as for Python source code. Once things are indented, you'll see that the line you commented as "this shouldn't be here" is correct after all. Add an additional parameter to the function to indicate level of indenting. Simplest way is to simply pass a string that's either 0 spaces, 4 spaces, etc. L = ['a', ['b', ['d', [], []], ['e', [], []]], ['c', ['f', [], []], []]] def printTree(L, indent=""): for i in L: if isinstance(i,str): print indent, 'Root: ', i else: print indent, '--Subtree: ', i printTree(i, indent+"") printTree(L) Root: a --Subtree: ['b', ['d', [], []], ['e', [], []]] Root: b --Subtree: ['d', [], []] Root: d --Subtree: [] --Subtree: [] --Subtree: ['e', [], []] Root: e --Subtree: [] --Subtree: [] --Subtree: ['c', ['f', [], []], []] Root: c --Subtree: ['f', [], []] Root: f --Subtree: [] --Subtree: [] --Subtree: [] Now I don't know if that's what your list of lists was supposed to mean, but at least it shows the structure of your printTree() loop. DaveA ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing tree structure
Thanks all for the feedback. As you all rightly pointed out, I was confusing myself by not keeping a track of the recursion depth. Thanks again 2009/7/3 Alan Gauld : > > "karma" wrote > >> thinking that a recursive solution would work here, but so far I can't >> quite get it working. This is what I have so far: >> >> Can someone suggest whether this is suited to a recursive solution and > > Yes certainly > >> if so, what am I doing wrong. > > L = ['a', > > ['b', ['d', [], [] ], > ['e', [], [] ] > ], ['c', > ['f', [], [] ], [] > ] > ] > > def printTree(L): >> >> for i in L: >> if isinstance(i,str): >> print 'Root: ', i >> else: >> print '--Subtree: ', i >> printTree(i) >> > > printTree(L) >> >> Root: a >> --Subtree: ['b', ['d', [], []], ['e', [], []]] >> Root: b >> --Subtree: ['d', [], []] >> Root: d >> --Subtree: [] >> --Subtree: [] > > This is the end of the recursive call to printTree( ['d', [ [], [] ] ] > >> --Subtree: ['e', [], []] # this shouldn't be here > > This is the next element in printTree( ['d', [], []], ['e', [], []] ) > >> Root: e > > This is the start of printTree( ['e', [], [] ] ) > >> --Subtree: [] >> --Subtree: [] > > Now why do you think the line you highlighted is an error? > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing tree structure
"karma" wrote thinking that a recursive solution would work here, but so far I can't quite get it working. This is what I have so far: Can someone suggest whether this is suited to a recursive solution and Yes certainly if so, what am I doing wrong. L = ['a', ['b', ['d', [], [] ], ['e', [], [] ] ], ['c', ['f', [], [] ], [] ] ] def printTree(L): for i in L: if isinstance(i,str): print 'Root: ', i else: print '--Subtree: ', i printTree(i) printTree(L) Root: a --Subtree: ['b', ['d', [], []], ['e', [], []]] Root: b --Subtree: ['d', [], []] Root: d --Subtree: [] --Subtree: [] This is the end of the recursive call to printTree( ['d', [ [], [] ] ] --Subtree: ['e', [], []] # this shouldn't be here This is the next element in printTree( ['d', [], []], ['e', [], []] ) Root: e This is the start of printTree( ['e', [], [] ] ) --Subtree: [] --Subtree: [] Now why do you think the line you highlighted is an error? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing tree structure
karma wrote: > Hi all , > > I have a nested list in the structure > [root,[leftSubtree],[RightSubtree]] that I want to print out. I was > thinking that a recursive solution would work here, but so far I can't > quite get it working. This is what I have so far: > > Can someone suggest whether this is suited to a recursive solution and > if so, what am I doing wrong. > > Thanks > L = ['a', ['b', ['d', [], []], ['e', [], []]], ['c', ['f', [], []], []]] def printTree(L): > for i in L: >if isinstance(i,str): > print 'Root: ', i > else: > print '--Subtree: ', i > printTree(i) > > printTree(L) > Root: a > --Subtree: ['b', ['d', [], []], ['e', [], []]] > Root: b > --Subtree: ['d', [], []] > Root: d > --Subtree: [] > --Subtree: [] > --Subtree: ['e', [], []] # this shouldn't be here Why shouldn't it be there? Look more closely: Root: b --Subtree: ['d', [], []] ... --Subtree: ['e', [], []] ... The program recurse correctly, however the way you're printing it is misleading. When printing trees with arbitrary depth, you usually want to keep track of the recursion depth. Something like this: def print_tree(tree, depth=0): ... print_tree(node, depth + 1) ... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor