Given the array representation of Complete Binary Tree i.e, if index i is the parent, index 2*i + 1 is the left child and index 2*i + 2 is the right child. The task is to find the minimum number of swap required to convert it into Binary Search Tree.

Examples:

Input : arr[] = { 5, 6, 7, 8, 9, 10, 11 } Output : 3 Binary tree of the given array: Swap 1: Swap node 8 with node 5. Swap 2: Swap node 9 with node 10. Swap 3: Swap node 10 with node 7. So, minimum 3 swaps are required. Input : arr[] = { 1, 2, 3 } Output : 1 Binary tree of the given array: After swapping node 1 with node 2. So, only 1 swap required.

The idea is to use the fact that inorder traversal of Binary Search Tree is in increasing order of their value.

So, find the inorder traversal of the Binary Tree and store it in the array and try to sort the array. The minimum number of swap required to get the array sorted will be the answer. Please refer below post to find minimum number of swaps required to get the array sorted.

Minimum number of swaps required to sort an array

**Time Complexity: **O(n log n).

`// C++ program for Minimum swap required ` `// to convert binary tree to binary search tree ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Inorder Traversal of Binary Tree ` `void` `inorder(` `int` `a[], std::vector<` `int` `> &v, ` ` ` `int` `n, ` `int` `index) ` `{ ` ` ` `// if index is greater or equal to vector size ` ` ` `if` `(index >= n) ` ` ` `return` `; ` ` ` `inorder(a, v, n, 2 * index + 1); ` ` ` ` ` `// push elements in vector ` ` ` `v.push_back(a[index]); ` ` ` `inorder(a, v, n, 2 * index + 2); ` `} ` ` ` `// Function to find minimum swaps to sort an array ` `int` `minSwaps(std::vector<` `int` `> &v) ` `{ ` ` ` `std::vector<pair<` `int` `,` `int` `> > t(v.size()); ` ` ` `int` `ans = 0; ` ` ` `for` `(` `int` `i = 0; i < v.size(); i++) ` ` ` `t[i].first = v[i], t[i].second = i; ` ` ` ` ` `sort(t.begin(), t.end()); ` ` ` `for` `(` `int` `i = 0; i < t.size(); i++) ` ` ` `{ ` ` ` `// second element is equal to i ` ` ` `if` `(i == t[i].second) ` ` ` `continue` `; ` ` ` `else` ` ` `{ ` ` ` `// swaping of elements ` ` ` `swap(t[i].first, t[t[i].second].first); ` ` ` `swap(t[i].second, t[t[i].second].second); ` ` ` `} ` ` ` ` ` `// Second is not equal to i ` ` ` `if` `(i != t[i].second) ` ` ` `--i; ` ` ` `ans++; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 5, 6, 7, 8, 9, 10, 11 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `std::vector<` `int` `> v; ` ` ` `inorder(a, v, n, 0); ` ` ` `cout << minSwaps(v) << endl; ` `} ` ` ` `// This code is contributed by code_freak ` |

*chevron_right*

*filter_none*

**Output:**

3

**Exercise:** Can we extend this to normal binary tree, i.e., a binary tree represented using left and right pointers, and not necessarily complete?

This article is contributed by **Anuj Chauhan**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Find the node with minimum value in a Binary Search Tree
- Sum and Product of minimum and maximum element of Binary Search Tree
- Find the node with minimum value in a Binary Search Tree using recursion
- Binary Tree to Binary Search Tree Conversion using STL set
- Difference between Binary Tree and Binary Search Tree
- Binary Tree to Binary Search Tree Conversion
- Count the Number of Binary Search Trees present in a Binary Tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Convert a given Binary tree to a tree that holds Logical AND property
- Convert a given Binary tree to a tree that holds Logical OR property
- Convert a Binary Tree into its Mirror Tree
- Swap Nodes in Binary tree of every k'th level
- Pairwise Swap leaf nodes in a binary tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Search a node in Binary Tree