tree data structure

Tom Good Tom_Good1 at
Mon Sep 9 20:07:00 EDT 2002

Padraig Brady <Padraig at> wrote in message news:<Ti1e9.2864$cP3.6124 at>...

> > {'S2': ['S0', 'S1', 'S3'], 'S1': ['Q2'], 'S0': ['Q1'], 'L0': ['S2']}
> Interesting I didn't know about setdefault.
> However how would you iterate over the above?

from __future__ import generators

def treeIterator(tree):
    for key in tree.keys():
        yield key
        for val in tree[key]:
            if not tree.has_key(val):
                yield val

if __name__ == '__main__':
    tree = {'S2': ['S0', 'S1', 'S3'], 'S1': ['Q2'], 'S0': ['Q1'], 'L0': ['S2']}
    for node in treeIterator(tree):
        print node

More information about the Python-list mailing list