From a16c9e3c901ecbae22ad9fced38b8bd90c5ddef5 Mon Sep 17 00:00:00 2001 From: xzero Date: Sun, 31 Oct 2021 20:30:43 +0100 Subject: [PATCH 1/2] Add DepthFirstSearch with Java Streams Add simple DepthFirstSearch with Java Streams --- Searches/DepthFirstSearch.java | 86 ++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Searches/DepthFirstSearch.java diff --git a/Searches/DepthFirstSearch.java b/Searches/DepthFirstSearch.java new file mode 100644 index 000000000000..f90dc495bcf8 --- /dev/null +++ b/Searches/DepthFirstSearch.java @@ -0,0 +1,86 @@ +package Searches; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +public class DepthFirstSearch { + static class Node { + private final String name; + private final List subNodes; + + public Node(final String name) { + this.name = name; + this.subNodes = new ArrayList<>(); + } + + public Node(final String name, final List subNodes) { + this.name = name; + this.subNodes = subNodes; + } + + public String getName() { + return name; + } + + public List getSubNodes() { + return subNodes; + } + } + + public static Optional search(final Node node, final String name) { + if (node.getName().equals(name)) { + return Optional.of(node); + } + + return node.getSubNodes() + .stream() + .map(value -> search(value, name)) + .flatMap(Optional::stream) + .findAny(); + } + + public static void assertThat(final Object actual, final Object expected) { + if (!Objects.equals(actual, expected)) { + throw new AssertionError(String.format("expected=%s but was actual=%s", expected, actual)); + } + } + + public static void main(final String[] args) { + final Node rootNode = new Node("A", List.of( + new Node("B", List.of(new Node("D"), new Node("F", List.of( + new Node("H"), new Node("I") + )))), + new Node("C", List.of(new Node("G"))), + new Node("E") + )); + + { + final String expected = "I"; + + final Node result = search(rootNode, expected) + .orElseThrow(() -> new AssertionError("Node not found!")); + + assertThat(result.getName(), expected); + } + + { + final String expected = "G"; + + final Node result = search(rootNode, expected) + .orElseThrow(() -> new AssertionError("Node not found!")); + + assertThat(result.getName(), expected); + } + + { + final String expected = "E"; + + final Node result = search(rootNode, expected) + .orElseThrow(() -> new AssertionError("Node not found!")); + + assertThat(result.getName(), expected); + } + } +} From e0e233f451c4aae1d866246f5e50d30e8fdc73d6 Mon Sep 17 00:00:00 2001 From: xzero Date: Sun, 31 Oct 2021 20:33:45 +0100 Subject: [PATCH 2/2] Add author name and date --- Searches/DepthFirstSearch.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Searches/DepthFirstSearch.java b/Searches/DepthFirstSearch.java index f90dc495bcf8..bca2685cf625 100644 --- a/Searches/DepthFirstSearch.java +++ b/Searches/DepthFirstSearch.java @@ -5,6 +5,10 @@ import java.util.Objects; import java.util.Optional; +/** + * @author: caos321 + * @date: 31 October 2021 (Sunday) + */ public class DepthFirstSearch { static class Node { private final String name;