简体   繁体   English

在二叉搜索树中查找交换节点

[英]Find the swapped nodes in binary search tree

Two of the nodes of a Binary Search Tree are swapped. 二进制搜索树的两个节点被交换。

Input Tree: 输入树:

     10
    /  \
   5    8
  / \
 2   20

In the above tree, nodes 20 and 8 must be swapped to fix the tree. 在上面的树中,必须交换节点20和8以固定树。

Output tree: 输出树:

     10
    /  \
   5    20
  / \
 2   8

I followed the solution given in here . 我按照这里给出的解决方案。 But I feel the solution is incorrect because: 但我觉得解决方案不正确,因为:

As per the site: 根据网站:

  1. The swapped nodes are not adjacent in the inorder traversal of the BST. 交换节点在BST的顺序遍历中不相邻。

    For example, Nodes 5 and 25 are swapped in {3 5 7 8 10 15 20 25} . 例如,节点5和25在{3 5 7 8 10 15 20 25}中交换。
    The inorder traversal of the given tree is 3 25 7 8 10 15 20 5 If we observe carefully, during inorder traversal, we find node 7 is smaller than the previous visited node 25. Here save the context of node 25 (previous node). 给定树的顺序遍历是3 25 7 8 10 15 20 5如果我们仔细观察,在顺序遍历期间,我们发现节点7小于先前的访问节点25.这里保存节点25(上一节点)的上下文。 Again, we find that node 5 is smaller than the previous node 20. This time, we save the context of node 5 ( current node ). 同样,我们发现节点5小于前一节点20.这次,我们保存节点5(当前节点)的上下文。 Finally swap the two node's values. 最后交换两个节点的值。

So my point is if it is considering 25 because it is greater than 7 than it should consider 20 as well because it is also greater than 5. So is this correct solution or I am missing something? 所以我的观点是,如果它考虑25因为它大于7而不应该考虑20因为它也大于5.所以这是正确的解决方案还是我遗漏了什么?

Yes. 是。 It is considering 25 because it is greater than 7. But, it should not consider 20 as well because it is also greater than 5. Instead, it should consider 5 because it is less than 20. 这是考虑到25,因为它是大于7。但是,这应该考虑20以及因为它也大于5。相反,它应该考虑5,因为它是小于20。

This example is not very good, because the position of 5 in the original array is the last one. 这个例子不是很好,因为原始数组中5的位置是最后一个。 Let's consider a sorted array {1, 2, 3, 4, 5} . 让我们考虑一个排序的数组{1, 2, 3, 4, 5} Swap 2 and 4, then we get {1, 4, 3, 2, 5} . 交换2和4,然后我们得到{1, 4, 3, 2, 5} If two elements (not adjacent) in a sorted array is swapped, for all pairs like (A[i], A[i+1]) , there will be exactly two pairs that is in wrong order, namely descending order. 如果交换排序数组中的两个元素(不相邻),对于所有对(A[i], A[i+1]) ,将会有两对错误顺序,即降序。 In the case of {1, 4, 3, 2, 5} , we have pair (4, 3) , and pair (3, 2) . {1, 4, 3, 2, 5} ,我们有对(4, 3)和对(3, 2) Suppose we have pair (A[p], A[p+1]) and pair (A[q], A[q+1]) , such that A[p] > A[p+1] and A[q] > A[q+1] , we can claim that it is A[p] and A[q+1] being swapped. 假设我们有对(A[p], A[p+1])和对(A[q], A[q+1]) ,使得A[p] > A[p+1]A[q] > A[q+1] ,我们可以声称它是A[p]A[q+1]被交换。 In the case of {1, 4, 3, 2, 5} , it is 4 and 2 being swapped. {1, 4, 3, 2, 5}的情况下,交换4和2。

Now come back to the example 3 25 7 8 10 15 20 5 , in which 25, 7 and 20 5 are the only two pairs in wrong order. 现在回到例子3 25 7 8 10 15 20 5 ,其中25, 720 5是错误顺序的唯一两对。 Then 25 and 5 are the two elements being swapped. 然后25和5是被交换的两个元素。

Following @jeffreys' notation, 关注@jeffreys的表示法,

if we have pair (A[p], A[p+1]) and pair (A[q], A[q+1]), such that A[p] > A[p+1] and A[q] > A[q+1], we can claim that it is A[p] and A[q+1] being swapped 如果我们有对(A [p],A [p + 1])和对(A [q],A [q + 1]),那么A [p]> A [p + 1]和A [q ]> A [q + 1],我们可以声称它是A [p]和A [q + 1]被交换

You know that there's only a single swap, that would create either 2 discrepancies in the sorted order, or only one if they're adjacent. 您知道只有一次交换,这会在排序顺序中产生2个差异,或者如果它们相邻则只产生一个差异。 Let's say p < q, so the A[p],A[p+1] is the first descending pair, and the q's are the second. 假设p <q,所以A [p],A [p + 1]是第一个下降对,q是第二个。

  • If there's no second couple, than swapping the first couple would fix the tree, that's the easy part. 如果没有第二对夫妇,那么交换第一对夫妇就可以修复这棵树,这很容易。 Otherwise we know there are two non-adjacent nodes. 否则我们知道有两个非相邻节点。

  • Out of the A[p] and A[p+1] let's say that A[p+1] was the one out of place. 在A [p]和A [p + 1]之外,让我们说A [p + 1]是不合适的。 Since this is the first couple we would have to move A[p+1] forward towards the second couple, but that means that it's still going to be smaller than the earlier A[p] that stayed in place, so we would not create a sorted array. 由于这是第一对夫妇,我们必须将A [p + 1]向前移动到第二对夫妇,但这意味着它仍然会比原来的A [p]小,所以我们不会创建一个排序的数组。 We must therefore chose A[p]. 因此,我们必须选择A [p]。

  • Same goes for the A[q] and A[q+1], let's say that A[q] was out of place, that means we'll have to move it backwards, and it would still be larger than A[q+1] appearing later, again breaking sort. 同样适用于A [q]和A [q + 1],假设A [q]不合适,这意味着我们必须向后移动它,它仍然会大于A [q + 1]出现在后面,再次打破排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM