[Tutor] printing tree structure
karma
dorjetarap at googlemail.com
Fri Jul 3 10:58:52 CEST 2009
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: []
More information about the Tutor
mailing list