Skip to content

Commit 9cde140

Browse files
authored
1 parent 4990f79 commit 9cde140

File tree

2 files changed

+47
-57
lines changed

2 files changed

+47
-57
lines changed

src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java

-57
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.thealgorithms.searches.DepthFirstSearch.Node;
44
import java.util.ArrayDeque;
5-
import java.util.List;
6-
import java.util.Objects;
75
import java.util.Optional;
86
import java.util.Queue;
97

@@ -12,7 +10,6 @@
1210
* @date: 31 October 2021 (Sunday)
1311
*/
1412
public class BreadthFirstSearch {
15-
1613
public static Optional<Node> search(final Node node, final String name) {
1714
if (node.getName().equals(name)) {
1815
return Optional.of(node);
@@ -28,62 +25,8 @@ public static Optional<Node> search(final Node node, final String name) {
2825
}
2926

3027
queue.addAll(current.getSubNodes());
31-
32-
queue.remove();
3328
}
3429

3530
return Optional.empty();
3631
}
37-
38-
public static void assertThat(final Object actual, final Object expected) {
39-
if (!Objects.equals(actual, expected)) {
40-
throw new AssertionError(
41-
String.format("expected=%s but was actual=%s", expected, actual)
42-
);
43-
}
44-
}
45-
46-
public static void main(final String[] args) {
47-
final Node rootNode = new Node(
48-
"A",
49-
List.of(
50-
new Node(
51-
"B",
52-
List.of(
53-
new Node("D"),
54-
new Node("F", List.of(new Node("H"), new Node("I")))
55-
)
56-
),
57-
new Node("C", List.of(new Node("G"))),
58-
new Node("E")
59-
)
60-
);
61-
62-
{
63-
final String expected = "I";
64-
65-
final Node result = search(rootNode, expected)
66-
.orElseThrow(() -> new AssertionError("Node not found!"));
67-
68-
assertThat(result.getName(), expected);
69-
}
70-
71-
{
72-
final String expected = "G";
73-
74-
final Node result = search(rootNode, expected)
75-
.orElseThrow(() -> new AssertionError("Node not found!"));
76-
77-
assertThat(result.getName(), expected);
78-
}
79-
80-
{
81-
final String expected = "E";
82-
83-
final Node result = search(rootNode, expected)
84-
.orElseThrow(() -> new AssertionError("Node not found!"));
85-
86-
assertThat(result.getName(), expected);
87-
}
88-
}
8932
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.thealgorithms.searches;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
import java.util.Optional;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
class BreadthFirstSearchTest {
11+
12+
private static final DepthFirstSearch.Node rootNode = new DepthFirstSearch.Node(
13+
"A",
14+
List.of(
15+
new DepthFirstSearch.Node(
16+
"B",
17+
List.of(
18+
new DepthFirstSearch.Node("D"),
19+
new DepthFirstSearch.Node("F", List.of(new DepthFirstSearch.Node("H"), new DepthFirstSearch.Node("I")))
20+
)
21+
),
22+
new DepthFirstSearch.Node("C", List.of(new DepthFirstSearch.Node("G"))),
23+
new DepthFirstSearch.Node("E")
24+
)
25+
);
26+
27+
@Test
28+
void searchI() {
29+
Optional<DepthFirstSearch.Node> Inode = BreadthFirstSearch.search(rootNode, "I");
30+
assertTrue(Inode.isPresent());
31+
assertEquals(Inode.get().getName(), "I");
32+
}
33+
34+
@Test
35+
void searchG() {
36+
Optional<DepthFirstSearch.Node> Gnode = BreadthFirstSearch.search(rootNode, "G");
37+
assertTrue(Gnode.isPresent());
38+
assertEquals(Gnode.get().getName(), "G");
39+
}
40+
41+
@Test
42+
void searchE() {
43+
Optional<DepthFirstSearch.Node> Enode = BreadthFirstSearch.search(rootNode, "E");
44+
assertTrue(Enode.isPresent());
45+
assertEquals(Enode.get().getName(), "E");
46+
}
47+
}

0 commit comments

Comments
 (0)