Data structures for tree traversals?

Miki Tebeka miki.tebeka at zoran.com
Sun Nov 14 09:47:19 EST 2004


Hello Carl,

> What are the best options for implementing fast and efficient trees in
> Python?
The answer varies depending on what will the trees be used for.

> Intuitively, dictionaries seem to be ideal candidates for accessing objects
> at nodes and traversing the tree. Am I right or are there better choices?
> Are lists better? Has Numerical Python speed-efficient methods that are to
> be preferred in comparison with lists ad dictionaries?
There's http://www.python.org/doc/essays/graphs.html by the BDFL.

I find that defining a little class:
class Tree:
    def __init__(self, value, children=None):
        self.value = value
        if children is None:
            self.children = []
        else:
            self.children = children

and subclassing each specific child.
Combined with a visitor pattern is usually the best way.

Go for the usual approach:
o Make it work
o Make it right
o Make it fast (only if you need)

HTH.

Bye.
--
------------------------------------------------------------------------
Miki Tebeka <miki.tebeka at gmail.com>
http://tebeka.spymac.net
The only difference between children and adults is the price of the toys



More information about the Python-list mailing list