-
Notifications
You must be signed in to change notification settings - Fork 17
/
UnmodifiableNode.java
98 lines (84 loc) · 2.48 KB
/
UnmodifiableNode.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
package by.andd3dfx.recursion;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
/**
* 1. Modify given class Node to make it immutable:
*
* <pre>
* public final class Node {
* private final List<Node> children;
* private final Integer value;
* }
* </pre>
* <p>
* 2. Provide name of this data structure
* <p>
* 3. Add sum() method to sum values of all items in this structure
* <ul>
* <li>recursive way
* <li>non-recursive way
* </ul>
*
* @see <a href="https://youtu.be/UaeleszV00w">Video solution</a>
*/
public final class UnmodifiableNode {
private final List<UnmodifiableNode> children = new ArrayList<>();
private final Integer value;
public UnmodifiableNode(Integer value) {
this.value = value;
}
public UnmodifiableNode(List<UnmodifiableNode> children, Integer value) {
if (children != null) {
this.children.addAll(children);
}
this.value = value;
}
public Integer getValue() {
return value;
}
public List<UnmodifiableNode> getChildren() {
return new ArrayList<>(children);
}
public int sumRecursive() {
return sumRecursive(this);
}
private int sumRecursive(UnmodifiableNode node) {
var result = node.value;
for (UnmodifiableNode child : node.getChildren()) {
result += sumRecursive(child);
}
return result;
}
public int sumRecursive2() {
return flattennedStream()
.map(UnmodifiableNode::getValue)
.reduce(0, (a, b) -> a + b);
}
private Stream<UnmodifiableNode> flattennedStream() {
return Stream.concat(
Stream.of(this),
children.stream().flatMap(UnmodifiableNode::flattennedStream)
);
}
public int sumNonRecursive() {
var result = value;
var stack = new ArrayDeque<List<UnmodifiableNode>>();
stack.add(children);
while (!stack.isEmpty()) {
for (var node : stack.pop()) {
result += node.value;
stack.add(node.children);
}
}
return result;
}
@Override
public String toString() {
if (children.isEmpty()) {
return "{%d}".formatted(value);
}
return "{%d, %s}".formatted(value, children);
}
}