forked from NiharRanjan53/HacktoberFest_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeBstToCpp.cpp
181 lines (134 loc) · 3.2 KB
/
TreeBstToCpp.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
ContestName = Shinchu_itachi
" Walking on the road not taken with my own gutts.. "
*/
#include <bits/stdc++.h>
using namespace std ;
class Tree {
public:
int val ;
Tree *left , *right;
Tree( int data) {
this-> val = data;
left = right = NULL;
}
void inorderBST ( Tree* root , vector<int> & a ) {
if ( !root ) return ;
inorderBST( root -> left , a );
cout << root -> val << " ";
a.push_back(root -> val );
inorderBST( root -> right , a );
}
vector<Tree*> ab;
Tree* insert( Tree* root , int data) {
if ( root == NULL) return new Tree(data);
if ( data < root -> val ) root -> left = insert ( root -> left , data);
if ( data >= root -> val ) root -> right = insert ( root -> right , data);
return root ;
}
int i = 0 , j = 0;
Tree* merge ( vector<int> &a , vector<int> &b , vector<Tree*> &ab) {
Tree* dum = new Tree(-1);
Tree* temp = dum , *list = dum;
if ( i >= a.size() or j >= b.size()) return dum;
while ( i < a.size() and j < b.size()) {
if ( a[i] <= b[j]) {
Tree* root = new Tree(a[i++]);
ab.push_back(root);
dum -> right = root;
dum = dum -> right;
dum -> left = temp;
temp = dum;
}
else {
Tree* root = new Tree(b[j++]);
ab.pb(root);
dum -> right = root;
dum = dum -> right;
dum -> left = temp;
temp = dum;
}
}
while ( i < a.size() ) {
Tree* root = new Tree(a.at(i));
i++;
ab.pb(root);
dum -> right = root;
dum = dum -> right;
dum -> left = temp;
temp = dum;
}
while ( j < b.size() ) {
Tree* root = new Tree(b.at(j));
j++;
ab.pb(root);
dum -> right = root;
dum = dum -> right;
dum -> left = temp;
temp = dum;
}
return list->right;
}
void inorder( Tree* root) {
if (! root) return;
inorder ( root -> left );
cout << root -> val << " ";
inorder ( root -> right);
}
};
void solve() {
Tree* root = new Tree(6) , *root1 = new Tree(9);
Tree bst = NULL;
Tree bst1 = NULL;
Tree b = NULL;
bst.insert(root , 4);
bst.insert( root, 5);
bst.insert( root, 3);
bst.insert( root, 2);
bst.insert( root, 8);
bst.insert( root, 7);
bst.insert( root, 9);
bst1.insert(root1 , 4);
bst1.insert( root1, 5);
bst1.insert( root1, 3);
bst1.insert( root1, 12);
bst1.insert( root1, 18);
bst1.insert( root1, 17);
bst1.insert( root1, 19);
vector<int> in1 , in2 ;
cout << "Tree1 is : " << nline;
bst.inorderBST ( root , in1);
cout << endl;
cout << "Tree2 is : " << nline;
bst1.inorderBST ( root1 , in2);
cout << endl;
debug(in1)
debug(in2)
cout << nline;
vector<Tree*> ab;
root = b.merge( in1, in2 , ab );
for ( auto i : ab) cout << i -> val << " ";
}
inline void testcases() {
int test = 1, testcase = 1 ;
// cin >> test ;
cout << setprecision(12) ;
cerr << setprecision(8) ;
while (test --) {
// cout << "Case #" << testcase++ << ": ";
solve () ;
}
}
int main () {
fastio();
#ifndef ONLINE_JUDGE
// freopen("output.txt", "w", stdout);
// freopen("input.txt", "r", stdin);
//freopen("error.txt", "w", stderr);
#endif
auto start = high_resolution_clock::now();
testcases();
auto end = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(end - start);
cerr << "Time : " << duration.count() / 1000 ;
}