二叉搜索树的删除过程
考虑 BST 上的删除过程,当要删除的节点有两个子节点时。假设我总是用右子树中保存最小键的节点替换它。
问题是:这个过程可交换吗?也就是说,先删除 x 再删除 y 与先删除 y 再删除 x 的结果相同吗?
我认为答案是否定的,但我找不到反例,也找不到任何有效的推理。
编辑:
也许我必须更清楚。
考虑一下移植(节点x,节点y)过程:它将x替换为y(及其子树)。 因此,如果我想删除一个有两个子节点的节点(比如 x),我会将其替换为在其右子树中保存最小键的节点:
y = minimum(x.right)
transplant(y, y.right) // extracts the minimum (it doesn't have left child)
y.right = x.right
y.left = x.left
transplant(x,y)
问题是如何证明上述过程不可交换。
Consider the deletion procedure on a BST, when the node to delete has two children. Let's say i always replace it with the node holding the minimum key in its right subtree.
The question is: is this procedure commutative? That is, deleting x and then y has the same result than deleting first y and then x?
I think the answer is no, but i can't find a counterexample, nor figure out any valid reasoning.
EDIT:
Maybe i've got to be clearer.
Consider the transplant(node x, node y)
procedure: it replace x with y (and its subtree).
So, if i want to delete a node (say x) which has two children i replace it with the node holding the minimum key in its right subtree:
y = minimum(x.right)
transplant(y, y.right) // extracts the minimum (it doesn't have left child)
y.right = x.right
y.left = x.left
transplant(x,y)
The question was how to prove the procedure above is not commutative.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
删除(一般来说)是不可交换的。这是一个反例:
如果我们先删除 4,然后删除 3,会怎样?
当我们删除 4 时,我们会得到 6 作为新的根:
删除 3 不会改变树,但会得到这样的结果:
>如果我们删除 3 然后删除 4 会怎样?
当我们删除 3 时,树不会改变:
但是,当我们现在删除 4 时,新的根变为 7:
生成的两个树不相同,因此删除是不可交换的。
更新
我没有阅读限制,即您总是删除具有 2 个子节点的节点。我的解决方案适用于一般情况。如果/当我能找到反例时,我会更新它。
另一更新
我没有具体的证据,但我会冒险猜测:
在一般情况下,根据您是否有两个孩子、一个孩子或没有孩子,您会以不同的方式处理删除。在我提供的反例中,我首先删除一个具有两个子节点的节点,然后删除一个具有一个子节点的节点。之后,我删除一个没有子节点的节点,然后删除另一个有一个子节点的节点。
在仅删除具有两个子节点的特殊情况下,您需要考虑两个节点位于同一子树中的情况(因为它们是否位于不同的子树中并不重要;您可以确定整体结构不会根据删除顺序而改变)。您真正需要证明的是同一子树(每个节点有两个子节点)中节点的删除顺序是否重要。
考虑两个节点 A 和 B,其中 A 是 B 的祖先。然后您可以进一步将问题细化为:
当您考虑从二叉搜索树中删除两个具有祖先-后代关系的节点时,删除是否可交换其他(这意味着它们位于同一子树中)?
当你删除一个节点(假设是A)时,你会遍历右子树来找到最小元素。该节点将是叶节点,并且永远不能等于 B(因为 B 有两个子节点,不能是叶节点)。然后,您可以将 A 的值替换为该叶节点的值。这意味着树的唯一结构变化是用叶节点的值替换 A 的值,以及叶节点的丢失。
B 也涉及相同的过程。即,替换节点的值并替换叶节点。因此,一般来说,当您删除具有两个子节点的节点时,唯一的结构变化是您要删除的节点的值发生变化,以及删除您用作替换的叶节点的值 。
所以问题进一步细化:
你能保证无论删除顺序如何(当你总是删除有两个子节点的节点时),你总是会得到相同的替换节点吗?
答案(我认为)是肯定的。为什么?以下是一些观察结果:
这不是一个严格的证明;这些只是我的一些观察。无论如何,请随意戳洞!
Deletion (in general) is not commutative. Here is a counterexample:
What if we delete 4 and then 3?
When we delete 4, we get 6 as the new root:
Deleting 3 doesn't change the tree, but gives us this:
What if we delete 3 and then 4?
When we delete 3 the tree doesn't change:
However, when we now delete 4, the new root becomes 7:
The two resulting trees are not the same, therefore deletion is not commutative.
UPDATE
I didn't read the restriction that this is when you always delete a node with 2 children. My solution is for the general case. I'll update it if/when I can find a counter-example.
ANOTHER UPDATE
I don't have concrete proof, but I'm going to hazard a guess:
In the general case, you handle deletions differently based on whether you have two children, one child, or no children. In the counter-example I provided, I first delete a node with two children and then a node with one child. After that, I delete a node with no children and then another node with one child.
In the special case of only deleting nodes with two children, you want to consider the case where both nodes are in the same sub-tree (since it wouldn't matter if they are in different sub-trees; you can be sure that the overall structure won't change based on the order of deletion). What you really need to prove is whether the order of deletion of nodes in the same sub-tree, where each node has two children, matters.
Consider two nodes A and B where A is an ancestor of B. Then you can further refine the question to be:
Is deletion commutative when you are considering the deletion of two nodes from a Binary Search Tree which have a ancestor-descendant relationship to each other (this would imply that they are in the same sub-tree)?
When you delete a node (let's say A), you traverse the right sub-tree to find the minimum element. This node will be a leaf node and can never be equal to B (because B has two children and cannot be a leaf node). You would then replace the value of A with the value of this leaf-node. What this means is that the only structural change to the tree is the replacement of A's value with the value of the leaf-node, and the loss of the leaf-node.
The same process is involved for B. That is, you replace the value of the node and replace a leaf-node. So in general, when you delete a node with two children, the only structural change is the change in value of the node you are deleting, and the deletion of the leaf node who's value you are using as replacement.
So the question is further refined:
Can you guarantee that you will always get the same replacement node regardless of the order of deletion (when you are always deleting a node with two children)?
The answer (I think) is yes. Why? Here are a few observations:
This is not a rigorous proof; these are just some observations I've made. By all means, feel free to poke holes!
在我看来,Vivin 的答案中显示的反例是非交换性的唯一情况,并且确实通过只能删除具有两个子节点的限制来消除它。
但如果我们放弃维文的前提之一,即我们应该尽可能少地遍历右子树以找到任何可接受的后继者,那么它也可以被消除。相反,如果我们总是将右子树中的最小节点提升为后继,无论它的位置有多远,那么即使我们放宽删除少于两个子节点的限制,Vivin 的结果
is never reached if we start at
相反,我们首先删除 3(没有后继),然后删除 4(有 6 作为后继),得到
这与删除顺序相反是一样的。
那么删除将是可交换的,并且我认为它总是可交换的,给定我所命名的前提(后继者始终是已删除节点的右子树中的最小节点)。
我没有正式的证明可以提供,只是列举一些情况:
如果要删除的两个节点位于不同的子树中,则删除一个节点不会影响另一个节点。只有当它们处于同一路径时,删除的顺序才有可能影响结果。
因此,只有当祖先节点及其后代节点之一都被删除时,才会对交换性产生任何影响。那么,它们的垂直关系如何影响交换性呢?
后代位于祖先的左子树中。这种情况不会影响交换性,因为后继者来自右子树,根本不会影响左子树。
祖先的右子树中的后代。如果祖先的后继者始终是右子树中最小的节点,则删除顺序不能改变后继者的选择,无论删除哪个后代在祖先之前或之后。即使祖先的后继者结果是也将被删除的后代节点,该后代也将被替换为它的下一个最大节点,并且该后代不能有自己的左子树剩余需要处理。因此,删除祖先和任何右子树后代将始终是可交换的。
It seems to me that the counterexample shown in Vivin's answer is the sole case of non-commutativity, and that it is indeed eliminated by the restriction that only nodes with two children can be deleted.
But it can also be eliminated if we discard what appears to be one of Vivin's premises, which is that we should traverse the right subtree as little as possible to find any acceptable successor. If, instead, we always promote the smallest node in the right subtree as the successor, regardless of how far away it turns out to be located, then even if we relax the restriction on deleting nodes with fewer than two children, Vivin's result
is never reached if we start at
Instead, we would first delete 3 (without successor) and then delete 4 (with 6 as successor), yielding
which is the same as if the order of deletion were reversed.
Deletion would then be commutative, and I think it is always commutative, given the premise I have named (successor is always smallest node in right subtree of deleted node).
I do not have a formal proof to offer, merely an enumeration of cases:
If the two nodes to be deleted are in different subtrees, then deletion of one does not affect the other. Only when they are in the same path can the order of deletion possibly affect the outcome.
So any effect on commutativity can come only when an ancestor node and one of its descendants are both deleted. Now, how does their vertical relationship affect commutativity?
Descendant in the left subtree of the ancestor. This situation will not affect commutativity because the successor comes from the right subtree and cannot affect the left subtree at all.
Descendant in the right subtree of the ancestor. If the ancestor's successor is always the smallest node in the right subtree, then order of deletion cannot change the choice of successor, no matter what descendant is deleted before or after the ancestor. Even if the successor to the ancestor turns out to be the descendant node that is also to be deleted, that descendant too is replaced with the the next-largest node to it, and that descendant cannot have its own left subtree remaining to be dealt with. So deletion of an ancestor and any right-subtree descendant will always be commutative.
我认为当节点有 2 个子节点时,有两种同样可行的方法来删除它:
跳到情况 4...
情况1:删除3(叶节点)
2 3
/ \ --> / \
1 3 1
情况 2:删除 2(左子节点)
2 3
/ \ --> / \
1 3 1
情况 3:删除 2(右子节点)
2 2
/ \ --> / \
1 3 3
________________________________________________________________________________
情况4:删除2个(左右子节点)
2 2 3
/ \ --> / \ 或 / \
1 3 1 &n公共服务提供商;3
两者都有效并且有不同的结果树:)
_____________________________________________________________
正如此处解释的算法:http: //www.mathcs.emory.edu/~cheung/Courses/323/Syllabus/Trees/AVL-delete.html
<代码>
删除具有 2 个子节点的节点:
1) 将(待删除)节点替换为其有序前驱或有序后继
2)然后删除中序前驱或中序后继
I think there are two equally viable ways to delete a node, when it has 2 children:
SKIP TO CASE 4...
Case 1: delete 3 (Leaf node)
2 3
/ \ --> / \
1 3 1
Case 2: delete 2 (Left child node)
2 3
/ \ --> / \
1 3 1
Case 3: delete 2 (Right child node)
2 2
/ \ --> / \
1 3 3
______________________________________________________________________
Case 4: delete 2 (Left & Right child nodes)
2 2 3
/ \ --> / \ or / \
1 3 1 3
BOTH WORK and have different resulting trees :)
______________________________________________________________________
As algorithm explained here: http://www.mathcs.emory.edu/~cheung/Courses/323/Syllabus/Trees/AVL-delete.html
Deleting a node with 2 children nodes:
1) Replace the (to-delete) node with its in-order predecessor or in-order successor
2) Then delete the in-order predecessor or in-order successor
我在这里回应 Vivin 的第二次更新。
我认为这是对问题的一个很好的改写:
但下面这句大胆的话并不正确:
因为A的右子树中的最小元素可以有一个右子节点。所以,它不是一片叶子。
我们将 A 右子树中的最小元素称为
successor(A)
。现在,B 确实不能成为
successor(A)
,但它可以位于其右子树中。所以,这是一团糟。我试着总结一下。
假设:
其他内容我们可以从假设中推断出:
后继者(A)
,A也不是后继者(B)
。现在,鉴于此,我认为有 4 种不同的情况(像往常一样,让 A 成为 B 的祖先):
successor(A)
successor(A)
是 B B 的祖先,我认为(但当然我无法证明)情况1、2和4并不重要。
因此,只有在
successor(A)
是B的祖先的情况下,删除过程才不能是可交换的。或者可以吗?我传球:)
问候。
I respond here to Vivin's second update.
I think this is a good recast of the question:
but this bold sentence below is not true:
since the minimum element in A's right subtree can have a right child. So, it is not a leaf.
Let's call the minimum element in A's right subtree
successor(A)
.Now, it is true that B cannot be
successor(A)
, but it can be in its right subtree. So, it is a mess.I try to summarize.
Hypothesis:
Other stuff we can deduce from hypothesis:
successor(A)
, neither A issuccessor(B)
.Now, given that, i think there are 4 different cases (as usual, let be A an ancestor of B):
successor(A)
successor(A)
is an ancestor of BI think (but of course i cannot prove it) that cases 1, 2 and 4 don't matter.
So, only in the case
successor(A)
is an ancestor of B deletion procedure could not be commutative. Or could it?I pass the ball : )
Regards.