|
14 | 14 | */ |
15 | 15 | public class BreadthFirstSearchTest { |
16 | 16 |
|
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); |
| 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); |
23 | 23 |
|
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); |
| 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); |
34 | 34 |
|
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); |
| 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 | + ); |
| 48 | + BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7)); |
| 49 | + List<Integer> thirdResult = breadthFirstSearch.levelOrder(root3); |
49 | 50 |
|
50 | | - assert firstResult.equals(firstList); |
51 | | - assert secondResult.equals(secondList); |
52 | | - assert thirdResult.equals(thirdList); |
53 | | - } |
| 51 | + assert firstResult.equals(firstList); |
| 52 | + assert secondResult.equals(secondList); |
| 53 | + assert thirdResult.equals(thirdList); |
| 54 | + } |
54 | 55 |
|
55 | | - @Test |
56 | | - public void bfs_friendHops_shouldReturnAccurate() { |
| 56 | + @Test |
| 57 | + public void bfs_friendHops_shouldReturnAccurate() { |
57 | 58 |
|
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); |
| 59 | + // Tests based on the following friend graph: |
| 60 | + // Andre ------ Ben ------- Diana ------- Evelyn ------- Gerald |
| 61 | + // | | | |
| 62 | + // Cathy Felix-------------------------- |
| 63 | + // | |
| 64 | + // Harold ------ Iris Anonymous |
| 65 | + GraphNode<String> andre = new GraphNode<>("andre"); |
| 66 | + GraphNode<String> ben = new GraphNode<>("ben"); |
| 67 | + GraphNode<String> cathy = new GraphNode<>("cathy"); |
| 68 | + GraphNode<String> diana = new GraphNode<>("diana"); |
| 69 | + GraphNode<String> evelyn = new GraphNode<>("evelyn"); |
| 70 | + GraphNode<String> felix = new GraphNode<>("felix"); |
| 71 | + GraphNode<String> gerald = new GraphNode<>("gerald"); |
| 72 | + GraphNode<String> harold = new GraphNode<>("harold"); |
| 73 | + GraphNode<String> iris = new GraphNode<>("iris"); |
| 74 | + GraphNode<String> anonymous = new GraphNode<>("anonymous"); |
| 75 | + GraphNode.connect(andre, ben); |
| 76 | + GraphNode.connect(andre, cathy); |
| 77 | + GraphNode.connect(cathy, harold); |
| 78 | + GraphNode.connect(harold, iris); |
| 79 | + GraphNode.connect(ben, felix); |
| 80 | + GraphNode.connect(ben, diana); |
| 81 | + GraphNode.connect(diana, evelyn); |
| 82 | + GraphNode.connect(evelyn, felix); |
| 83 | + GraphNode.connect(evelyn, gerald); |
83 | 84 |
|
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 | | - } |
| 85 | + assert breadthFirstSearch.friendHops(anonymous, diana) == -1; |
| 86 | + assert breadthFirstSearch.friendHops(iris, gerald) == 7; |
| 87 | + assert breadthFirstSearch.friendHops(andre, gerald) == 4; |
| 88 | + assert breadthFirstSearch.friendHops(felix, harold) == 4; |
| 89 | + } |
89 | 90 | } |
0 commit comments