I am trying to figure out the best way of coding a Node Class (to be used in a binary tree) that would contain the attributes key, left and right.
I thought I would do something like
class Node:
def __init__(self, key):
self.key= key
self.left = None
self.right = None
and then do
a = Node('A')
b = Node('B')
c = Node('C')
a.left = b
a.right = c
Here I am a little bit confused: are (under the hood) left and right pointers? Or is a containing a copy of the whole tree?
If I add
d = Node('B') # same key as before, but an entirely different node
c.right = d
then are b and d two different objects even if they have the same attributes? I would think so because they don't share any memory.
Finally, if I want to do a deep copy of one of my nodes, is
e = Node(a.key))
sufficient?
childrenthat is a list of nodes, which should't change the essence of my question, I believe.a.left = bandb.right = acreating a loop which is not allowed in a tree.