Delete A Root From A Tree In Python
I am implementing a deletion of a node from a binary search tree in python. I got stuck in an edge case. Consider next code: class Node(): def __init__(self, value, left=None,
Solution 1:
There is indeed no way to replace root
and have it replaced wherever the instance pointed to by the name root
appears.
The only way to achieve what you want is to mutate root
to duplicate the child node.
defdelete(node, inheritLeft=True):
child = node.left if inheritLeft else node.right
node.value = child.value
node.left = child.left
node.right = child.right
(Obviously you might want to do something smarter regarding choosing which node to inherit).
Post a Comment for "Delete A Root From A Tree In Python"