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
|
package bjc.data;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import bjc.funcdata.FunctionalList;
import bjc.funcdata.IList;
import bjc.funcdata.bst.TreeLinearizationMethod;
/**
* A node in a homogeneous tree.
*
* @author ben
*
* @param <ContainedType>
* The type contained in the tree.
*/
public class Tree<ContainedType> implements ITree<ContainedType> {
/* The data/label for this node. */
private ContainedType data;
/* The children of this node. */
private IList<ITree<ContainedType>> children;
/* Whether this node has children. */
/*
* @NOTE Why have both this boolean and childCount? Why not just do a
* childCount == 0 whenever you'd check hasChildren?
* - Because hasChildren is set once and not reset, and really what
* it indicates is that children has been allocated.
*/
private boolean hasChildren;
/* The number of children this node has. */
private int childCount = 0;
/* The ID of this node. */
private int ID;
/* The next ID to assign to a node. */
private static int nextID = 0;
/**
* Create a new leaf node in a tree.
*/
public Tree() {
this(null);
}
/**
* Create a new leaf node in a tree.
*
* @param leaf
* The data to store as a leaf node.
*/
public Tree(final ContainedType leaf) {
data = leaf;
hasChildren = false;
ID = nextID++;
}
/**
* Create a new tree node with the specified children.
*
* @param leaf
* The data to hold in this node.
*
* @param childrn
* A list of children for this node.
*/
public Tree(final ContainedType leaf, final IList<ITree<ContainedType>> childrn) {
this(leaf);
hasChildren = true;
childCount = childrn.getSize();
children = childrn;
}
/**
* Create a new tree node with the specified children.
*
* @param leaf
* The data to hold in this node.
*
* @param childrn
* A list of children for this node.
*/
@SafeVarargs
public Tree(final ContainedType leaf, final ITree<ContainedType>... childrn) {
this(leaf);
hasChildren = true;
childCount = 0;
children = new FunctionalList<>();
for(final ITree<ContainedType> child : childrn) {
children.add(child);
childCount++;
}
}
@Override
public void addChild(final ContainedType child) {
addChild(new Tree<>(child));
}
@Override
public void addChild(final ITree<ContainedType> child) {
if(hasChildren == false) {
hasChildren = true;
children = new FunctionalList<>();
}
childCount++;
children.add(child);
}
@Override
public void prependChild(final ITree<ContainedType> child) {
if(hasChildren == false) {
hasChildren = true;
children = new FunctionalList<>();
}
childCount++;
children.prepend(child);
}
@Override
public void doForChildren(final Consumer<ITree<ContainedType>> action) {
if(childCount > 0) {
children.forEach(action);
}
}
@Override
public int getChildrenCount() {
return childCount;
}
@Override
public int revFind(final Predicate<ITree<ContainedType>> childPred) {
if(childCount == 0) {
return -1;
}
for(int i = childCount - 1; i >= 0; i--) {
if(childPred.test(getChild(i))) return i;
}
return -1;
}
@Override
public void traverse(final TreeLinearizationMethod linearizationMethod, final Consumer<ContainedType> action) {
if(hasChildren) {
switch(linearizationMethod) {
case INORDER:
if(childCount != 2) {
final String msg = "Can only do in-order traversal for binary trees.";
throw new IllegalArgumentException(msg);
}
children.getByIndex(0).traverse(linearizationMethod, action);
action.accept(data);
children.getByIndex(1).traverse(linearizationMethod, action);
break;
case POSTORDER:
children.forEach((child) -> child.traverse(linearizationMethod, action));
action.accept(data);
break;
case PREORDER:
action.accept(data);
children.forEach((child) -> child.traverse(linearizationMethod, action));
break;
default:
break;
}
} else {
action.accept(data);
}
}
@Override
public <NewType, ReturnedType> ReturnedType collapse(final Function<ContainedType, NewType> leafTransform,
final BiFunction<ContainedType, IList<NewType>, NewType> nodeCollapser,
final Function<NewType, ReturnedType> resultTransformer) {
return resultTransformer.apply(internalCollapse(leafTransform, nodeCollapser));
}
@Override
public ITree<ContainedType> flatMapTree(final Function<ContainedType, ITree<ContainedType>> mapper) {
if(hasChildren) {
final ITree<ContainedType> flatMappedData = mapper.apply(data);
final IList<ITree<ContainedType>> mappedChildren = children
.map(child -> child.flatMapTree(mapper));
mappedChildren.forEach(flatMappedData::addChild);
return flatMappedData;
}
return mapper.apply(data);
}
/*
* Do a collapse of this tree.
*
* @NOTE Why is this protected? I can't see any good reason someone'd
* want to override it.
*/
protected <NewType> NewType internalCollapse(final Function<ContainedType, NewType> leafTransform,
final BiFunction<ContainedType, IList<NewType>, NewType> nodeCollapser) {
if(hasChildren) {
final IList<NewType> collapsedChildren = children.map(child -> {
final NewType collapsed = child.collapse(leafTransform, nodeCollapser,
subTreeVal -> subTreeVal);
return collapsed;
});
return nodeCollapser.apply(data, collapsedChildren);
}
return leafTransform.apply(data);
}
protected void internalToString(final StringBuilder builder, final int indentLevel, final boolean initial) {
if(!initial) {
for(int i = 0; i < indentLevel; i++) {
builder.append(">\t");
}
}
builder.append("Node #");
builder.append(ID);
builder.append(": ");
builder.append(data == null ? "(null)" : data.toString());
builder.append("\n");
if(hasChildren) {
children.forEach(child -> {
if(child instanceof Tree<?>) {
final Tree<ContainedType> kid = (Tree<ContainedType>) child;
kid.internalToString(builder, indentLevel + 1, false);
} else {
for(int i = 0; i < indentLevel + 1; i++) {
builder.append(">\t");
}
builder.append("Unknown node of type ");
builder.append(child.getClass().getName());
builder.append("\n");
}
});
}
}
@Override
public <MappedType> ITree<MappedType> rebuildTree(final Function<ContainedType, MappedType> leafTransformer,
final Function<ContainedType, MappedType> operatorTransformer) {
if(hasChildren) {
final IList<ITree<MappedType>> mappedChildren = children.map(child -> {
return child.rebuildTree(leafTransformer, operatorTransformer);
});
final MappedType mapData = operatorTransformer.apply(data);
return new Tree<>(mapData, mappedChildren);
}
return new Tree<>(leafTransformer.apply(data));
}
@Override
public void selectiveTransform(final Predicate<ContainedType> nodePicker,
final UnaryOperator<ContainedType> transformer) {
if(hasChildren) {
children.forEach(child -> child.selectiveTransform(nodePicker, transformer));
} else {
data = transformer.apply(data);
}
}
@Override
public ITree<ContainedType> topDownTransform(
final Function<ContainedType, TopDownTransformResult> transformPicker,
final UnaryOperator<ITree<ContainedType>> transformer) {
final TopDownTransformResult transformResult = transformPicker.apply(data);
switch(transformResult) {
case PASSTHROUGH:
ITree<ContainedType> result = new Tree<>(data);
if(hasChildren) {
children.forEach(child -> {
final ITree<ContainedType> kid = child.topDownTransform(transformPicker,
transformer);
result.addChild(kid);
});
}
return result;
case SKIP:
return this;
case TRANSFORM:
return transformer.apply(this);
case RTRANSFORM:
return transformer.apply(this).topDownTransform(transformPicker, transformer);
case PUSHDOWN:
result = new Tree<>(data);
if(hasChildren) {
children.forEach(child -> {
final ITree<ContainedType> kid = child.topDownTransform(transformPicker,
transformer);
result.addChild(kid);
});
}
return transformer.apply(result);
case PULLUP:
final ITree<ContainedType> intermediateResult = transformer.apply(this);
result = new Tree<>(intermediateResult.getHead());
intermediateResult.doForChildren(child -> {
final ITree<ContainedType> kid = child.topDownTransform(transformPicker, transformer);
result.addChild(kid);
});
return result;
default:
final String msg = String.format("Recieved unknown transform result type %s", transformResult);
throw new IllegalArgumentException(msg);
}
}
@Override
public <TransformedType> TransformedType transformChild(final int childNo,
final Function<ITree<ContainedType>, TransformedType> transformer) {
if(childNo < 0 || childNo > childCount - 1) {
final String msg = String.format("Child index #%d is invalid", childNo);
throw new IllegalArgumentException(msg);
}
final ITree<ContainedType> selectedKid = children.getByIndex(childNo);
return transformer.apply(selectedKid);
}
@Override
public <TransformedType> TransformedType transformHead(
final Function<ContainedType, TransformedType> transformer) {
return transformer.apply(data);
}
@Override
public void setHead(ContainedType dat) {
this.data = dat;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + childCount;
result = prime * result + (children == null ? 0 : children.hashCode());
result = prime * result + (data == null ? 0 : data.hashCode());
return result;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
internalToString(builder, 1, true);
/* Delete a trailing nl. */
builder.deleteCharAt(builder.length() - 1);
return builder.toString();
}
@Override
public boolean equals(final Object obj) {
if(this == obj) return true;
if(obj == null) return false;
if(!(obj instanceof Tree<?>)) return false;
final Tree<?> other = (Tree<?>) obj;
if(data == null) {
if(other.data != null) return false;
} else if(!data.equals(other.data)) return false;
if(childCount != other.childCount) return false;
if(children == null) {
if(other.children != null) return false;
} else if(!children.equals(other.children)) return false;
return true;
}
}
|