I created a small function funct
which should assign None
to node if a value of -1 is passed else assign the value to the value attribute of the node object. I created a simple binary tree [root, left, right] and finally print the nodes.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def funct(node, val):
if val == -1:
### modify code here
node = None
else:
node.val = val
root = TreeNode()
root.val = 'root'
root.left = TreeNode()
root.right = TreeNode()
funct(root.left, 'left')
funct(root.left, -1)
print(root, root.val)
print(root.left, root.left.val)
print(root.right, root.right.val)
When I print the nodes I see the following output.
The right node is in memory and is not None
.
How do I assign None
to the orignal object by modifying the code inside the if
in funct
to get the following output instead.
esentially simulating the following code and getting the output :
root = TreeNode()
root.val = 'root'
root.left = TreeNode('left')
root.right = None
Note : I can change my algo. to create a new node only when val != -1. However I want to understand if there is a way to modify a passed object it in pyhton.
Edits : removed the word "delete". Added some clarification.
CodePudding user response:
I created a small function funct which should assign None to node if a value of -1 is passed else assign the value to the value attribute of the node object.
funct
cannot replace the node that was passed to it (i.e.: make one of the caller's variables refer to a new node instead of this one), because it receives the node, and not any variable name for that node.
It can modify the node that was passed to it. In particular, it can replace the child values, by reassigning to node.right
and node.left
. This means "the .right
and .left
of the TreeNode
instance that was passed to me". node
is a name that funct
uses for that passed-in instance, that has nothing to do with the calling code in any way.
To "remove" a subtree, then, we need to pass the parent of the tree that will be removed, and also indicate which side will be removed. That might look like:
def remove_child(parent, which):
if which == 'left':
parent.left = None
elif which == 'right':
parent.right = None
else:
raise ValueError("invalid child name")
The root of the tree has no parent, so removing the entire tree must be treated as a special case.
CodePudding user response:
Not sure if this is what you need, but I think its doing the behavior you want:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def funct(self, child: str, val):
if val == -1:
### modify code here
setattr(self,child, None)
else:
setattr(self,child,TreeNode(val))
root = TreeNode()
root.val = 'root'
root.left = TreeNode()
root.right = TreeNode()
root.funct(child='left', val='left')
root.funct(child='right', val='right')
print(root, root.val)
print(root.left, root.left.val)
print(root.right, root.right.val)
print(None)
root.funct(child='left', val= -1)
print("\n")
print(root, root.val)
# print(root.left, root.left.val) - you cannot print root.left.val because None has no attribute
print(root.left)
print(root.right, root.right.val)