-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimumSwapsToConvertBinaryTreeToBST.cpp
61 lines (55 loc) · 1.44 KB
/
minimumSwapsToConvertBinaryTreeToBST.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Implemented by Kritagya Kumra
// C++ program for Minimum swap required
// to convert binary tree to binary search tree
// #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
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
{
// swapping 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;
}
// Implemented by Kritagya Kumra