-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvert_BST_to_Balanced_BST.cpp
75 lines (58 loc) · 1.32 KB
/
Convert_BST_to_Balanced_BST.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *left, *right;
};
Node *create()
{
int data;
Node *tree;
tree = new Node;
cout << "\nEnter data to be inserted or type -1 : ";
cin >> data;
if (data == -1)
return 0;
tree->data = data;
cout << "Enter left child of " << data;
tree->left = create();
cout << "Enter right child of " << data;
tree->right = create();
return tree;
}
vector<int> v;
void preorder(Node *root)
{
if (root == NULL)
return;
cout << root->data << " ";
// To store the data.
v.push_back(root->data);
preorder(root->left);
preorder(root->right);
}
Node *Balanced_BST(int start, int end)
{
if (start > end)
return NULL;
int mid = (start + end) / 2;
Node *root = new Node;
root->data = v[mid];
root->left = Balanced_BST(start, mid - 1);
root->right = Balanced_BST(mid + 1, end);
return root;
}
int main()
{
Node *root = NULL;
root = create();
cout << "Preorder traversal of the tree is : ";
preorder(root);
// We will sort the array to create the balanced BST.
sort(v.begin(), v.end());
root = Balanced_BST(0, v.size() - 1);
cout << "\nPreorder traversal of the tree after balancing : ";
preorder(root);
return 0;
}