mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
32
src/main/java/com/thealgorithms/datastructures/Node.java
Normal file
32
src/main/java/com/thealgorithms/datastructures/Node.java
Normal file
@ -0,0 +1,32 @@
|
||||
package com.thealgorithms.datastructures;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Node<T> {
|
||||
|
||||
private final T value;
|
||||
private final List<Node<T>> children;
|
||||
|
||||
public Node(final T value) {
|
||||
this.value = value;
|
||||
this.children = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Node(final T value, final List<Node<T>> children) {
|
||||
this.value = value;
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public T getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void addChild(Node<T> child) {
|
||||
children.add(child);
|
||||
}
|
||||
|
||||
public List<Node<T>> getChildren() {
|
||||
return children;
|
||||
}
|
||||
}
|
@ -1,32 +1,45 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import com.thealgorithms.searches.DepthFirstSearch.Node;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Optional;
|
||||
import java.util.Queue;
|
||||
import com.thealgorithms.datastructures.Node;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author: caos321
|
||||
* @date: 31 October 2021 (Sunday)
|
||||
* @wiki: https://en.wikipedia.org/wiki/Breadth-first_search
|
||||
*/
|
||||
public class BreadthFirstSearch {
|
||||
public static Optional<Node> search(final Node node, final String name) {
|
||||
if (node.getName().equals(name)) {
|
||||
public class BreadthFirstSearch<T> {
|
||||
|
||||
private final List<T> visited = new ArrayList<>();
|
||||
|
||||
public Optional<Node<T>> search(final Node<T> node, final T value) {
|
||||
if (node == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (node.getValue().equals(value)) {
|
||||
// add root node to visited
|
||||
visited.add(value);
|
||||
return Optional.of(node);
|
||||
}
|
||||
visited.add(node.getValue());
|
||||
|
||||
Queue<Node> queue = new ArrayDeque<>(node.getSubNodes());
|
||||
Queue<Node<T>> queue = new ArrayDeque<>(node.getChildren());
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
final Node current = queue.poll();
|
||||
final Node<T> current = queue.poll();
|
||||
visited.add(current.getValue());
|
||||
|
||||
if (current.getName().equals(name)) {
|
||||
if (current.getValue().equals(value)) {
|
||||
return Optional.of(current);
|
||||
}
|
||||
|
||||
queue.addAll(current.getSubNodes());
|
||||
queue.addAll(current.getChildren());
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public List<T> getVisited() {
|
||||
return visited;
|
||||
}
|
||||
}
|
||||
|
@ -1,79 +1,32 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import com.thealgorithms.datastructures.Node;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author: caos321
|
||||
* @date: 31 October 2021 (Sunday)
|
||||
* @wiki: https://en.wikipedia.org/wiki/Depth-first_search
|
||||
*/
|
||||
public class DepthFirstSearch {
|
||||
public class DepthFirstSearch<T> {
|
||||
|
||||
static class Node {
|
||||
private final List<T> visited = new ArrayList<>();
|
||||
|
||||
private final String name;
|
||||
private final List<Node> subNodes;
|
||||
|
||||
public Node(final String name) {
|
||||
this.name = name;
|
||||
this.subNodes = new ArrayList<>();
|
||||
public Optional<Node<T>> recursiveSearch(final Node<T> node, final Integer value) {
|
||||
if (node == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public Node(final String name, final List<Node> subNodes) {
|
||||
this.name = name;
|
||||
this.subNodes = subNodes;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<Node> getSubNodes() {
|
||||
return subNodes;
|
||||
}
|
||||
}
|
||||
|
||||
public static Optional<Node> search(final Node node, final String name) {
|
||||
if (node.getName().equals(name)) {
|
||||
visited.add(node.getValue());
|
||||
if (node.getValue().equals(value)) {
|
||||
return Optional.of(node);
|
||||
}
|
||||
|
||||
return node.getSubNodes().stream().map(value -> search(value, name)).flatMap(Optional::stream).findAny();
|
||||
return node.getChildren().stream().map(v -> recursiveSearch(v, value)).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);
|
||||
}
|
||||
public List<T> getVisited() {
|
||||
return visited;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user