在python中使用del语句后无法删除对象 [英] Unable to remove object after using del statement in python
问题描述
class BinaryNode:
$我正在构建二进制搜索树的功能。到目前为止,在编写删除函数时,我处理了要从树中删除的根是左节点(没有任何子节点的节点)的情况。为此,我只需要说del root。这对树完全没有影响,并且值9.5仍然存在。
def __init __(self,value):
self.data =值
self.left =无
self .right = None
def contains(root,value):
如果root为None:
返回False
如果value == root.data :
返回true
如果值< root.data:
return contains(root.left,value)
else:
return contains(root.right,value)
def插入(root,value):
如果root为None:
root = BinaryNode(value)
else:
如果value> root.data:
如果root.right为None:
root.right = BinaryNode(value)
else:
return insert(root.right,value)
否则:
如果root.left为None:
root.left = BinaryNode(value)
否则:
return insert(root.left,value)
def getMin(root):
如果root.left为None:
返回根
返回getMin(root.left)
def remove(root,value ):如果root为None,则
:
返回False
elif值< root.data:
remove(root.left,value)
elif value> root.data:
remove(root.right,value)
else:
如果root.left为None且root.right为None:
del root
def顺序(root):
如果root不存在:
顺序(root.left)
print(root.data)
inorder(root.right)
b = BinaryNode(10)
insert(b,9)
insert(b,11)
insert( b,8)
插入(b,9.5)
删除(b,9.5)
有序(b)
解决方案
del
仅删除引用,此处为本地名称root
。它不会删除您顶部的b
引用。您无法使用函数来完成您想做的事情;函数不拥有调用方中的引用。
最多可以将
root
值包装在单独的BinaryTree
类实例,可以为空。为它提供root
属性,如果该属性设置为None
,则清除树。class BinaryNode: def __init__(self, value): self.data = value self.left = None self.right = None def contains(root, value): if root is None: return False if value == root.data: return True if value < root.data: return contains(root.left, value) else: return contains(root.right, value) def insert(root, value): if root is None: root = BinaryNode(value) else: if value > root.data: if root.right is None: root.right = BinaryNode(value) else: return insert(root.right, value) else: if root.left is None: root.left = BinaryNode(value) else: return insert(root.left, value) def getMin(root): if root.left is None: return root return getMin(root.left) def remove(root, value): if root is None: return False elif value < root.data: remove(root.left, value) elif value > root.data: remove(root.right, value) else: if root.left is None and root.right is None: del root def inorder(root): if root is not None: inorder(root.left) print(root.data) inorder(root.right) b = BinaryNode(10) insert(b, 9) insert(b, 11) insert(b,8) insert(b,9.5) remove(b, 9.5) inorder(b)
i'm constructing the functions of a binary search tree. So far, in writing my remove function I handle the case in which the root to be removed from the tree is a left node (node without any children). For this, all I would have to do say del root. This has no effect at all on the tree and the value 9.5 still exists.
解决方案
del
only removes the reference, here the local nameroot
. It won't delete theb
reference you have at the top. You can't do what you want with a function; a function doesn't own the references in the caller.At most you can wrap your
root
value in a separateBinaryTree
class instance, than can be 'empty'. Give it aroot
attribute and if that attribute is set toNone
the tree is cleared.这篇关于在python中使用del语句后无法删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!