tree data structure
Padraig Brady
Padraig at Linux.ie
Mon Sep 9 06:13:05 EDT 2002
Padraig Brady wrote:
> Raymond Hettinger wrote:
>
>> There are a number of ways to represent trees:
>> -- instances of a treenode class
>> -- a list of lists: [l0, [s2, [s0, [q1]], [s1,[q2]], [s3]]]
>> -- or, my favorite, a dictionary of lists:
>>
>> original = [('L0','S2'), ('S2','S0'), ('S2','S1'), ('S1','Q2'),
>> ('S0','Q1'), ('S2','S3')]
>>
>> tree = {}
>> for key, value in original:
>> tree.setdefault(key,[]).append(value)
>> tree
>> {'S2': ['S0', 'S1', 'S3'], 'S1': ['Q2'], 'S0': ['Q1'], 'L0': ['S2']}
>
> How would you iterate over the above?
To complete this thread, Guido wrote an essay
on how to manipulate graphs like that generated above:
http://www.python.org/doc/essays/graphs.html
Pádraig.
More information about the Python-list
mailing list