[英]Purpose of base case in binary search tree deletion
I am taking a course on algorithms and data structures in Python 3 and my instructor recently introduced us to the binary search tree. 我正在学习Python 3中的算法和数据结构课程,我的讲师最近向我们介绍了二叉搜索树。 However, I am having trouble understanding the deletion algorithm.
但是,我无法理解删除算法。 Below is the implementation we were taught, however when I initially wrote my own rendition, I did not include a "base case" and it still worked:
下面是我们所教授的实现,但是当我最初编写自己的演绎时,我没有包含“基本案例”,它仍然有效:
def remove(self, data):
if self.root:
self.root = self.remove_node(data, self.root)
def remove_node(self, data, node):
if node is None:
return node
if data < node.data:
node.leftChild = self.remove_node(data, node.leftChild)
elif data > node.data:
node.rightChild = self.remove_node(data, node.rightChild)
else:
if not node.rightChild and not node.leftChild:
print('removing leaf node')
del node
return None
if not node.leftChild:
print('removing node with single right child')
tempNode = node.rightChild
del node
return tempNode
elif not node.rightChild:
print('removing node with single left child')
tempNode = node.leftChild
del node
return tempNode
print('removing node with two children')
tempNode = self.get_predecessor(node.leftChild)
node.data = tempNode.data
node.leftChild = self.remove_node(tempNode.data, node.leftChild)
return node
Now, all of this makes sense to me except the statement below: 现在,除了以下声明之外,所有这些对我都有意义:
if node is None:
return node
When we previously learned about base cases, we were taught that they were essentially the exit points for our algorithms. 当我们之前了解基本情况时,我们被告知它们本质上是我们算法的出口点。 However, I do not understand how this is the case in the given code.
但是,我不明白给定代码中的情况如何。 For one, I do not see how a node could ever be empty and even if it was, why would we return an empty node?
首先,我没有看到节点是如何变空的,即使它是,为什么我们会返回一个空节点? As far as I can see, this check serves no purpose in the overall recursion because we do not seem to "recur towards it" as we would in any other recursive function.
据我所知,这个检查在整体递归中没有用处,因为我们似乎没有像任何其他递归函数那样“重复”。 I would greatly appreciate an explanation!
我非常感谢你的解释!
Base case(s), in general, serve one or more purposes, these include; 一般而言,基本案例有一个或多个目的,包括:
With tree deletion, the first point isn't really a concern (because the recursion tree will only have a finite number of nodes - same as the tree you recurse over). 删除树时,第一点并不是真正的问题(因为递归树只有有限数量的节点 - 与你递归的树相同)。 You will be concerned with points 2 and 3 here.
您将关注第2点和第3点。
In your function, you do have a base case - in fact, you have two (thanks to @user2357112) - 在你的函数, 你有一个基本情况-事实上,你有两个(感谢@ user2357112) -
The value-not-found portion, specified by 未找到的值部分,由。指定
if node is None: return node
and, 和,
The value-found portion, specified by your code inside the else
statement, which performs the actual deletion. value-found部分,由
else
语句中的代码指定,执行实际删除。
To keep the behaviour consistent with the recursive cases, the value-not-found base case returns None
. 为了使行为与递归情况保持一致,value-not-found基本情况返回
None
。 As you see, the first base case is consistent performs the second function of a generic base case outlined above, while the second base case performs the third. 如您所见,第一个基本案例是一致执行上面概述的通用基本案例的第二个功能,而第二个基本案例执行第三个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.