0

What is the best way to implement a tree structure (generic - not binary) in python? My intuition would have the following skeleton:

class TNode(self, data):
   #enter things for each individual node

class TStructure(self):
   #enter code for implementing nodes that reference each other.
1
  • 1
    if this is homework you should mark it as such. Commented Oct 11, 2011 at 1:22

2 Answers 2

1

Why not just have a class for nodes, that has a list of children nodes?

Edit to add skeleton:

class TreeNode(object):
    def __init__(self, data, children=[]):
        self.data = data
        self.children = list(children)

    def add(self, child):
        self.children.append(child)
    ...

There's really not much to it. Every TreeNode contains a set of children (leaf nodes just have 0 children, which is about as close to a pure code implementation of the very definition of a tree leaf node as you can get). You could add methods to manipulate the order of children, but if you need that you're possibly better off just considering the children list exposed and using the list methods directly. You could add methods like search, but for a generic tree without known ordering constraints (like in a binary search tree, where the contents of one subtree are less than the contents of the other subtree) there's not a whole lot for it to do. You could add generator methods for traversal (with several possible traversal strategies).

If you only want the leaf nodes to have data, then you have a separate class for internal nodes and leaf nodes, where the internal nodes have children and the leaf nodes have data.

Sign up to request clarification or add additional context in comments.

5 Comments

@cfarm54: you create a node object to represent the tree's root node, and populate the tree by adding child node objects to the root node's list. Of course each child node object also contains a list, so you can continue expanding the tree. A node with an empty list would be a leaf node.
@cfarm64: Of course you might still want a Tree class with implementations of algorithms for searching, adding, and removing from the tree.
@robjb Or you could just put that on the node class, since each node is the root of a subtree.
@cfarm54 Skeleton added. Nick Johnson is exactly right wrt robjb's comment; there's very little point having a separate Tree class since there's nothing more to a Tree than the root node, so it's much more convenient to have all the nodes be trees.
I see so you would have nodes in each of the lists and those nodes would lead to other lists of nodes.
1

look into networkx and the Graph object.

see here

If this is for homework and you have to implement from scratch I'll give you the hint that you probably want a class for nodes and a class for edges...

EDIT:

You don't really need a structure class if you have a node class and an edge class something traversing the graph is just a matter of search algorithms once it is implemented.

class Node:
    name=str()

class Edge:
    a = Node()
    b = Node()

and then string them together with __init__ functions for one of the two classes depending on how you will be loading in data.

2 Comments

it's not for homework, but it's interesting you mention the class for edges. So are you suggesting that I have 3 classes, one for edges, one for nodes, then one for the structure?
despite my edit I would say that if this isn't for homework why reinvent the world... networkx is actually pretty bad ass for python data structures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.