tags:

views:

214

answers:

5

I am looking for a good Tree data structure class. I have come across this package, but since I am relatively new to Python (not programming), I dont know if there are any better ones out there.

I'd like to hear from the Pythonistas on here - do you have a favorite tree script that you regularly use and would recommend?

[Edit]

To clarify, by 'Tree', I mean a simple unordered tree (Hmm, thats a bit of a recursive definition - but hopefully, that clarifies things somewhat). Regarding what I need the tree for (i.e. use case). I am reading tree data from a flat file and I need to build a tree from the data and traverse all nodes in the tree.

A: 

It might be worth writing your own tree wrapper based on an acyclic directed graph using the networkx library.

Andrew Walker
+2  A: 

Roll you own. For example, just model you tree as list of list. You should detail your specific need before people can provide better recommendation.

Wai Yip Tung
+2  A: 

For a tree with ordered children, I'd usually do something kind of like this (though a little less generic, tailored to what I'm doing):

class TreeNode(list):

    def __init__(self, iterable=(), **attributes):
        self.attr = attributes
        list.__init__(self, iterable)

    def __repr__(self):
        return '%s(%s, %r)' % (type(self).__name__, list.__repr__(self),
            self.attr)

You could do something comparable with a dict or using DictMixin or it's more modern descendants if you want unordered children accessed by key.

Matt Anderson
A: 

Would BTrees help? They're part of the Zope Object Database code. Downloading the whole ZODB package is a bit of overkill, but I hope the BTrees module would be at least somewhat separable.

Jenn D.
A: 

Apart from "unordered" (which presumably means that you don't need to do an in-order traversal, and cuts out B-trees etc etc), you haven't told us much. What "pointers" (references to other nodes) does each node need? What attributes does each node need?

John Machin