forked from priya2006/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
291 lines (230 loc) · 5.65 KB
/
BinaryTree.cpp
File metadata and controls
291 lines (230 loc) · 5.65 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
It is all about
binary tree
*/
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class BinaryTreeNode {
public:
T data;
BinaryTreeNode<T>* left;
BinaryTreeNode<T>* right;
BinaryTreeNode(T d)
{
data = d;
left = NULL;
right = NULL;
}
//to delete tree by typing "delete root" only
~BinaryTreeNode ()
{
delete left;
delete right;
}
};
//Take binary tree input DFS wise
BinaryTreeNode<int>* input() {
int rootData;
cin >> rootData;
if (rootData == -1)
return NULL;
BinaryTreeNode<int>* root = new BinaryTreeNode<int>(rootData);
BinaryTreeNode<int>* leftChild = input();
BinaryTreeNode<int>* rightChild = input();
root->left = leftChild;
root->right = rightChild;
return root;
}
//Take binary ree input BFS wise
BinaryTreeNode<int>* input1() {
int rootData;
cin >> rootData;
if (rootData == -1)
return NULL;
BinaryTreeNode<int>* root = new BinaryTreeNode<int>(rootData);
queue<BinaryTreeNode<int>*> q;
q.push(root);
while (!q.empty ())
{
BinaryTreeNode<int>* node = q.front ();
q.pop();
int l, r;
cin >> l >> r;
BinaryTreeNode<int> *leftChild = NULL, *rightChild = NULL;
if (l != -1) {
leftChild = new BinaryTreeNode<int>(l);
q.push(leftChild);
}
if (r != -1) {
rightChild = new BinaryTreeNode<int>(r);
q.push(rightChild);
}
node->left = leftChild;
node->right = rightChild;
}
return root;
}
//Print binary tree level wise
void print1(BinaryTreeNode<int>* root) {
if (root == NULL) {
cout << "Tree is empty!!" << endl;
return;
}
queue<BinaryTreeNode<int>*> q;
q.push(root);
while (!q.empty())
{
BinaryTreeNode<int>* node = q.front();
q.pop();
cout << node->data << ":";
if (node->left) {
cout << "L" << node->left->data << " ";
q.push(node->left);
}
if (node->right) {
cout << "R" << node->right->data;
q.push(node->right);
}
cout << endl;
}
}
//Print binary tree pre-order wise
void print(BinaryTreeNode<int>* root) {
if (root == NULL) {
// cout << "Tree is Empty!!" << endl;
return;
}
cout << root->data << ":";
if (root->left)
cout << "L" << root->left->data << " ";
if (root->right)
cout << "R" << root->right->data;
cout << endl;
print(root->left);
print(root->right);
}
//count no of node in binary tree
int countNode(BinaryTreeNode<int>* root) {
if (root == NULL) {
// cout << "Tree is empty!!" << endl;
return 0;
}
int ans = 0;
ans = ans + countNode(root->left);
ans = ans + countNode(root->right);
return (ans + 1);
}
//Print tree in inorder format
void inorder (BinaryTreeNode<int>* root) {
if (root == NULL) {
// cout << "Tree is empty!!" << endl;
return;
}
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
//Print tree in post-order format
void postorder(BinaryTreeNode<int>* root) {
if (root == NULL) {
// cout << "Tree is empty!!" << endl;
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
//Helper function to construct tree using its in-order and pre-order
BinaryTreeNode<int>* helperFunction(int* in, int* pre, int in_s, int in_e, int pre_s, int pre_e) {
if (in_s > in_e) {
return NULL;
}
int rootData = pre[pre_s];
int i;
for (i = in_s; i <= in_e; i++) {
if (rootData == in[i])
{
break;
}
}
int lin_s, lin_e, lpre_s, lpre_e, rin_s, rin_e, rpre_s, rpre_e;
lin_s = in_s;
lin_e = i - 1;
rin_s = i + 1;
rin_e = in_e;
lpre_s = pre_s + 1;
lpre_e = lpre_s + (lin_e - lin_s);
rpre_s = lpre_e + 1;
rpre_e = pre_e;
BinaryTreeNode<int>* root = new BinaryTreeNode<int>(rootData);
BinaryTreeNode<int>* leftChild = helperFunction(in, pre, lin_s, lin_e, lpre_s, lpre_e);
BinaryTreeNode<int>* rightChild = helperFunction(in, pre, rin_s, rin_e, rpre_s, rpre_e);
root->left = leftChild;
root->right = rightChild;
return root;
}
//Construct tree from given pre-order and in-order
BinaryTreeNode<int>* constructTree(int* in, int* pre, int s) {
BinaryTreeNode<int>* root = helperFunction (in, pre, 0, s - 1, 0, s - 1);
return root;
}
//Height of binary tree
int height (BinaryTreeNode<int>* root) {
if (root == NULL) {
return 0;
}
return 1 + max (height(root->left) , height(root->right));
}
//Diameter of binary tree -- time complexcity O(n^2)
int diameter (BinaryTreeNode<int>* root) {
if (root == NULL) {
return 0;
}
int op1 = height (root->left) + height (root->right);
int op2 = diameter (root->left);
int op3 = diameter (root->right);
return max(op1, max(op2, op3));
}
//Diameter of binary tree -- time complexcity O(n)
pair<int, int> heightDaimeter (BinaryTreeNode<int>* root) {
if (root == NULL) {
pair<int, int> p;
p = make_pair(0, 0);
return p;
}
pair<int, int> p1 = heightDaimeter (root->left);
pair<int, int> p2 = heightDaimeter (root->right);
pair<int, int> p;
p.first = 1 + max(p1.first, p2.first);
p.second = max(p1.first + p2.first, max(p1.first, p2.first));
return p;
}
int main() {
#ifndef ONLINE_JUDGE
//for getting input from input.txt
freopen ("input.txt", "r", stdin);
//for writing output to output.txt
freopen ("output.txt", "w", stdout);
#endif
/*BinaryTreeNode<int>* root = new BinaryTreeNode<int> (1);
BinaryTreeNode<int>* l = new BinaryTreeNode<int> (2);
BinaryTreeNode<int>* r = new BinaryTreeNode<int> (3);
root->left = l;
root->right = r;*/
BinaryTreeNode<int>* root = input1();
print1(root);
// int n = countNode(root);
// cout << n << endl;
// inorder(root);
// cout << endl;
// postorder(root);
// int in[] = {4, 2, 5, 1, 8, 6, 9, 3, 7};
// int pre[] = {1, 2, 4, 5, 3, 6, 8, 9, 7};
// BinaryTreeNode<int>* root = constructTree(in, pre, 9);
// print(root);
int d = diameter(root);
cout << d << endl;
pair<int, int> d2 = heightDaimeter(root);
cout << d2.second << endl;
}