[英]Deletion procedure for a Binary Search Tree
Consider the deletion procedure on a BST, when the node to delete has two children. 当要删除的节点有两个子节点时,请考虑BST上的删除过程。 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? 也就是说,删除x然后y与删除第一个y然后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). 考虑transplant(node x, node y)
过程:它用y(和它的子树)替换x。 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: 所以,如果我想删除一个有两个子节点的节点(比如说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)
The question was how to prove the procedure above is not commutative. 问题是如何证明上述程序不是可交换的。
Deletion (in general) is not commutative. 删除(一般情况下)不是可交换的。 Here is a counterexample: 这是一个反例:
4
/ \
3 7
/
6
What if we delete 4 and then 3? 如果我们删除4然后删除3怎么办?
When we delete 4, we get 6 as the new root: 当我们删除4时,我们得到6作为新根:
6
/ \
3 7
Deleting 3 doesn't change the tree, but gives us this: 删除3不会改变树,但是给我们这个:
6
\
7
What if we delete 3 and then 4? 如果我们删除3然后删除4怎么办?
When we delete 3 the tree doesn't change: 当我们删除3时,树不会改变:
4
\
7
/
6
However, when we now delete 4, the new root becomes 7: 但是,当我们现在删除4时,新的根变为7:
7
/
6
The two resulting trees are not the same, therefore deletion is not commutative. 产生的两棵树不一样,因此删除不是可交换的。
UPDATE UPDATE
I didn't read the restriction that this is when you always delete a node with 2 children. 当你总是删除一个有2个孩子的节点时,我没有读到这个限制。 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: 考虑两个节点A和B,其中A是B的祖先。然后,您可以进一步细化问题:
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. 当您删除节点(假设为A)时,您将遍历右侧子树以查找最小元素。 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). 此节点将是叶节点,并且永远不能等于B(因为B有两个子节点,不能是叶节点)。 You would then replace the value of A with the value of this leaf-node. 然后,您将使用此叶节点的值替换A的值。 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. 这意味着树的唯一结构变化是用叶节点的值替换A的值,以及叶节点的丢失。
The same process is involved for B. That is, you replace the value of the node and replace a leaf-node. B涉及相同的过程。也就是说,您替换节点的值并替换叶节点。 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! 无论如何,随意戳洞!
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. 在我看来,Vivin的答案中显示的反例是非交换性的唯一情况,并且确实通过限制只能删除有两个孩子的节点来消除它。
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. 但是如果我们抛弃看起来像Vivin的前提之一的话,它也可以被消除,这就是我们应该尽可能少地遍历正确的子树以找到任何可接受的继任者。 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 相反,如果我们总是将右子树中的最小节点作为后继子进行推广,无论它到底有多远,那么即使我们放宽了删除少于两个孩子的节点的限制,Vivin的结果也是如此
7 / 6is never reached if we start at 如果我们开始,永远不会达到
\n 4 4\n / \\ / \\\n 3 7 3 7\n / /\n 6 6\n
Instead, we would first delete 3 (without successor) and then delete 4 (with 6 as successor), yielding 相反,我们首先删除3(没有后继者),然后删除4(6作为后继者),屈服
\n 6 6\n \\ \\\n 7 7\n
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. 因此,删除祖先和任何右子树后代将始终是可交换的。
I think there are two equally viable ways to delete a node, when it has 2 children: 我认为有两种同样可行的删除节点的方法,当它有2个子节点时:
SKIP TO CASE 4... 跳到案例4 ......
Case 1: delete 3 (Leaf node) 案例1:删除3(叶节点)
2 3 2 3
/ \\ --> / \\ / \\ - > / \\
1 3 1 1 3 1
Case 2: delete 2 (Left child node) 案例2:删除2(左子节点)
2 3 2 3
/ \\ --> / \\ / \\ - > / \\
1 3 1 1 3 1
Case 3: delete 2 (Right child node) 案例3:删除2(右子节点)
2 2 2 2
/ \\ --> / \\ / \\ - > / \\
1 3 3 1 3 3
______________________________________________________________________ ______________________________________________________________________
Case 4: delete 2 (Left & Right child nodes) 案例4:删除2(左右子节点)
2 2 3 2 2 3
/ \\ --> / \\ or / \\ / \\ - > / \\或/ \\
1 3 1 3 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
正如此处解释的算法: 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
I respond here to Vivin's second update. 我在此回应Vivin的第二次更新。
I think this is a good recast of the question: 我认为这是一个很好的重写问题:
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)? 当您考虑从二进制搜索树中删除两个具有祖先 - 后代关系的节点时,删除是否可交换(这意味着它们在同一个子树中)?
but this bold sentence below is not true: 但下面这个大胆的句子不是真的:
When you delete a node (let's say A), you traverse the right sub-tree to find the minimum element. 当您删除节点(假设为A)时,您将遍历右侧子树以查找最小元素。 This node will be a leaf node and can never be equal to B 该节点将是叶节点 ,永远不能等于B.
since the minimum element in A's right subtree can have a right child . 因为A右子树中的最小元素可以有一个正确的子元素。 So, it is not a leaf. 所以,它不是一片叶子。 Let's call the minimum element in A's right subtree successor(A)
. 让我们调用A的右子树successor(A)
的最小元素。 Now, it is true that B cannot be successor(A)
, but it can be in its right subtree. 现在,B确实不能成为successor(A)
,但它可以是正确的子树。 So, it is a mess. 所以,这是一团糟。
I try to summarize. 我试着总结一下。
Hypothesis : 假设 :
Other stuff we can deduce from hypothesis: 我们可以从假设中推断出的其他东西 :
successor(A)
, neither A is successor(B)
. B不是successor(A)
,A不是successor(B)
。 Now, given that, i think there are 4 different cases (as usual, let be A an ancestor of B): 现在,鉴于此,我认为有4种不同的情况(像往常一样,让我们成为B的祖先):
successor(A)
B是successor(A)
的祖先successor(A)
successor(A)
is an ancestor of B successor(A)
是B的祖先 I think (but of course i cannot prove it) that cases 1, 2 and 4 don't matter. 我认为(但当然我无法证明)案例1,2和4并不重要。 So, only in the case successor(A)
is an ancestor of B deletion procedure could not be commutative. 因此,只有在successor(A)
是B删除程序的祖先的情况下才能进行交换。 Or could it? 或者可以吗?
I pass the ball : ) 我传球:)
Regards. 问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.