Skip to content

Commit b87bcc0

Browse files
author
lennart
committed
throw exception for invalid startNode, add tests, clean up
1 parent 3474d60 commit b87bcc0

2 files changed

Lines changed: 26 additions & 28 deletions

File tree

src/main/java/com/thealgorithms/tree/CentroidDecomposition.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,28 @@ public class CentroidDecomposition {
1212
private int startingNode;
1313
private int N;
1414

15+
@SuppressWarnings("unchecked")
1516
public CentroidDecomposition(int n, int startingNode){
1617
tree = new ArrayList[n];
1718
centroidTree = new ArrayList[n];
1819
N = n;
19-
for (int i = 0; i < centroidTree.length; i++) centroidTree[i] = new ArrayList<>();
20-
for (int i = 0; i < tree.length; i++) tree[i] = new ArrayList<>();
2120
centroidMarked = new boolean[n];
2221
centroidParent = new int[n];
23-
startingNode = (int)(Math.random() * n+1);
22+
if (startingNode < 0 || startingNode > n-1){
23+
throw new IllegalArgumentException("Starting node must be in range 0.." + (n - 1) + " but got " + startingNode);
24+
}
2425
for(int i = 0; i<n; i++){
2526
tree[i] = new ArrayList<>();
27+
centroidTree[i] = new ArrayList<>();
2628
}
2729
}
30+
2831
public CentroidDecomposition(int n){
29-
tree = new ArrayList[n];
30-
centroidTree = new ArrayList[n];
31-
centroidMarked = new boolean[n];
32-
centroidParent = new int[n];
33-
startingNode = (this.startingNode == -1) ? (int)(Math.random() * n+1) : this.startingNode;
34-
N = n;
35-
for (int i = 0; i < centroidTree.length; i++) centroidTree[i] = new ArrayList<>();
36-
for (int i = 0; i < tree.length; i++) tree[i] = new ArrayList<>();
37-
for(int i = 0; i<n; i++){
38-
tree[i] = new ArrayList<>();
39-
}
32+
this(n, (int)(Math.random() * n));
33+
}
34+
35+
public int getStartingNode(){
36+
return startingNode;
4037
}
4138

4239
public List<Integer>[] getCentroidTree(){
@@ -54,7 +51,6 @@ private void addEdgeCTree(int u, int v){
5451
centroidParent[v] = u;
5552
}
5653

57-
5854
public void findSubtreeSizes(int src, boolean[] visited, int[] subtreeSizes){
5955
// dfs traversal to find size of subtree rooted at src
6056
visited[src] = true;
@@ -87,14 +83,12 @@ public void findCentroid(int src, int previousCentroid){
8783

8884
if (centroidMarked[src]){
8985
if (src != startingNode && src != previousCentroid) addEdgeCTree(previousCentroid, src);
90-
// centroidTree[previousCentroid].add(src);
9186
for (int node : tree[src]){
9287
if (!centroidMarked[node])
9388
findCentroid(node, src);
9489
}
9590
}
9691
else{
97-
// pick one of the children of the root for next check
9892
int nextLargestSubtree = tree[src].getFirst();
9993
for (int node : tree[src]){
10094
if (subtreeSizes[node] >= subtreeSizes[nextLargestSubtree])
@@ -105,7 +99,7 @@ public void findCentroid(int src, int previousCentroid){
10599
}
106100

107101
public static void main(String[] args) {
108-
CentroidDecomposition cd = new CentroidDecomposition(16, -1);
102+
CentroidDecomposition cd = new CentroidDecomposition(16);
109103
cd.addEdge(0, 1);
110104
cd.addEdge(0, 2);
111105
cd.addEdge(0, 3);
@@ -126,7 +120,7 @@ public static void main(String[] args) {
126120
int[] subtreeSizes = new int[16];
127121

128122
// int start = cd.startingNode;
129-
int src = (int) ((Math.random() * (15)) + 0);
123+
int src = (int)(Math.random() * 15);
130124
System.out.println("src= " + src);
131125

132126
cd.findCentroid(src, src);

src/test/java/com/thealgorithms/tree/CentroidDecompositionTest.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package com.thealgorithms.tree;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
43
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertTrue;
6-
import static org.mockito.Answers.values;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
75

86
import java.util.ArrayList;
9-
import java.util.Arrays;
107
import java.util.List;
118

129
import org.junit.jupiter.api.BeforeEach;
@@ -37,7 +34,8 @@ void setUp(){
3734
cd.addEdge(14, 15);
3835

3936
/*
40-
* 0
37+
* Initial Tree:
38+
* 0
4139
/ | \
4240
1 2 3
4341
/ \ / \ \
@@ -52,7 +50,7 @@ void setUp(){
5250
15
5351
5452
55-
53+
* centroid Tree:
5654
0
5755
/ | \
5856
1 11 8
@@ -74,12 +72,18 @@ void testFindSubtreeSizes(){
7472
assertEquals(subtreeSizes[0], 12);
7573
}
7674

75+
@Test
76+
void IllegalArgumentThrows(){
77+
assertThrows(IllegalArgumentException.class, () -> {
78+
new CentroidDecomposition(10, 99);
79+
});
80+
}
81+
7782
@RepeatedTest(100)
7883
void testBuildCentroidTree(){
79-
int src = (int) ((Math.random() * (15)) + 0);
80-
cd.findCentroid(src, src);
84+
int start = cd.getStartingNode();
85+
System.out.println(start);
8186
List<Integer>[] centroidTree = cd.getCentroidTree();
82-
8387
List<Integer> correct = new ArrayList<Integer>();
8488
correct.add(0);
8589
correct.add(3);

0 commit comments

Comments
 (0)