@Test void trace() { List<Node<String>> trace = NODE.trace("D"); assertNotNull(trace); assertEquals(Arrays.asList("A", "B", "C", "D"), trace.stream().map(Node::getElement).collect(Collectors.toList())); }
public @Nullable List<Node<T>> trace(T element) { if (Objects.equals(element, getElement())) { return Collections.singletonList(this); } List<Node<T>> trace = new ArrayList<>(); for (Node<T> child : getChildren()) { @Nullable List<Node<T>> result = child.trace(element); if (result != null) { trace.add(this); trace.addAll(result); return trace; } } return null; }
Node { public @Nullable List<Node<T>> trace(T element) { if (Objects.equals(element, getElement())) { return Collections.singletonList(this); } List<Node<T>> trace = new ArrayList<>(); for (Node<T> child : getChildren()) { @Nullable List<Node<T>> result = child.trace(element); if (result != null) { trace.add(this); trace.addAll(result); return trace; } } return null; } }
Node { public @Nullable List<Node<T>> trace(T element) { if (Objects.equals(element, getElement())) { return Collections.singletonList(this); } List<Node<T>> trace = new ArrayList<>(); for (Node<T> child : getChildren()) { @Nullable List<Node<T>> result = child.trace(element); if (result != null) { trace.add(this); trace.addAll(result); return trace; } } return null; } Node(T element); }
Node { public @Nullable List<Node<T>> trace(T element) { if (Objects.equals(element, getElement())) { return Collections.singletonList(this); } List<Node<T>> trace = new ArrayList<>(); for (Node<T> child : getChildren()) { @Nullable List<Node<T>> result = child.trace(element); if (result != null) { trace.add(this); trace.addAll(result); return trace; } } return null; } Node(T element); Set<T> collectLeafs(Predicate<T> filter); @Nullable Node<T> find(T element); @Nullable List<Node<T>> trace(T element); Node<T> add(Node<T> node); void add(Collection<Node<T>> nodes); boolean isEmpty(); Set<Node<T>> getChildren(); T getElement(); @Override String toString(); }
Node { public @Nullable List<Node<T>> trace(T element) { if (Objects.equals(element, getElement())) { return Collections.singletonList(this); } List<Node<T>> trace = new ArrayList<>(); for (Node<T> child : getChildren()) { @Nullable List<Node<T>> result = child.trace(element); if (result != null) { trace.add(this); trace.addAll(result); return trace; } } return null; } Node(T element); Set<T> collectLeafs(Predicate<T> filter); @Nullable Node<T> find(T element); @Nullable List<Node<T>> trace(T element); Node<T> add(Node<T> node); void add(Collection<Node<T>> nodes); boolean isEmpty(); Set<Node<T>> getChildren(); T getElement(); @Override String toString();  }