1
1
package algorithms .graphs ;
2
2
3
+ import java .util .ArrayList ;
4
+ import java .util .Arrays ;
5
+ import java .util .List ;
6
+
3
7
import org .junit .Test ;
4
8
5
9
import algorithms .graphs .util .BinaryTreeNode ;
6
10
import algorithms .graphs .util .GraphNode ;
7
11
8
- import java .util .ArrayList ;
9
- import java .util .Arrays ;
10
- import java .util .List ;
11
-
12
- public class breadthFirstSearchTest {
12
+ /**
13
+ * Test cases for {@link breadthFirstSearch}.
14
+ */
15
+ public class BreadthFirstSearchTest {
13
16
14
- @ Test
15
- public void bfs_levelOrderTraversal_shouldReturnAccurate () {
16
- // empty tree
17
- List <Integer > firstList = new ArrayList <>();
18
- BinaryTreeNode root1 = null ;
19
- List <Integer > firstResult = breadthFirstSearch .levelOrder (root1 );
17
+ @ Test
18
+ public void bfs_levelOrderTraversal_shouldReturnAccurate () {
19
+ // empty tree
20
+ List <Integer > firstList = new ArrayList <>();
21
+ BinaryTreeNode root1 = null ;
22
+ List <Integer > firstResult = breadthFirstSearch .levelOrder (root1 );
20
23
21
- //standard tree
22
- // 1
23
- // / \
24
- // 2 3
25
- // / \
26
- // 4 5
27
- List <Integer > secondList = new ArrayList <>(Arrays .asList (1 , 2 , 3 , 4 , 5 ));
28
- BinaryTreeNode rootRight2 = new BinaryTreeNode (3 , new BinaryTreeNode (4 ), new BinaryTreeNode (5 ));
29
- BinaryTreeNode root2 = new BinaryTreeNode (1 , new BinaryTreeNode (2 ), rootRight2 );
30
- List <Integer > secondResult = breadthFirstSearch .levelOrder (root2 );
24
+ //standard tree
25
+ // 1
26
+ // / \
27
+ // 2 3
28
+ // / \
29
+ // 4 5
30
+ List <Integer > secondList = new ArrayList <>(Arrays .asList (1 , 2 , 3 , 4 , 5 ));
31
+ BinaryTreeNode rootRight2 = new BinaryTreeNode (3 , new BinaryTreeNode (4 ), new BinaryTreeNode (5 ));
32
+ BinaryTreeNode root2 = new BinaryTreeNode (1 , new BinaryTreeNode (2 ), rootRight2 );
33
+ List <Integer > secondResult = breadthFirstSearch .levelOrder (root2 );
31
34
32
- //standard tree 2
33
- // 1
34
- // / \
35
- // 2 7
36
- // / \
37
- // 3 5
38
- // / /
39
- // 4 6
40
- List <Integer > thirdList = new ArrayList <>(Arrays .asList (1 , 2 , 7 , 3 , 5 , 4 , 6 ));
41
- BinaryTreeNode rootLeft3 = new BinaryTreeNode (2 , new BinaryTreeNode (3 , new BinaryTreeNode (4 ), null ), new BinaryTreeNode (5 , new BinaryTreeNode (6 ), null ));
42
- BinaryTreeNode root3 = new BinaryTreeNode (1 , rootLeft3 , new BinaryTreeNode (7 ));
43
- List <Integer > thirdResult = breadthFirstSearch .levelOrder (root3 );
35
+ //standard tree 2
36
+ // 1
37
+ // / \
38
+ // 2 7
39
+ // / \
40
+ // 3 5
41
+ // / /
42
+ // 4 6
43
+ List <Integer > thirdList = new ArrayList <>(Arrays .asList (1 , 2 , 7 , 3 , 5 , 4 , 6 ));
44
+ BinaryTreeNode rootLeft3 =
45
+ new BinaryTreeNode (2 , new BinaryTreeNode (3 , new BinaryTreeNode (4 ), null ),
46
+ new BinaryTreeNode (5 , new BinaryTreeNode (6 ), null ));
47
+ BinaryTreeNode root3 = new BinaryTreeNode (1 , rootLeft3 , new BinaryTreeNode (7 ));
48
+ List <Integer > thirdResult = breadthFirstSearch .levelOrder (root3 );
44
49
45
- assert firstResult .equals (firstList );
46
- // System.out.println(secondResult.toString());
47
- assert secondResult .equals (secondList );
48
- // System.out.println(thirdResult.toString());
49
- assert thirdResult .equals (thirdList );
50
- }
50
+ assert firstResult .equals (firstList );
51
+ assert secondResult .equals (secondList );
52
+ assert thirdResult .equals (thirdList );
53
+ }
51
54
52
- @ Test
53
- public void bfs_friendHops_shouldReturnAccurate () {
55
+ @ Test
56
+ public void bfs_friendHops_shouldReturnAccurate () {
54
57
55
- // Tests based on the following friend graph:
56
- // Andre ------ Ben ------- Diana ------- Evelyn ------- Gerald
57
- // | | |
58
- // Cathy Felix--------------------------
59
- // |
60
- // Harold ------ Iris Anonymous
61
- GraphNode <String > andre = new GraphNode <>("andre" );
62
- GraphNode <String > ben = new GraphNode <>("ben" );
63
- GraphNode <String > cathy = new GraphNode <>("cathy" );
64
- GraphNode <String > diana = new GraphNode <>("diana" );
65
- GraphNode <String > evelyn = new GraphNode <>("evelyn" );
66
- GraphNode <String > felix = new GraphNode <>("felix" );
67
- GraphNode <String > gerald = new GraphNode <>("gerald" );
68
- GraphNode <String > harold = new GraphNode <>("harold" );
69
- GraphNode <String > iris = new GraphNode <>("iris" );
70
- GraphNode <String > anonymous = new GraphNode <>("anonymous" );
71
- GraphNode .connect (andre , ben );
72
- GraphNode .connect (andre , cathy );
73
- GraphNode .connect (cathy , harold );
74
- GraphNode .connect (harold , iris );
75
- GraphNode .connect (ben , felix );
76
- GraphNode .connect (ben , diana );
77
- GraphNode .connect (diana , evelyn );
78
- GraphNode .connect (evelyn , felix );
79
- GraphNode .connect (evelyn , gerald );
58
+ // Tests based on the following friend graph:
59
+ // Andre ------ Ben ------- Diana ------- Evelyn ------- Gerald
60
+ // | | |
61
+ // Cathy Felix--------------------------
62
+ // |
63
+ // Harold ------ Iris Anonymous
64
+ GraphNode <String > andre = new GraphNode <>("andre" );
65
+ GraphNode <String > ben = new GraphNode <>("ben" );
66
+ GraphNode <String > cathy = new GraphNode <>("cathy" );
67
+ GraphNode <String > diana = new GraphNode <>("diana" );
68
+ GraphNode <String > evelyn = new GraphNode <>("evelyn" );
69
+ GraphNode <String > felix = new GraphNode <>("felix" );
70
+ GraphNode <String > gerald = new GraphNode <>("gerald" );
71
+ GraphNode <String > harold = new GraphNode <>("harold" );
72
+ GraphNode <String > iris = new GraphNode <>("iris" );
73
+ GraphNode <String > anonymous = new GraphNode <>("anonymous" );
74
+ GraphNode .connect (andre , ben );
75
+ GraphNode .connect (andre , cathy );
76
+ GraphNode .connect (cathy , harold );
77
+ GraphNode .connect (harold , iris );
78
+ GraphNode .connect (ben , felix );
79
+ GraphNode .connect (ben , diana );
80
+ GraphNode .connect (diana , evelyn );
81
+ GraphNode .connect (evelyn , felix );
82
+ GraphNode .connect (evelyn , gerald );
80
83
81
- assert breadthFirstSearch .friendHops (anonymous , diana ) == -1 ;
82
- assert breadthFirstSearch .friendHops (iris , gerald ) == 7 ;
83
- assert breadthFirstSearch .friendHops (andre , gerald ) == 4 ;
84
- assert breadthFirstSearch .friendHops (felix , harold ) == 4 ;
85
- }
86
- }
84
+ assert breadthFirstSearch .friendHops (anonymous , diana ) == -1 ;
85
+ assert breadthFirstSearch .friendHops (iris , gerald ) == 7 ;
86
+ assert breadthFirstSearch .friendHops (andre , gerald ) == 4 ;
87
+ assert breadthFirstSearch .friendHops (felix , harold ) == 4 ;
88
+ }
89
+ }
0 commit comments