begging for a tree implementation
Ant
antroy at gmail.com
Thu Apr 27 04:38:30 EDT 2006
I was looking for a tree implementation a while back, but got no real
pointers. Seems that there are no tree data model modules out there -
which surprises me, since it is such a fundamental data structure.
I ended up using a very basic tree data class - I didn't need all of
the methods you mentioned. Here it is in case that helps at all:
class Node (object):
def __init__(self, data, parent=None):
self.data = data
self.parent = parent
self.children = []
def add_child(self, child):
self.children.append(child)
child.parent = self
Some of those methods you mentioned would be trivial to implement:
def is_root(self):
return not self.parent
def is_leaf(self):
return not self.children
It may also be more pythonic to have 'walk' methods for traversing
trees.
I may start a tree data structure project - it's something I've been
thinking about for a while, and now it's clear that it's not just me
who uses trees as data structures!
More information about the Python-list
mailing list