除了删除操作之外,我几乎完成了Python中的二进制搜索树实现。
到目前为止,在我迄今为止测试的函数的所有用例中: 1.叶节点被正确删除 2.正确删除有两个孩子的节点 3.正确删除根节点
但是我无法删除左子项或右子项的节点。我的Eclipse IDE向我展示了一些语句没有效果(下图中标有黄色的语句),所以我在我的iPython Notebook本地服务器上尝试了该程序,但结果似乎是一样的。 / p>
我在python程序中错过了什么?请帮帮我。这是我的代码:
prevnode = None
class Node:
def __init__(self,data):
self.data = data
self.left = None
self.right = None
def insert(node,info):
if node.data is None:
node.data = info
elif info < node.data:
if node.left is None:
node.left = Node(None)
insert(node.left,info)
elif info >= node.data:
if node.right is None:
node.right = Node(None)
insert(node.right,info)
def search(node,info):
if node is None:
print "Node with the mentioned data is not found"
if node.data == info:
print "Found the node containing value held by info"
elif info < node.data and node.left is not None:
search(node.left,info)
elif info > node.data and node.right is not None:
search(node.right,info)
def delete(node,info):
global prevnode
if info == node.data:
print "This is the place where info is stored"
if node.left is None and node.right is None:
if prevnode.left is not None and prevnode.left.data == node.data:
prevnode.left = None
del node
elif prevnode.right is not None and prevnode.right.data == node.data:
prevnode.right = None
del node
return
elif node.left is not None and node.right is None:
if prevnode.left is not None and prevnode.left.data == node.data:
prevnode.left == node.left
del node
elif prevnode.right is not None and prevnode.right.data == node.data:
prevnode.right == node.left
del node
return
elif node.left is None and node.right is not None:
if prevnode.left is not None and prevnode.left.data == node.data:
prevnode.left == node.right
del node
elif prevnode.right is not None and prevnode.right.data == node.data:
prevnode.right == node.right
del node
return
elif node.left is not None and node.right is not None:
node.data = None
prevnode = node.right
successor = prevnode.left
if successor is None:
node.data = prevnode.data
node.right = prevnode.right
elif successor is not None:
while successor.left is not None:
prevnode = successor
successor = prevnode.left
if successor.right is None:
node.data = successor.data
prevnode.left = None
del successor
elif successor.right is not None:
prevnode.left = successor.right
node.data = successor.data
successor.right = None
del successor
elif info < node.data:
print "We will have to go to the left child of the current node"
prevnode = node
print "parent of the left child will be prevnode : ",prevnode.data
delete(node.left,info)
elif info >= node.data:
print "We will have to go to the right child of the current node"
prevnode = node
print "parent of the right child will be prevnode : ",prevnode.data
delete(node.right,info)
def display(node,parent):
if node is not None:
print "value at this node is ",node.data
if parent is None:
print "it is the root node"
else:
print "the parent is ",parent.data
if node.left is not None:
display(node.left,node)
if node.right is not None:
display(node.right,node)
else:
return
BST = Node(None)
while True:
choice = int(raw_input("Please enter your choice 1.Insert, 2.Search, 3.Delete, 4.Display, 5.Exit"))
if choice == 1:
num = int(raw_input("Please enter the number you wish to insert"))
insert(BST,num)
elif choice == 2:
num = int(raw_input("Please enter the number you wish to find"))
search(BST,num)
elif choice == 3:
num = int(raw_input("Please enter the number you wish to delete"))
delete(BST,num)
elif choice == 4:
print "Displaying the Tree"
display(BST,None)
elif choice == 5:
break
else:
print "Please enter the correct input"
P.S:如果有任何错误的缩进(我很确定没有),它们可以正确缩进
答案 0 :(得分:2)
有一个简单的拼写错误。您正在使用==而不是=。
所以不要这样:
elif node.left is not None and node.right is None:
if prevnode.left is not None and prevnode.left.data == node.data:
prevnode.left == node.left
del node
elif prevnode.right is not None and prevnode.right.data == node.data:
prevnode.right == node.left
del node
return
elif node.left is None and node.right is not None:
if prevnode.left is not None and prevnode.left.data == node.data:
prevnode.left == node.right
del node
elif prevnode.right is not None and prevnode.right.data == node.data:
prevnode.right == node.right
del node
return
你想这样做:
elif node.left is not None and node.right is None:
if prevnode.left is not None and prevnode.left.data == node.data:
prevnode.left = node.left # Changed == to =
del node
elif prevnode.right is not None and prevnode.right.data == node.data:
prevnode.right = node.left # Changed == to =
del node
return
elif node.left is None and node.right is not None:
if prevnode.left is not None and prevnode.left.data == node.data:
prevnode.left = node.right # Changed == to =
del node
elif prevnode.right is not None and prevnode.right.data == node.data:
prevnode.right = node.right # Changed == to =
del node
return