I’m trying to build a tree with data in the nodes as well as in the arcs. What I did so far is build classes Tree, Arc and Node like this:
class Tree():
def __init__(self):
self.root = None
self.nodes = {}
self.end_arcs = {}
class Arc():
def __init__(self,start,end,data={}):
self.start = start
self.end = end
self.data = data
class Node():
def __init__(self,token,data={}):
self.token = str(token)
self.state = self.OPEN
self.data = data
Nodes are numbered increasingly with the end node being labeled as -1. However, when I want to reverse the tree and renumber the nodes, I have to change a lot of things: the keys of my nodes dict, the label of every node and perhaps (or can they point to the nodes in my nodes dict?) also the nodes in my arcs dict. So I’m thinking that my way of implementing this common structure is not a good one. Is there a standard way of solving this problem? Or how would you build a tree structure like this?
Thanks in advance,
Joris
Extending my comment a bit, consider the following
“Renaming” the nodes is no problem because the arc keeps tracks of the node “by reference”, not by it’s ID. When we look at, for example, the arcs, using references allow us to not care about how the underlying node is changed.