-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericTree.java
More file actions
520 lines (400 loc) · 15.4 KB
/
GenericTree.java
File metadata and controls
520 lines (400 loc) · 15.4 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
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
517
518
519
520
import java.util.*;
import javax.sound.sampled.SourceDataLine;
public class GenericTree{
private static class Node{
int data;
ArrayList<Node> children = new ArrayList<>();
}
//d(10) --> 10 will print itself and its family
//d(20), d(30), d(40) will print themselves and their families
//d(10) = self(10) + d(20) + d(20) + d(30) + d(40)
public static void display(Node node){
String str = node.data + " --> ";
for(Node child: node.children){
str +=child.data + ", ";
}
str +=".";
System.out.println(str);
for(Node child: node.children){
display(child);
}
}
// public static int size; //part of multisolver
// public static int min;
// public static int max;
// public static int height;
public static Node predecessor; //part of predecessor and successor
public static Node successor;
static int var;
public static int ceil; //SMALLEST AMONGST LARGER //part of ceil and floor
public static int floor; //LARGEST AMONGST SMALLER
public static int maximumSumNode = 0; //part of Maximum Subtree Sum
public static int maximumSum = Integer.MIN_VALUE;
public static void main(String[] args){
int[] arr={10,20,50,-1,60,-1,-1,30,70,-1,80,
110,-1,120,-1,-1,90,-1,-1,40,100,
-1,-1,-1};
Node root = null;
Stack<Node> st= new Stack<>(); //create a stack
for(int i=0; i<arr.length; i++){
if(arr[i] == -1){ //If it is a leaf node
st.pop();
}
else{
Node t= new Node();
t.data= arr[i];
if(st.size() > 0){ //checking if there is a root already in the stack
st.peek().children.add(t);
}
else{
root=t; //creating the root
}
st.push(t); //Pushing the Node in the stack
}
}
// System.out.println("Generic Tree: ");
// display(root);
// System.out.println("");
// System.out.println("Size of Generic Tree : " + size(root));
// System.out.println("");
// System.out.println("Biggest node of Generic Tree : " + max(root));
// System.out.println("");
// System.out.println("Mirror Tree: ");
// mirrorTree(root);
// display(root);
// System.out.println("");
// System.out.println("Removing Leaves: ");
// removeLeaves(root);
// display(root);
// System.out.println("");
// System.out.println("Linearize Tree: ");
// Linearize(root);
// display(root);
// System.out.println("");
// System.out.println("Linearize Tree: ");
// linear2(root);
// display(root);
// System.out.println("");
// System.out.println("Height of Generic Tree: " + height(root));
// System.out.println("");
// System.out.println("Traverse Tree: ");
// traversal(root);
// System.out.println("");
// System.out.println("Level Order Line Wise Order Tree: ");
// levelOrderLineWise(root);
// System.out.println("Find element in Tree: ");
// System.out.println(findelement(110,root));
// System.out.println("Node to root path in Tree: ");
// System.out.println(nodetoRootPath(root,110));
// System.out.println("Lowest common ancestor in Tree: ");
// System.out.println(lca(root,110,80));
// System.out.println("Distance between nodes in a tree: ");
// System.out.println(distancebetweennodes(root,110,80));
// System.out.println("Check similar tree: ");
// System.out.println(similarTree(root,root)); //generally make 2 trees but here i'm comparing with 1 only
// System.out.println("Check similar tree: ");
// System.out.println(areTreesMirrorInShape(root,root)); //generally make 2 trees but here i'm comparing with 1 only
// System.out.println("Check symmetric: ");
// System.out.println(isSymmetric(root)); //generally make 2 trees but here i'm comparing with 1 only
// Node root = construct(arr); //for multisolver question
// size = 0;
// min = INTEGER.MAX_VALUE;
// max = INTEGER.MIN_VALUE;
// height = 0;
// multisolver(root,0);
// System.out.println("Size : " + size);
// System.out.println("Min : " + min);
// System.out.println("Max : " + max);
// System.out.println("Height : " + height);
// System.out.println("Predecessor and successor: ");
// pas(root,110);
// System.out.println("Predecessor: " + predecessor.data);
// System.out.println("Successor: " + successor.data);
// ceil = Integer.MAX_VALUE;
// floor = Integer.MIN_VALUE;
// System.out.println("Ceil and floor: ");
// cnf(root,110);
// System.out.println("ceil: " + ceil);
// System.out.println("floor: " + floor);
// System.out.println("Kth Largest: ");
// System.out.println(kthlargest(root,110));
// System.out.println("Maximum sum subtree: ");
// System.out.println(maxSumSubTree(root));
}
public static int size (Node node){ //Generic Tree size
int s=0;
for(Node child : node.children){
int cs = size(child);
s = s + cs; //all nodes lying inside the main parent node
}
s = s + 1; //+1 for the main parent node
return s;
}
public static int max(Node node){ //biggest node in the tree
int max = Integer.MIN_VALUE;
for(Node child : node.children){
int cm = max(child);
max = Math.max(cm, max);
}
max= Math.max(node.data, max);
return max;
}
public static int height(Node node){ //height of generic tree
int ht = -1;
for(Node child : node.children){
int ch = height(child);
ht = Math.max(ch,ht);
}
ht+=1;
return ht;
}
public static void traversal(Node node){ //traversing a tree
//area1 euler's left, on the way deep in recursion, node's pre area
System.out.println("Node Pre: " + node.data); //euler
for (Node child : node.children){
//edge pre
System.out.println("Edge Pre: " + node.data + "--" + child.data);
traversal(child);
System.out.println("Edge Pre: " + node.data + "--" + child.data);
//edge post
}
//area2 euler's right, on the way out of recursion, node's post area
System.out.println("Node Post: " + node.data); //euler
for (Node child : node.children){
traversal(child);
}
}
public static void levelOrderLineWise(Node node){ //Level Order Line Wise
Queue<Node> mainqueue = new ArrayDeque<>();
mainqueue.add(node);
Queue<Node> childqueue = new ArrayDeque<>();
while(mainqueue.size() > 0){
node = mainqueue.remove();
System.out.println(node.data + " ");
for(Node child : node.children){
childqueue.add(child);
}
if (mainqueue.size() == 0){
mainqueue = childqueue;
childqueue = new ArrayDeque<>();
System.out.println();
}
}
}
private static void childQueue(Node child) {
}
public static void mirrorTree(Node node){ //mirror a generic tree
Stack<Node> s= new Stack<>();
for (Node child : node.children){
s.push(child);
}
node.children= new ArrayList<>();
while(!s.isEmpty()){
node.children.add(s.pop());
}
for (Node child : node.children){
mirrorTree(child);
}
}
public static void removeLeaves(Node node){ //remove leaves from a tree
for (int i= node.children.size() -1 ; i>=0 ; i--){
Node child = node.children.get(i);
if (child.children.size() == 0){
node.children.remove(child);
}
}
for (Node child : node.children){
removeLeaves(child);
}
}
public static void Linearize(Node node){ //linearize a tree
for (Node child : node.children){
Linearize(child);
}
while (node.children.size() > 1){
Node lastchild = node.children.remove(node.children.size() -1);
Node secondlast = node.children.get(node.children.size() -1);
Node c = Last(secondlast);
c.children.add(lastchild);
}
}
public static Node Last(Node node){
while (node.children.size() == 1){
node = node.children.get(0);
}
return node;
}
public static Node linear2(Node root) //efficient way of linearize tree
{
if(root.children.size() == 0)
return root;
Node ltail = linear2(root.children.get(root.children.size() - 1));
while(root.children.size() > 1)
{
Node last = root.children.remove(root.children.size() - 1);
Node sec_last = root.children.get(root.children.size() - 1);
Node tail = linear2(sec_last);
tail.children.add(last);
}
return ltail;
}
public static boolean findelement(int element, Node node){ //Find element in a tree
boolean ans = false;
for (Node child : node.children){
boolean temp = findelement(element, child);
ans = ans || temp;
}
if (node.data == element){
ans = true;
}
return ans;
}
public static ArrayList<Integer> nodetoRootPath(Node node, int data){ //node to root path
if (node.data == data){
ArrayList<Integer> list = new ArrayList<>();
list.add(node.data);
return list;
}
for(Node child: node.children){
ArrayList<Integer> pathtillchild = nodetoRootPath(child, data);
if (pathtillchild != null){
pathtillchild.add(node.data);
return pathtillchild;
}
}
return null;
}
public static int lca (Node node, int data1, int data2){ //lowest common ancestor in a tree
ArrayList<Integer> path1 = nodetoRootPath(node, data1);
ArrayList<Integer> path2 = nodetoRootPath(node, data2);
int i = path1.size() - 1, j = path2.size() - 1;
while(i >= 0 && j>=0 && path1.get(i) == path2.get(j)){
i--; j--;
}
i++; j++;
int a= path1.get(i); //or return p2.get(j)
return a;
}
public static int distancebetweennodes (Node node, int data1, int data2){ //distance between nodes
ArrayList<Integer> path1 = nodetoRootPath(node, data1);
ArrayList<Integer> path2 = nodetoRootPath(node, data2);
int i = path1.size() - 1, j = path2.size() - 1;
while(i >= 0 && j>=0 && path1.get(i) == path2.get(j)){
i--; j--;
}
i++; j++;
return i+j;
}
public static boolean similarTree(Node node1, Node node2){ //check for similar tree
if (node1.children.size() != node2.children.size()){
return false;
}
int i;
for (i = 0; i < node1.children.size(); i++){
Node a = node1.children.get(i);
Node b = node2.children.get(i);
if(similarTree (a,b) == false){
return false;
}
}
return true;
}
public static boolean areTreesMirrorInShape(Node node1, Node node2){ //check for trees mirror in shape
if (node1.children.size() != node2.children.size()){
return false;
}
int i;
for (i = 0; i < node1.children.size(); i++){
int j = node1.children.size() - 1 - i;
Node a = node1.children.get(i);
Node b = node2.children.get(j);
if(areTreesMirrorInShape(a,b) == false){
return false;
}
}
return true;
}
public static boolean isSymmetric(Node node){ //Check is tree is symmetric
return areTreesMirrorInShape(node, node);
}
// public static Node construct(int[] arr){ //used in multisolver thats why repeated
// Node root = null;
// Stack<Node> st= new Stack<>(); //create a stack
// for(int i=0; i<arr.length; i++){
// if(arr[i] == -1){ //If it is a leaf node
// st.pop();
// }
// else{
// Node t= new Node();
// t.data= arr[i];
// if(st.size() > 0){ //checking if there is a root already in the stack
// st.peek().children.add(t);
// }
// else{
// root=t; //creating the root
// }
// st.push(t); //Pushing the Node in the stack
// }
// }
// }
// public static void multisolver(Node node, int depth){
// size++;
// min = Math.min(min, node.data);
// max = Math.max(max, node.data);
// height = Math.max(height, depth);
// for (Node child : node.children){
// multisolver(child,depth+1);
// }
// }
public static void pas(Node node, int data){ //predecessor and sucessor
if (var ==0){
if(node.data == data){
var = 1;
}
else {
predecessor = node;
}
}
else if(var == 1){
successor = node;
var++;
}
for(Node child : node.children){
pas(child, data);
}
}
public static void cnf(Node node, int data){ //ceil and floor
if(node.data >= data){
ceil = Math.min(ceil, node.data);
}
else{
floor = Math.max(floor,node.data);
}
for(Node child : node.children){
cnf(child, data);
}
}
// public static int kthlargest(Node node, int k){ //kth largest element
// floor = Integer.MIN_VALUE;
// int factor = Integer.MAX_VALUE;
// for (int i = 0 ; i < k ; i++){
// cnf(node, factor); //will set floor
// factor = floor;
// floor = Integer.MIN_VALUE;
// }
// return factor;
// }
static int maxSumSubTree(Node node){ //retrun Sum and maxSumSubTree
int sum = 0;
for (Node child : node.children){
int csum=maxSumSubTree(child);
sum += csum;
}
sum += node.data;
if (sum > maximumSum){
maximumSumNode = node.data;
maximumSum = sum;
}
return sum;
}
}