forked from xiaoningning/java-algorithm-2010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboldTree.java
More file actions
233 lines (195 loc) · 6.59 KB
/
boldTree.java
File metadata and controls
233 lines (195 loc) · 6.59 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
import java.util.*;
public class boldTree {
private int number;
private boolean isBold;
private List<boldTree> children;
private boolean keep;
public boldTree(int n, boolean bold) {
number = n;
isBold = bold;
keep = true;
children = new ArrayList<boldTree>();
}
public static boldTree copy(boldTree node){
return new boldTree(node.number, node.isBold);
}
public void addChild(boldTree node) {
children.add(node);
}
public List<boldTree> getChildren() {
return children;
}
public boldTree getChild(int n) {
for (boldTree c : this.children) {
if (c.number == n)
return c;
}
return null;
}
public void removeChild(boldTree c) {
if (this.children.contains(c))
this.children.remove(c);
}
//the best, bottom-up by copying
public static boldTree boldSubTreeCopy(boldTree root) {
if (root == null)
return null;
boldTree tmp = copy(root);
for(boldTree c : root.getChildren()){
if(boldSubTreeCopy(c) != null){
tmp.addChild(c);
}
}
if(tmp.isBold ||tmp.getChildren().size() != 0)
return tmp;
else
return null;
}
// use flag, bottom-up
public static boldTree boldSubTree(boldTree root) {
if (root == null)
return null;
root.keep = root.isBold;
for(boldTree c : root.getChildren()){
if(boldSubTree(c).keep)
root.keep = true;
}
return root;
}
public static void show2(boldTree n) {
if (n == null){
System.out.println("no tree");
return;
}
Queue<boldTree> currentLevel = new LinkedList<boldTree>();
Queue<boldTree> nextLevel = new LinkedList<boldTree>();
currentLevel.add(n);
System.out.println(n.number + " ");
while (!currentLevel.isEmpty()) {
boldTree r = currentLevel.poll();
for (boldTree c : r.getChildren()) {
if (c.keep) {
if (c.isBold)
System.out.print(c.number + "(b) ");
else
System.out.print(c.number + " ");
}
nextLevel.add(c);
}
if (currentLevel.isEmpty()) {
currentLevel.addAll( nextLevel);
nextLevel.clear();
System.out.println();
}
}
}
// track non-bold node and remove, top-down approach
public static boldTree extractBoldSubTree(boldTree root) {
if (root == null) {
return null;
}
Queue<boldTree> currentLevel = new LinkedList<boldTree>();
Queue<boldTree> nextLevel = new LinkedList<boldTree>();
HashMap<boldTree, List<boldTree>> nonBoldNodeList = new HashMap<boldTree, List<boldTree>>();
currentLevel.add(root);
while (!currentLevel.isEmpty()) {
boldTree r = currentLevel.poll();
for (boldTree c : r.getChildren()) {
if (!hasBoldChild(c)) {
if (!nonBoldNodeList.containsKey(r))
nonBoldNodeList.put(r, new ArrayList<boldTree>());
nonBoldNodeList.get(r).add(c);
} else {
nextLevel.add(c);
}
}
if (currentLevel.isEmpty()) {
currentLevel.addAll(nextLevel);
nextLevel.clear();
for (boldTree k : nonBoldNodeList.keySet()) {
for (boldTree c : nonBoldNodeList.get(k)) {
k.removeChild(c);
}
}
nonBoldNodeList.clear();
}
}
return root;
}
public static boolean hasBoldChild(boldTree n) {
if(n == null)
return false;
for(boldTree c : n.getChildren()){
if(hasBoldChild(c))
return true;
}
return n.isBold;
}
public static void show(boldTree n) {
if (n == null){
System.out.println("no tree");
return;
}
Queue<boldTree> currentLevel = new LinkedList<boldTree>();
Queue<boldTree> nextLevel = new LinkedList<boldTree>();
currentLevel.add(n);
System.out.println(n.number + " ");
while (!currentLevel.isEmpty()) {
boldTree r = currentLevel.poll();
for (boldTree c : r.getChildren()) {
if (c.isBold)
System.out.print(c.number + "(b) ");
else
System.out.print(c.number + " ");
nextLevel.add(c);
}
if (currentLevel.isEmpty()) {
currentLevel = nextLevel;
nextLevel = new LinkedList<boldTree>();
System.out.println();
}
}
}
public static void main(String[] args) {
boldTree root = new boldTree(1, false);
boldTree c2 = new boldTree(2, false);
boldTree c3 = new boldTree(3, false);
boldTree c4 = new boldTree(4, true);
root.addChild(c2);
root.addChild(c3);
root.addChild(c4);
boldTree c22 = new boldTree(22, true);
c2.addChild(c22);
boldTree c31 = new boldTree(31, false);
boldTree c32 = new boldTree(32, true);
c3.addChild(c31);
c3.addChild(c32);
boldTree c41 = new boldTree(41, false);
boldTree c42 = new boldTree(42, false);
boldTree c43 = new boldTree(43, false);
c4.addChild(c41);
c4.addChild(c42);
c4.addChild(c43);
boldTree c221 = new boldTree(221, false);
c22.addChild(c221);
boldTree c311 = new boldTree(311, true);
c31.addChild(c311);
boldTree c421 = new boldTree(421, true);
boldTree c431 = new boldTree(431, false);
c42.addChild(c421);
c43.addChild(c431);
boldTree root2 = root;
System.out.println("source tree");
show(root);
System.out.println("bold tree");
boldTree newRoot = extractBoldSubTree(root);
show(newRoot);
// need a flag to keep if the node should be included.
System.out.println("another way to extract bold tree");
boldTree newRoot2 = boldSubTree(root2);
show2(newRoot2);
System.out.println("another way to extract bold tree by copying: ");
boldTree newRoot3 = boldSubTreeCopy(root2);
show(newRoot3);
}
}