我有一个作业,其中我必须完成给定的类框架。我卡在BSTNode类的函数删除中。有一个问题,因为它出于某种原因将子代复制到其他分支。不适用于此功能的代码以及该功能后的输入树和输出。
def _delete_two(node, parent):
"""delete a node with two subtrees. Return T|F succesfully deleted"""
result = False
succ = node.right
parent_succ = node
if succ.left is None:
node.data = succ.data
node.right = succ.right
result = True
return result
def _find_successor(node, parent):
"""return successor and parent of successor"""
succ = node.right
parent_succ = node
while succ is not None:
if succ.left is not None:
return _find_successor(succ.left,parent_succ)
else:
return succ, parent_succ
# we are always finding a successor ...
succ, parent_succ = _find_successor(node, parent)
node.data = succ.data
parent_succ.left = succ.right
BSTNode.delete(node,key)
result = True
return result
然后在此二叉树上使用该函数
----------------------------------------
Zack
Will
Sean
Rodd
Pete
Maze
Mary
Lisa
Leon
Lars
John
Dora
Chuc
Bert
Anna
我删除姓名:以('Lars','Maze','Chuc')中的姓名命名:
,输出为(不正确):
----------------------------------------
Zack
Will
Sean
Rodd
Pete
Will
Sean
Rodd
Mary
Lisa
Leon
Mary
Lisa
John
Dora
Bert
Anna
我认为存在一些递归问题,并且替换后还是不能移除继承者?