Skip to content

Commit 7ffe57d

Browse files
feat: Add Centroid Decomposition for trees (#7054)
- Implement CentroidDecomposition with O(N log N) construction - Add CentroidTree class with parent tracking and query methods - Include buildFromEdges helper for easy tree construction - Add comprehensive test suite with 20+ test cases - Cover edge cases, validation, and various tree structures Closes #7054
1 parent 45dd8bb commit 7ffe57d

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/main/java/com/thealgorithms/tree/CentroidDecomposition.java renamed to src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
package com.thealgorithms.tree;
1+
package com.thealgorithms.datastructures.trees;
22

3-
import java.util.*;
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
46

57
/**
68
* Centroid Decomposition is a divide-and-conquer technique for trees.
79
* It recursively partitions a tree by finding centroids - nodes whose removal
810
* creates balanced subtrees (each with at most N/2 nodes).
9-
*
11+
*
12+
* <p>
1013
* Time Complexity: O(N log N) for construction
1114
* Space Complexity: O(N)
12-
*
15+
*
16+
* <p>
1317
* Applications:
1418
* - Distance queries on trees
1519
* - Path counting problems
1620
* - Nearest neighbor searches
17-
*
21+
*
22+
* @see <a href="https://en.wikipedia.org/wiki/Centroid_decomposition">Centroid Decomposition</a>
23+
* @see <a href="https://codeforces.com/blog/entry/81661">Centroid Decomposition Tutorial</a>
1824
* @author lens161
1925
*/
2026
public final class CentroidDecomposition {
2127

2228
private CentroidDecomposition() {
23-
// Utility class
2429
}
2530

2631
/**

src/test/java/com/thealgorithms/tree/CentroidDecompositionTest.java renamed to src/test/java/com/thealgorithms/datastructures/trees/CentroidDecompositionTest.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
package com.thealgorithms.tree;
1+
package com.thealgorithms.datastructures.trees;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
47

8+
import java.util.ArrayList;
9+
import java.util.List;
510
import org.junit.jupiter.api.Test;
6-
import java.util.*;
711

812
/**
913
* Test cases for CentroidDecomposition
10-
*
14+
*
1115
* @author lens161
1216
*/
1317
class CentroidDecompositionTest {
@@ -17,7 +21,7 @@ void testSingleNode() {
1721
// Tree with just one node
1822
int[][] edges = {};
1923
CentroidDecomposition.CentroidTree tree = CentroidDecomposition.buildFromEdges(1, edges);
20-
24+
2125
assertEquals(1, tree.size());
2226
assertEquals(0, tree.getRoot());
2327
assertEquals(-1, tree.getParent(0));
@@ -28,11 +32,11 @@ void testTwoNodes() {
2832
// Simple tree: 0 - 1
2933
int[][] edges = {{0, 1}};
3034
CentroidDecomposition.CentroidTree tree = CentroidDecomposition.buildFromEdges(2, edges);
31-
35+
3236
assertEquals(2, tree.size());
3337
int root = tree.getRoot();
3438
assertTrue(root == 0 || root == 1, "Root should be either node 0 or 1");
35-
39+
3640
// One node should be root, other should have the root as parent
3741
int nonRoot = (root == 0) ? 1 : 0;
3842
assertEquals(-1, tree.getParent(root));
@@ -44,7 +48,7 @@ void testLinearTree() {
4448
// Linear tree: 0 - 1 - 2 - 3 - 4
4549
int[][] edges = {{0, 1}, {1, 2}, {2, 3}, {3, 4}};
4650
CentroidDecomposition.CentroidTree tree = CentroidDecomposition.buildFromEdges(5, edges);
47-
51+
4852
assertEquals(5, tree.size());
4953
// For a linear tree of 5 nodes, the centroid should be the middle node (node 2)
5054
assertEquals(2, tree.getRoot());
@@ -61,13 +65,13 @@ void testBalancedBinaryTree() {
6165
// 3 4
6266
int[][] edges = {{0, 1}, {0, 2}, {1, 3}, {1, 4}};
6367
CentroidDecomposition.CentroidTree tree = CentroidDecomposition.buildFromEdges(5, edges);
64-
68+
6569
assertEquals(5, tree.size());
6670
// Root should be 0 or 1 (both are valid centroids)
6771
int root = tree.getRoot();
6872
assertTrue(root == 0 || root == 1);
6973
assertEquals(-1, tree.getParent(root));
70-
74+
7175
// All nodes should have a parent in centroid tree except root
7276
for (int i = 0; i < 5; i++) {
7377
if (i != root) {

0 commit comments

Comments
 (0)