In the following code, I understand that the print of tree
(named in the code) and parent
(named in the code) should not be the same. But I do not understand, why the tree
is always updating according to the updating of parent
?
(This question continues the last question here)
Code:
class node(object): def __init__(self, value): self.value = value self.children = [] def __repr__(self, level=0): ret = "t"*level+repr(self.value)+"n" for child in self.children: ret += child.__repr__(level+1) return ret def add(self, nod): self.children.append(nod) list_0 = ['AA', 'BB', 'CC', 'DD'] tree = parent = node(list_0[0]) i=1 while i<len(list_0): current_node = node(list_0[i]) parent.add(current_node) parent = current_node i+=1 print(tree) print(parent)
Output:
'AA' 'BB' 'CC' 'DD' 'DD'
In the code, it is obvious that the parent
is re-initialized every time in the loop, but according to my understanding, while the parent
is updating, the tree
should not be updated accordingly. Because once the parent
is re-initialized in the loop, it then does not have the same id(maybe address) with the tree
anymore, then the tree
will not be changed according to the parent
.
Question:
why the
tree
is still updating according to theparent
even theid(parent)
is changed every time in the loop?how the
tree
is linking to theparent
which is inside the loop? e.g.,node[1]
,node[2]
.(update)
Appreciate any comment or explanation, thanks in advance!
Advertisement
Answer
I think its because your tree is always pointing to the root node, but parent is pointing to the next node.
Let’s define some nomenclature:
- node[0]: First node created
- node[1]: Second node created
- ref(node[X]): Reference to the node X
- ref(node[0]) -> ref(node[1]): Reference to node 0 that has as children the node 1
Step 0
tree = parent = node(list_0[0]) tree is pointing to ref(node[0]) parent is pointing to ref(node[0])
Step 1 (inside the while loop)
parent = current_node tree still pointing to ref(node[0]) -> ref(node[1]) parent is pointing to ref(node[1])
Step n
tree still pointing to ref(node[0]) -> ref(node[1]) -> .... -> ref(node[n]) parent is pointing to ref(node[n])
A more graphic way to explain this:
# Step 0 tree, parent -> node[0] # Step 1 tree -> node[0] | parent -> node[1] # Step 2 tree -> node[0] | node[1] | parent -> node[2] # Step n tree -> node[0] | node[1] | ... | parent -> node[n]
A node is being added as child of another node in this line:
parent.add(current_node)
So tree is always pointing to the root element of the tree and parent is pointing to the last element of the tree. That’s the reason why the ids are different!
I hope I have provided you with useful information!