I am developing a class for creating a Tree object. For some reason when I am establishing parent child relationship between the root and the children nodes, all the nodes are getting their children attributes updated. I am not sure what is causing this. I cannot find any bug in my code. Any guidance and pointers or explanation of the internal workings of my code will be much appreciated.
class TreeNode():
def __init__(self, val, children=[], parent=None):
self.val = val
self.children = children
self.parent = parent
def __str__(self) -> str:
return f"{self.val} with children {[child.val for child in self.children]} with parent {None if self.parent==None else self.parent.val}"
def add_children(self, children):
for child in children:
assert isinstance(child, TreeNode)
self.children.append(child)
child.parent = self
# create nodes
root = TreeNode(0)
node1 = TreeNode(1)
node2 = TreeNode(2)
node3 = TreeNode(3)
node4 = TreeNode(4)
node5 = TreeNode(5)
node7 = TreeNode(7)
node6 = TreeNode(6)
print('node4 children', node4.children) # PRINTS: [] # as expected
# add children node1 and node2 to root
root.add_children([node1, node2])
# When I am adding nodes it is adding the same children to all the nodes and updating their children attribute
print('root', root) # PRINTS: root 0 with children [1, 2] with parent None
print('node4', node4) # PRINTS: node4 4 with children [1, 2] with parent None # How does node4 have children!