-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathGenericTree.java
174 lines (128 loc) Β· 3.19 KB
/
GenericTree.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
package section23_GenericTree;
import java.util.ArrayList;
import java.util.Scanner;
public class GenericTree {
Scanner sc = new Scanner(System.in);
private class Node {
int data;
ArrayList<Node> children = new ArrayList<>();
}
private Node root;
public GenericTree() {
int childIndex = -1;
Node rootParent = null;
this.root = construct(rootParent, childIndex);
}
private Node construct(Node parent, int ith) {
// root node
if (parent == null) {
System.out.println("Enter the data for root node?");
} else {
// prompt
System.out.println("Enter the data for " + ith + "th child of " + parent.data + "?");
}
// take node data
int item = sc.nextInt();
// create Node
Node newNode = new Node();
newNode.data = item;
// prompt
System.out.println("Enter the no. of children of " + item + "?");
int totalChild = sc.nextInt();
// create all children of newNode
for (int childIdx = 0; childIdx < totalChild; childIdx++) {
Node child = construct(newNode, childIdx);
newNode.children.add(child);
}
return newNode;
}
public void display() {
System.out.println("------------------------------------------\n");
this.display(this.root);
System.out.println("------------------------------------------\n");
}
private void display(Node node) {
// self-work
String ans = node.data + " => ";
// loop childrens of current node
for (Node child : node.children) {
ans += child.data + " ";
}
ans += " . ";
System.out.println(ans);
// do the work for smaller problems
for (Node child : node.children) {
display(child);
}
}
public int size() {
return size(root);
}
private int size(Node node) {
int totalSize = 0;
// count total children of current node
for (Node child : node.children) {
int childSize = size(child);
totalSize += childSize;
}
return totalSize + 1; // +1 for parent node
}
public int max() {
return max(root);
}
/*
* private int max(Node node) {
*
* int currentMax = Integer.MIN_VALUE; int maxChild = Integer.MIN_VALUE;
*
* for (Node child : node.children) { maxChild = max(child); }
*
* if (maxChild > currentMax) { currentMax = maxChild; }
*
* return Math.max(currentMax, node.data); }
*/
private int max(Node node) {
int totalMax = node.data;
for (Node child : node.children) {
int childMax = max(child);
if (childMax > totalMax) {
totalMax = childMax;
}
}
return totalMax;
}
public boolean find(int item) {
return find(root, item);
}
private boolean find(Node node, int item) {
if (node.data == item) {
return true;
}
for (Node child : node.children) {
boolean childResult = find(child, item);
if (childResult) {
return true;
}
}
return false;
}
public int height() {
return height(root);
}
private int height(Node node) {
// leaf node
if (node.children.size() == 0) {
return 0;
}
// can remove base case by initializing maxHeight by -1
int maxHeight = 0;
for (Node child : node.children) {
int currChildHt = height(child);
if (currChildHt > maxHeight) {
maxHeight = currChildHt;
}
}
// +1 for height from root to max height child, level 0 to level 1
return maxHeight + 1;
}
}