fork download
  1.  
  2. // This function does inorder traversal to find out the two swapped nodes.
  3. // It sets three pointers, first, middle and last. If the swapped nodes are
  4. // adjacent to each other, then first and middle contain the resultant nodes
  5. // Else, first and last contain the resultant nodes
  6. void correctBSTUtil( struct node* root, struct node** first,
  7. struct node** middle, struct node** last,
  8. struct node** prev )
  9. {
  10. if( root )
  11. {
  12. // Recur for the left subtree
  13. correctBSTUtil( root->left, first, middle, last, prev );
  14.  
  15. // If this node is smaller than the previous node, it's violating
  16. // the BST rule.
  17. if (*prev && root->data < (*prev)->data)
  18. {
  19. // If this is first violation, mark these two nodes as
  20. // 'first' and 'middle'
  21. if ( !*first )
  22. {
  23. *first = *prev;
  24. *middle = root;
  25. }
  26.  
  27. // If this is second violation, mark this node as last
  28. else
  29. *last = root;
  30. }
  31.  
  32. // Mark this node as previous
  33. *prev = root;
  34.  
  35. // Recur for the right subtree
  36. correctBSTUtil( root->right, first, middle, last, prev );
  37. }
  38. }
  39.  
  40. // A function to fix a given BST where two nodes are swapped. This
  41. // function uses correctBSTUtil() to find out two nodes and swaps the
  42. // nodes to fix the BST
  43. void correctBST( struct node* root )
  44. {
  45. // Initialize pointers needed for correctBSTUtil()
  46. struct node *first, *middle, *last, *prev;
  47. first = middle = last = prev = NULL;
  48.  
  49. // Set the poiters to find out two nodes
  50. correctBSTUtil( root, &first, &middle, &last, &prev );
  51.  
  52. // Fix (or correct) the tree
  53. if( first && last )
  54. swap( &(first->data), &(last->data) );
  55. else if( first && middle ) // Adjacent nodes swapped
  56. swap( &(first->data), &(middle->data) );
  57.  
  58. // else nodes have not been swapped, passed tree is really BST.
  59. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void correctBSTUtil(node*, node**, node**, node**, node**)’:
prog.cpp:13:29: error: invalid use of incomplete type ‘struct node’
         correctBSTUtil( root->left, first, middle, last, prev );
                             ^~
prog.cpp:6:29: note: forward declaration of ‘struct node’
 void correctBSTUtil( struct node* root, struct node** first,
                             ^~~~
prog.cpp:17:26: error: invalid use of incomplete type ‘struct node’
         if (*prev && root->data < (*prev)->data)
                          ^~
prog.cpp:6:29: note: forward declaration of ‘struct node’
 void correctBSTUtil( struct node* root, struct node** first,
                             ^~~~
prog.cpp:17:42: error: invalid use of incomplete type ‘struct node’
         if (*prev && root->data < (*prev)->data)
                                          ^~
prog.cpp:6:29: note: forward declaration of ‘struct node’
 void correctBSTUtil( struct node* root, struct node** first,
                             ^~~~
prog.cpp:36:29: error: invalid use of incomplete type ‘struct node’
         correctBSTUtil( root->right, first, middle, last, prev );
                             ^~
prog.cpp:6:29: note: forward declaration of ‘struct node’
 void correctBSTUtil( struct node* root, struct node** first,
                             ^~~~
prog.cpp: In function ‘void correctBST(node*)’:
prog.cpp:47:36: error: ‘NULL’ was not declared in this scope
     first = middle = last = prev = NULL;
                                    ^~~~
prog.cpp:47:36: note: ‘NULL’ is defined in header ‘<cstddef>’; did you forget to ‘#include <cstddef>’?
prog.cpp:1:1:
+#include <cstddef>
 
prog.cpp:47:36:
     first = middle = last = prev = NULL;
                                    ^~~~
prog.cpp:54:22: error: invalid use of incomplete type ‘struct node’
         swap( &(first->data), &(last->data) );
                      ^~
prog.cpp:6:29: note: forward declaration of ‘struct node’
 void correctBSTUtil( struct node* root, struct node** first,
                             ^~~~
prog.cpp:54:37: error: invalid use of incomplete type ‘struct node’
         swap( &(first->data), &(last->data) );
                                     ^~
prog.cpp:6:29: note: forward declaration of ‘struct node’
 void correctBSTUtil( struct node* root, struct node** first,
                             ^~~~
prog.cpp:54:9: error: ‘swap’ was not declared in this scope
         swap( &(first->data), &(last->data) );
         ^~~~
prog.cpp:56:22: error: invalid use of incomplete type ‘struct node’
         swap( &(first->data), &(middle->data) );
                      ^~
prog.cpp:6:29: note: forward declaration of ‘struct node’
 void correctBSTUtil( struct node* root, struct node** first,
                             ^~~~
prog.cpp:56:39: error: invalid use of incomplete type ‘struct node’
         swap( &(first->data), &(middle->data) );
                                       ^~
prog.cpp:6:29: note: forward declaration of ‘struct node’
 void correctBSTUtil( struct node* root, struct node** first,
                             ^~~~
prog.cpp:56:9: error: ‘swap’ was not declared in this scope
         swap( &(first->data), &(middle->data) );
         ^~~~
stdout
Standard output is empty