Creating lists from subtrees
For an application I'm writing, I need to create flat lists from a tree
based structure which contain the items of the sublist.
For example, if I had this tree:
-a
|-b
|-c
|-d
I'd like to create lists from the left and right leaves recursively, so
for example:
[a][b,c,d]. [b][c,d], [c][d]
etc
So far I'm storing my tree as a tuple like so:
(1.0,(0.5,(0.25, (0.125,'d'),(0.125,'c')),(0.25,'b')),(0.5,'a'))
I have a function which prints the information on the leaves:
def printTree(tree, prefix = ''):
if len(tree) == 2:
print tree[1], prefix
else:
printTree(tree[1], prefix + '0')
printTree(tree[2], prefix + '1')
(This is a Huffman).
I've tried creating a function whoneich replaces the print statements with
list() statements, but that didn't work.
Does anyone have any ideas about how I could go about this?
No comments:
Post a Comment