简体   繁体   中英

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.

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.

    For example, Nodes 5 and 25 are swapped in {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). Again, we find that node 5 is smaller than the previous node 20. This time, we save the context of node 5 ( current node ). 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?

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.

This example is not very good, because the position of 5 in the original array is the last one. Let's consider a sorted array {1, 2, 3, 4, 5} . Swap 2 and 4, then we get {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. In the case of {1, 4, 3, 2, 5} , we have pair (4, 3) , and pair (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. In the case of {1, 4, 3, 2, 5} , it is 4 and 2 being swapped.

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. Then 25 and 5 are the two elements being swapped.

Following @jeffreys' notation,

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

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. Let's say p < q, so the A[p],A[p+1] is the first descending pair, and the q's are the second.

  • 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. 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. We must therefore chose 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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