-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_Tree_Example.java
516 lines (415 loc) · 12.3 KB
/
Binary_Tree_Example.java
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
*
* @author pulkit4tech
*/
public class Binary_Tree_Example implements Runnable {
BufferedReader c;
PrintWriter pout;
// static long mod = 1000000007;
public void run() {
try {
c = new BufferedReader(new InputStreamReader(System.in));
pout = new PrintWriter(System.out, true);
solve();
pout.close();
} catch (Exception e) {
pout.close();
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) throws Exception {
new Thread(new Binary_Tree_Example()).start();
}
void solve() throws Exception {
// Question?
// If you are given two traversal sequences,
// can you construct the binary tree?
//
// It depends on what traversals are given.
// If one of the traversal methods is Inorder then the tree can be
// constructed, otherwise not.
// tree_traversal();
// tree_diameter();
// getMaxWidth();
// print_node_at_k_distance();
// print_ancestor();
//subtree_of_anotherTree();
//construct_tree_from_given_order();
flatten_binary_tree();
}
void flatten_binary_tree(){
BinaryTree<Integer> tree = new BinaryTree<>();
tree.root = new Node<Integer>(1);
tree.root.left = new Node<Integer>(2);
tree.root.right = new Node<Integer>(3);
tree.root.left.left = new Node<Integer>(4);
tree.root.left.right = new Node<Integer>(5);
tree.flattenTree(tree.root);
}
void construct_tree_from_given_order(){
BinaryTree<Character> tree = new BinaryTree<>();
char in[] = new char[]{'D', 'B', 'E', 'A', 'F', 'C'};
char pre[] = new char[]{'A', 'B', 'D', 'E', 'C', 'F'};
int len = in.length;
Node<Character> root = tree.buildTree(in, pre, 0, len - 1);
// building the tree by printing inorder traversal
System.out.println("Inorder traversal of constructed tree is : ");
tree.printInorder(root);
}
void subtree_of_anotherTree() {
// refer GeekForGeek
BinaryTree<Integer> tree1 = new BinaryTree<>();
// TREE 1
/*
* Construct the following tree 26 / \ 10 3 / \ \ 4 6 3 \ 30
*/
tree1.root = new Node<>(26);
tree1.root.right = new Node<>(3);
tree1.root.right.right = new Node<>(3);
tree1.root.left = new Node<>(10);
tree1.root.left.left = new Node<>(4);
tree1.root.left.left.right = new Node<>(30);
tree1.root.left.right = new Node<>(6);
// TREE 2
/*
* Construct the following tree 10 / \ 4 6 \ 30
*/
BinaryTree<Integer> tree2 = new BinaryTree<>();
tree2.root = new Node<>(10);
tree2.root.right = new Node<>(6);
tree2.root.left = new Node<>(4);
tree2.root.left.right = new Node<>(30);
if (tree1.isSubTree(tree1.root, tree2.root))
System.out.println("Tree 2 is subtree of Tree 1 ");
else
System.out.println("Tree 2 is not a subtree of Tree 1");
}
void print_ancestor() {
BinaryTree<Integer> tree = new BinaryTree<>();
tree.root = new Node<Integer>(1);
tree.root.left = new Node<Integer>(2);
tree.root.right = new Node<Integer>(3);
tree.root.left.left = new Node<Integer>(4);
tree.root.left.right = new Node<Integer>(5);
tree.printAncestors(tree.root, 5);
}
void print_node_at_k_distance() {
BinaryTree<Integer> tree = new BinaryTree<>();
tree.root = new Node<Integer>(1);
tree.root.left = new Node<Integer>(2);
tree.root.right = new Node<Integer>(3);
tree.root.left.left = new Node<Integer>(4);
tree.root.left.right = new Node<Integer>(5);
tree.printKDistant(tree.root, 2);
}
void getMaxWidth() {
BinaryTree<Integer> tree = new BinaryTree<>();
tree.root = new Node<Integer>(1);
tree.root.left = new Node<Integer>(2);
tree.root.right = new Node<Integer>(3);
tree.root.left.left = new Node<Integer>(4);
tree.root.left.right = new Node<Integer>(5);
int h = tree.height(tree.root);
int count[] = new int[h];
int level = 0;
tree.getMaxWidth(tree.root, count, level);
pout.println("level width");
for (int i = 0; i < h; i++)
pout.println(i + " " + count[i]);
}
void tree_diameter() {
BinaryTree<Integer> tree = new BinaryTree<>();
tree.root = new Node<Integer>(1);
tree.root.left = new Node<Integer>(2);
tree.root.right = new Node<Integer>(3);
tree.root.left.left = new Node<Integer>(4);
tree.root.left.right = new Node<Integer>(5);
pout.println("Diameter is (not optimized):");
pout.println(tree.diameter(tree.root));
Height h = new Height();
pout.println("Diameter is (optimized) :");
pout.println(tree.diameterOpt(tree.root, h));
}
void tree_traversal() {
BinaryTree<Integer> tree = new BinaryTree<>();
tree.root = new Node<Integer>(1);
tree.root.left = new Node<Integer>(2);
tree.root.right = new Node<Integer>(3);
tree.root.left.left = new Node<Integer>(4);
tree.root.left.right = new Node<Integer>(5);
pout.println("Preorder traversal of binary tree is ");
tree.printPreorder(tree.root);
pout.println("\nInorder traversal of binary tree is ");
tree.printInorder(tree.root);
pout.println("\nPostorder traversal of binary tree is ");
tree.printPostorder(tree.root);
pout.println("\nLevel order traversal of binary tree is ");
tree.printLevelorder(tree.root);
pout.println("\nInorder using stack (not recursive) is ");
tree.printInorderNotRec(tree.root);
pout.println("\nInorder using morris traversal (not recursive or stack) is ");
tree.morrisTraversal(tree.root);
}
class Node<T> {
T key;
Node<T> left, right;
public Node(T item) {
key = item;
left = right = null;
}
}
static int preIndex = 0;
class BinaryTree<T> {
Node<T> root;
public BinaryTree() {
root = null;
}
Node<T> flattenTreeHelper(Node<T> root){
if(root == null)
return root;
if (root.left != null) {
Node<T> rChild = root.right;
root.right = root.left;
root.left = null;
Node<T> rMost = root.right;
while (rMost.right != null) {
rMost = rMost.right;
}
rMost.right = rChild;
}
flattenTreeHelper(root.right);
return root;
}
void flattenTree(Node<T> root){
Node<T> temp = flattenTreeHelper(root);
printFlattenTree(temp);
}
void printFlattenTree(Node<T> node){
pout.println("Flattened tree : ");
while(node!=null){
pout.print(node.key+" ");
node = node.right;
}
pout.println();
}
Node<Character> buildTree(char in[], char pre[], int inStrt, int inEnd) {
if (inStrt > inEnd)
return null;
/*
* Pick current node from Preorder traversal using preIndex and
* increment preIndex
*/
Node<Character> tNode = new Node<>(pre[preIndex++]);
/* If this node has no children then return */
if (inStrt == inEnd)
return tNode;
/* Else find the index of this node in Inorder traversal */
int inIndex = search(in, inStrt, inEnd, tNode.key);
/*
* Using index in Inorder traversal, construct left and right
* subtress
*/
tNode.left = buildTree(in, pre, inStrt, inIndex - 1);
tNode.right = buildTree(in, pre, inIndex + 1, inEnd);
return tNode;
}
int search(char arr[], int strt, int end, char value) {
int i;
for (i = strt; i <= end; i++) {
if (arr[i] == value)
return i;
}
return i;
}
boolean areIdentical(Node<T> node1, Node<T> node2) {
if (node1 == null && node2 == null)
return true;
if (node1 == null || node2 == null)
return false;
if (node1.key == node2.key && areIdentical(node1.left, node2.left)
&& areIdentical(node1.right, node2.right))
return true;
return false;
}
boolean isSubTree(Node<T> root1, Node<T> root2) {
if (root2 == null)
return true;
if (root1 == null)
return false;
if (areIdentical(root1, root2))
return true;
return isSubTree(root1.left, root2) || isSubTree(root1.right, root2);
}
boolean printAncestors(Node<T> node, T dest) {
if (node == null)
return false;
if (node.key == dest)
return true;
if (printAncestors(node.left, dest) || printAncestors(node.right, dest)) {
pout.print(node.key + " ");
return true;
}
return false;
}
void printKDistant(Node<T> node, int k) {
if (node == null)
return;
if (k == 0) {
pout.print(node.key + " ");
return;
} else {
printKDistant(node.left, k - 1);
printKDistant(node.right, k - 1);
}
}
void getMaxWidth(Node<T> node, int count[], int level) {
if (node == null)
return;
count[level]++;
getMaxWidth(node.left, count, level + 1);
getMaxWidth(node.right, count, level + 1);
}
// The diameter of a tree T is the largest of the following quantities:
// (Refer : GeeksForGeeks)
// * the diameter of Ts left subtree
// * the diameter of Ts right subtree
// * the longest path between leaves that goes through the root of T
// (this can be computed from the heights of the subtrees of T)
// O(N^2) => Not optimized
int diameter(Node<T> root) {
if (root == null)
return 0;
int leftheight = height(root.left);
int rightheight = height(root.right);
int leftdiameter = diameter(root.left);
int rightdiameter = diameter(root.right);
return Math.max(Math.max(leftdiameter, rightdiameter), leftheight + rightheight + 1);
}
// O(N) => Optimized
int diameterOpt(Node<T> root, Height height) {
/*
* lh --> Height of left subtree rh --> Height of right subtree
*/
Height lh = new Height(), rh = new Height();
if (root == null) {
height.h = 0;
return 0; // diameter is also 0
}
/*
* Get the heights of left and right subtrees in lh and rh And store
* the returned values in ldiameter and ldiameter
*/
lh.h++;
rh.h++;
int ldiameter = diameterOpt(root.left, lh);
int rdiameter = diameterOpt(root.right, rh);
/*
* Height of current node is max of heights of left and right
* subtrees plus 1
*/
height.h = Math.max(lh.h, rh.h) + 1;
return Math.max(lh.h + rh.h + 1, Math.max(ldiameter, rdiameter));
}
// height of tree
int height(Node<T> node) {
if (node == null)
return 0;
return 1 + Math.max(height(node.left), height(node.right));
}
// TODO : Awesome algo (revise again)
// Inorder without using recursion or stack
void morrisTraversal(Node<T> root) {
if (root == null)
return;
Node<T> current = root;
while (current != null) {
if (current.left == null) {
pout.print(current.key + " ");
current = current.right;
} else {
Node<T> predecessor = current.left;
// Moving to rightmost child of left subtree
while (predecessor.right != null && predecessor.right != current)
predecessor = predecessor.right;
// Threading BT: make current as right child of its inorder
// predecessor
if (predecessor.right == null) {
predecessor.right = current;
current = current.left;
}
// else revert changes made to original tree
else {
predecessor.right = null;
pout.print(current.key + " ");
current = current.right;
}
}
}
}
void printInorderNotRec(Node<T> root) {
if (root == null)
return;
Stack<Node<T>> st = new Stack<>();
Node<T> node = root;
while (node != null) {
st.push(node);
node = node.left;
}
while (st.size() > 0) {
node = st.pop();
pout.print(node.key + " ");
if (node.right != null) {
node = node.right;
while (node != null) {
st.push(node);
node = node.left;
}
}
}
}
void printInorder(Node<T> node) {
if (node == null)
return;
printInorder(node.left);
pout.print(node.key + " ");
printInorder(node.right);
}
void printPostorder(Node<T> node) {
if (node == null)
return;
printPostorder(node.left);
printPostorder(node.right);
pout.print(node.key + " ");
}
void printPreorder(Node<T> node) {
if (node == null)
return;
pout.print(node.key + " ");
printPreorder(node.left);
printPreorder(node.right);
}
// using queue complexity => O(N)
void printLevelorder(Node<T> node) {
Queue<Node<T>> q = new LinkedList<>();
q.add(node);
while (!q.isEmpty()) {
Node<T> temp = q.poll();
pout.print(temp.key + " ");
if (temp.left != null)
q.add(temp.left);
if (temp.right != null)
q.add(temp.right);
}
}
}
class Height {
int h;
}
}