Skip to content

Commit 6c34da1

Browse files
committed
Tree center tests
1 parent 13a67d0 commit 6c34da1

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

com/williamfiset/algorithms/graphtheory/treealgorithms/TreeCenter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
*
44
* <p>Time complexity: O(V+E)
55
*
6-
* @author Jeffrey Xiao, https://github.com/jeffrey-xiao
6+
* @author Original author: Jeffrey Xiao, https://github.com/jeffrey-xiao
7+
* @author Modifications by: William Fiset, william.alexandre.fiset@gmail.com
78
*/
89
package com.williamfiset.algorithms.graphtheory.treealgorithms;
910

1011
import java.util.ArrayList;
1112
import java.util.LinkedList;
1213
import java.util.List;
1314

14-
class TreeCenter {
15+
public class TreeCenter {
1516

1617
public static List<Integer> findTreeCenters(List<List<Integer>> tree) {
1718
final int n = tree.size();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// To run this test in isolation from root folder:
2+
//
3+
// $ gradle test --tests
4+
// javatests.com.williamfiset.algorithms.graphtheory.treealgorithms.TreeCenterTest
5+
6+
package javatests.com.williamfiset.algorithms.graphtheory.treealgorithms;
7+
8+
import static com.google.common.truth.Truth.assertThat;
9+
import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeCenter.addUndirectedEdge;
10+
import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeCenter.createEmptyTree;
11+
import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeCenter.findTreeCenters;
12+
13+
import java.util.*;
14+
import org.junit.*;
15+
16+
public class TreeCenterTest {
17+
18+
@Test
19+
public void emptyTreeThrowsException() {
20+
assertThat(createEmptyTree(0)).isEmpty();
21+
}
22+
23+
@Test
24+
public void simpleTest1() {
25+
List<List<Integer>> graph = createEmptyTree(9);
26+
addUndirectedEdge(graph, 0, 1);
27+
addUndirectedEdge(graph, 2, 1);
28+
addUndirectedEdge(graph, 2, 3);
29+
addUndirectedEdge(graph, 3, 4);
30+
addUndirectedEdge(graph, 5, 3);
31+
addUndirectedEdge(graph, 2, 6);
32+
addUndirectedEdge(graph, 6, 7);
33+
addUndirectedEdge(graph, 6, 8);
34+
assertThat(findTreeCenters(graph)).containsExactly(2);
35+
}
36+
37+
@Test
38+
public void singleton() {
39+
assertThat(findTreeCenters(createEmptyTree(1))).containsExactly(0);
40+
}
41+
42+
@Test
43+
public void twoNodeTree() {
44+
List<List<Integer>> graph = createEmptyTree(2);
45+
addUndirectedEdge(graph, 0, 1);
46+
assertThat(findTreeCenters(graph)).containsExactly(0, 1);
47+
}
48+
49+
@Test
50+
public void simpleTest2() {
51+
List<List<Integer>> graph = createEmptyTree(3);
52+
addUndirectedEdge(graph, 0, 1);
53+
addUndirectedEdge(graph, 1, 2);
54+
assertThat(findTreeCenters(graph)).containsExactly(1);
55+
}
56+
57+
@Test
58+
public void simpleTest3() {
59+
List<List<Integer>> graph = createEmptyTree(4);
60+
addUndirectedEdge(graph, 0, 1);
61+
addUndirectedEdge(graph, 1, 2);
62+
addUndirectedEdge(graph, 2, 3);
63+
assertThat(findTreeCenters(graph)).containsExactly(1, 2);
64+
}
65+
66+
@Test
67+
public void simpleTest4() {
68+
List<List<Integer>> graph = createEmptyTree(7);
69+
addUndirectedEdge(graph, 0, 1);
70+
addUndirectedEdge(graph, 1, 2);
71+
addUndirectedEdge(graph, 2, 3);
72+
addUndirectedEdge(graph, 3, 4);
73+
addUndirectedEdge(graph, 4, 5);
74+
addUndirectedEdge(graph, 4, 6);
75+
assertThat(findTreeCenters(graph)).containsExactly(2, 3);
76+
}
77+
}

slides/graphtheory/trees_slides.key

69 KB
Binary file not shown.

0 commit comments

Comments
 (0)