2
2
3
3
import com .thealgorithms .searches .DepthFirstSearch .Node ;
4
4
import java .util .ArrayDeque ;
5
- import java .util .List ;
6
- import java .util .Objects ;
7
5
import java .util .Optional ;
8
6
import java .util .Queue ;
9
7
12
10
* @date: 31 October 2021 (Sunday)
13
11
*/
14
12
public class BreadthFirstSearch {
15
-
16
13
public static Optional <Node > search (final Node node , final String name ) {
17
14
if (node .getName ().equals (name )) {
18
15
return Optional .of (node );
@@ -28,62 +25,8 @@ public static Optional<Node> search(final Node node, final String name) {
28
25
}
29
26
30
27
queue .addAll (current .getSubNodes ());
31
-
32
- queue .remove ();
33
28
}
34
29
35
30
return Optional .empty ();
36
31
}
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
- }
89
32
}
0 commit comments