-
Notifications
You must be signed in to change notification settings - Fork 1
/
Convert_normal_BST_to_Balanced_BST.cpp
128 lines (104 loc) · 2.1 KB
/
Convert_normal_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Problem Statement:
-----------------
Given a BST (Binary Search Tree) that may be unbalanced, convert it into a balanced BST that has minimum possible height.
Examples :
Input:
30
/
20
/
10
Output:
20
/ \
10 30
Input:
4
/
3
/
2
/
1
Output:
3 3 2
/ \ / \ / \
1 4 OR 2 4 OR 1 3 OR ..
\ / \
2 1 4
Input:
4
/ \
3 5
/ \
2 6
/ \
1 7
Output:
4
/ \
2 6
/ \ / \
1 3 5 7
*/
// Link --> https://www.geeksforgeeks.org/convert-normal-bst-balanced-bst/
// Code:
#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;
}