Fix BFS (#3759)
This commit is contained in:
parent
4990f791a6
commit
9cde14095c
@ -2,8 +2,6 @@ package com.thealgorithms.searches;
|
|||||||
|
|
||||||
import com.thealgorithms.searches.DepthFirstSearch.Node;
|
import com.thealgorithms.searches.DepthFirstSearch.Node;
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
|
|
||||||
@ -12,7 +10,6 @@ import java.util.Queue;
|
|||||||
* @date: 31 October 2021 (Sunday)
|
* @date: 31 October 2021 (Sunday)
|
||||||
*/
|
*/
|
||||||
public class BreadthFirstSearch {
|
public class BreadthFirstSearch {
|
||||||
|
|
||||||
public static Optional<Node> search(final Node node, final String name) {
|
public static Optional<Node> search(final Node node, final String name) {
|
||||||
if (node.getName().equals(name)) {
|
if (node.getName().equals(name)) {
|
||||||
return Optional.of(node);
|
return Optional.of(node);
|
||||||
@ -28,62 +25,8 @@ public class BreadthFirstSearch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
queue.addAll(current.getSubNodes());
|
queue.addAll(current.getSubNodes());
|
||||||
|
|
||||||
queue.remove();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class BreadthFirstSearchTest {
|
||||||
|
|
||||||
|
private static final DepthFirstSearch.Node rootNode = new DepthFirstSearch.Node(
|
||||||
|
"A",
|
||||||
|
List.of(
|
||||||
|
new DepthFirstSearch.Node(
|
||||||
|
"B",
|
||||||
|
List.of(
|
||||||
|
new DepthFirstSearch.Node("D"),
|
||||||
|
new DepthFirstSearch.Node("F", List.of(new DepthFirstSearch.Node("H"), new DepthFirstSearch.Node("I")))
|
||||||
|
)
|
||||||
|
),
|
||||||
|
new DepthFirstSearch.Node("C", List.of(new DepthFirstSearch.Node("G"))),
|
||||||
|
new DepthFirstSearch.Node("E")
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void searchI() {
|
||||||
|
Optional<DepthFirstSearch.Node> Inode = BreadthFirstSearch.search(rootNode, "I");
|
||||||
|
assertTrue(Inode.isPresent());
|
||||||
|
assertEquals(Inode.get().getName(), "I");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void searchG() {
|
||||||
|
Optional<DepthFirstSearch.Node> Gnode = BreadthFirstSearch.search(rootNode, "G");
|
||||||
|
assertTrue(Gnode.isPresent());
|
||||||
|
assertEquals(Gnode.get().getName(), "G");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void searchE() {
|
||||||
|
Optional<DepthFirstSearch.Node> Enode = BreadthFirstSearch.search(rootNode, "E");
|
||||||
|
assertTrue(Enode.isPresent());
|
||||||
|
assertEquals(Enode.get().getName(), "E");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user