This code is meant to create a graph implementation. However, all of the nodes "self.children" link to the same list in memory, so adding children to any of them adds it to all of them. I can not for the life of me think of why this would happen, I have made classes like this many times, and not had such an issue.
I mean, just because you define a list in the default values doesn't mean I make a list then and there, does it? This is confusing...
class DescisionNode(object):
def __init__(self,data,score,childs=[]):
self.data = data
self.score = score
self.parent = None
self.children = childs
def getTop(self):
if self.parent == None:
return self
else:
return self.parent.getTop()
def getEnds(self):
out = []
if len(self.children)==0:
return [self]
else:
print(self,self.children)
for n in self.children:
out += n.getEnds()
return out
def add(self, newNode):
if newNode.parent == None:
newNode.parent = self
else:
raise Exception("Parent already exists.")
if newNode is self:
raise Exception("self may not be child")
self.children.append(newNode)