Two nodes of a BST are swapped, correct the BST

【Two nodes of a BST are swapped, correct the BST】亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述Two nodes of a BST are swapped, correct the BST相关的知识,希望能为你提供帮助。
Two nodes of a BST are swapped, correct the BST[转载]
Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST.

Input Tree: 10 /58 /220In the above tree, nodes 20 and 8 must be swapped to fix the tree. Following is the output tree 10 /520 /28

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.
The inorder traversal of a BST produces a sorted array. So a  simple method  is to store inorder traversal of the input tree in an auxiliary array. Sort the auxiliary array. Finally, insert the auxiilary array elements back to the BST, keeping the structure of the BST same. Time complexity of this method is O(nLogn) and auxiliary space needed is O(n).
We can solve this in O(n) time and with a single traversal of the given BST. Since inorder traversal of BST is always a sorted array, the problem can be reduced to a problem where two elements of a sorted array are swapped. There are two cases that we need to handle:
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.
2.  The swapped nodes are adjacent in the inorder traversal of BST.
For example, Nodes 7 and 8 are swapped in {3 5 7 8 10 15 20 25}. The inorder traversal of the given tree is 3 5 8 7 10 15 20 25

Unlike case #1, here only one point exists where a node value is smaller than previous node value. e.g. node 7 is smaller than node 8.
How to Solve?  We will maintain three pointers, first, middle and last. When we find the first point where current node value is smaller than previous node value, we update the first with the previous node & middle with the current node. When we find the second point where current node value is smaller than previous node value, we update the last with the current node. In case #2, we will never find the second point. So, last pointer will not be updated. After processing, if the last node value is null, then two swapped nodes of BST are adjacent.
Following is the implementation of the given code.
1 // Two nodes in the BST‘s swapped, correct the BST. 2 #include < stdio.h> 3 #include < stdlib.h> 4 5 /* A binary tree node has data, pointer to left child 6and a pointer to right child */ 7 struct node 8 { 9int data; 10struct node *left, *right; 11 }; 12 13 // A utility function to swap two integers 14 void swap( int* a, int* b ) 15 { 16int t = *a; 17*a = *b; 18*b = t; 19 } 20 21 /* Helper function that allocates a new node with the 22given data and NULL left and right pointers. */ 23 struct node* newNode(int data) 24 { 25struct node* node = (struct node *)malloc(sizeof(struct node)); 26node-> data = https://www.songbingjia.com/android/data; 27node-> left = NULL; 28node-> right = NULL; 29return(node); 30 } 31 32 // This function does inorder traversal to find out the two swapped nodes. 33 // It sets three pointers, first, middle and last.If the swapped nodes are 34 // adjacent to each other, then first and middle contain the resultant nodes 35 // Else, first and last contain the resultant nodes 36 void correctBSTUtil( struct node* root, struct node** first, 37struct node** middle, struct node** last, 38struct node** prev ) 39 { 40if( root ) 41{ 42// Recur for the left subtree 43correctBSTUtil( root-> left, first, middle, last, prev ); 44 45// If this node is smaller than the previous node, it‘s violating 46// the BST rule. 47if (*prev & & root-> data < (*prev)-> data) 48{ 49// If this is first violation, mark these two nodes as 50// ‘first‘ and ‘middle‘ 51if ( !*first ) 52{ 53*first = *prev; 54*middle = root; 55} 56 57// If this is second violation, mark this node as last 58else 59*last = root; 60} 61 62// Mark this node as previous 63*prev = root; 64 65// Recur for the right subtree 66correctBSTUtil( root-> right, first, middle, last, prev ); 67} 68 } 69 70 // A function to fix a given BST where two nodes are swapped.This 71 // function uses correctBSTUtil() to find out two nodes and swaps the 72 // nodes to fix the BST 73 void correctBST( struct node* root ) 74 { 75// Initialize pointers needed for correctBSTUtil() 76struct node *first, *middle, *last, *prev; 77first = middle = last = prev = NULL; 78 79// Set the poiters to find out two nodes 80correctBSTUtil( root, & first, & middle, & last, & prev ); 81 82// Fix (or correct) the tree 83if( first & & last ) 84swap( & (first-> data), & (last-> data) ); 85else if( first & & middle ) // Adjacent nodes swapped 86swap( & (first-> data), & (middle-> data) ); 87 88// else nodes have not been swapped, passed tree is really BST. 89 } 90 91 /* A utility function to print Inoder traversal */ 92 void printInorder(struct node* node) 93 { 94if (node == NULL) 95return; 96printInorder(node-> left); 97printf("%d ", node-> data); 98printInorder(node-> right); 99 } 100 101 /* Driver program to test above functions*/ 102 int main() 103 { 104/*6 105/106102 107/ \/ 10813 712 10910 and 2 are swapped 110*/ 111 112struct node *root = newNode(6); 113root-> left= newNode(10); 114root-> right= newNode(2); 115root-> left-> left= newNode(1); 116root-> left-> right = newNode(3); 117root-> right-> right = newNode(12); 118root-> right-> left = newNode(7); 119 120printf("Inorder Traversal of the original tree \n"); 121printInorder(root); 122 123correctBST(root); 124 125printf("\nInorder Traversal of the fixed tree \n"); 126printInorder(root); 127 128return 0; 129 }

 

    推荐阅读