target = None
only rebinds the variable target
to a new value, None
. Whatever target
was bound to before doesn't change.
You'll have to track the parent node and set it's lChild
or rChild
attribute to None
instead.
def delete(node,key):
cNode = node
target = parent = None
while cNode:
if cNode.value == key:
target = cNode
break
elif cNode.value > key:
parent, cNode = cNode, cNode.lChild
elif cNode.value < key:
parent, cNode = cNode, cNode.rChild
if target:
if parent:
if parent.lChild is target:
parent.lChild = None
else:
parent.rChild = None
else:
# target is top-level node; perhaps return None in that case?
return node