Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

G2-1509 Support DFS with parent reporting #363

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.function.Predicate;
import java.util.stream.Stream;

import com.g2forge.alexandria.java.adt.tuple.ITuple2G_;
import com.g2forge.alexandria.java.adt.tuple.implementations.Tuple2G_I;
import com.g2forge.alexandria.java.core.marker.Helpers;

import lombok.experimental.UtilityClass;
Expand All @@ -19,6 +21,12 @@ public static <N> Stream<N> dfs(N node, final Function<? super N, ? extends Coll
else return children.stream().map(child -> dfs(child, getChildren, postorder)).reduce(Stream.of(node), postorder ? (s0, s1) -> Stream.concat(s1, s0) : Stream::concat);
}

public static <N> Stream<ITuple2G_<N, N>> dfsWithParent(N parent, N node, final Function<? super N, ? extends Collection<? extends N>> getChildren, boolean postorder) {
final Collection<? extends N> children = getChildren.apply(node);
if ((children == null) || children.isEmpty()) return Stream.of(new Tuple2G_I<>(parent, node));
else return children.stream().map(child -> dfsWithParent(node, child, getChildren, postorder)).reduce(Stream.of(new Tuple2G_I<>(parent, node)), postorder ? (s0, s1) -> Stream.concat(s1, s0) : Stream::concat);
}

public static <N> Optional<N> find(N node, final Function<? super N, ? extends Collection<? extends N>> getChildren, Predicate<? super N> find) {
if (find.test(node)) return Optional.of(node);
for (N child : getChildren.apply(node)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.g2forge.alexandria.java.core.helpers;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.Assert;
import org.junit.Test;

import com.g2forge.alexandria.java.adt.name.IStringNamed;
import com.g2forge.alexandria.java.adt.tuple.ITuple2G_;
import com.g2forge.alexandria.java.adt.tuple.implementations.Tuple2G_I;

import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.Singular;

public class TestHTree {
@Data
@Builder(toBuilder = true)
@RequiredArgsConstructor
protected static class Node implements IStringNamed {
public static NodeBuilder named(String name) {
return builder().name(name);
}

protected final String name;

@Singular
protected final List<Node> children;
}

protected void assertDFS(final Node root, final List<Node> expected, final boolean postorder) {
Assert.assertEquals(expected, HTree.dfs(root, Node::getChildren, postorder).collect(Collectors.toList()));
}

protected void assertDFSWithParent(final Node root, final List<ITuple2G_<Node, Node>> expected, final boolean postorder) {
Assert.assertEquals(expected, HTree.dfsWithParent(null, root, Node::getChildren, postorder).collect(Collectors.toList()));
}

@Test
public void dfsChild() {
final Node b, a = Node.named("a").child(b = Node.named("b").build()).build();
assertDFS(a, HCollection.asList(a, b), false);
}

@Test
public void dfsChildren() {
final Node c, b, a = Node.named("a").child(b = Node.named("b").build()).child(c = Node.named("c").build()).build();
assertDFS(a, HCollection.asList(a, b, c), false);
assertDFS(a, HCollection.asList(c, b, a), true);
}

@Test
public void dfsGrandchildren() {
final Node d, c, b, a = Node.named("a").child(b = Node.named("b").child(c = Node.named("c").build()).child(d = Node.named("d").build()).build()).build();
assertDFS(a, HCollection.asList(a, b, c, d), false);
assertDFS(a, HCollection.asList(d, c, b, a), true);
}

@Test
public void dfsSingle() {
final Node a = Node.named("a").build();
assertDFS(a, HCollection.asList(a), false);
}

@Test
public void dfsWithParentChild() {
final Node b, a = Node.named("a").child(b = Node.named("b").build()).build();
assertDFSWithParent(a, HCollection.asList(new Tuple2G_I<>(null, a), new Tuple2G_I<>(a, b)), false);
}

@Test
public void dfsWithParentChildren() {
final Node c, b, a = Node.named("a").child(b = Node.named("b").build()).child(c = Node.named("c").build()).build();
assertDFSWithParent(a, HCollection.asList(new Tuple2G_I<>(null, a), new Tuple2G_I<>(a, b), new Tuple2G_I<>(a, c)), false);
assertDFSWithParent(a, HCollection.asList(new Tuple2G_I<>(a, c), new Tuple2G_I<>(a, b), new Tuple2G_I<>(null, a)), true);
}

@Test
public void dfsWithParentGrandchild() {
final Node d, c, b, a = Node.named("a").child(b = Node.named("b").child(c = Node.named("c").build()).child(d = Node.named("d").build()).build()).build();
assertDFSWithParent(a, HCollection.asList(new Tuple2G_I<>(null, a), new Tuple2G_I<>(a, b), new Tuple2G_I<>(b, c), new Tuple2G_I<>(b, d)), false);
assertDFSWithParent(a, HCollection.asList(new Tuple2G_I<>(b, d), new Tuple2G_I<>(b, c), new Tuple2G_I<>(a, b), new Tuple2G_I<>(null, a)), true);
}

@Test
public void dfsWithParentSingle() {
final Node a = Node.named("a").build();
assertDFSWithParent(a, HCollection.asList(new Tuple2G_I<>(null, a)), false);
}
}