Java icon indicating copy to clipboard operation
Java copied to clipboard

Feature/centroid decomposition

Open lens161 opened this issue 3 months ago • 2 comments

  • [ x] I have read CONTRIBUTING.md.
  • [ x] This pull request is all my own work -- I have not plagiarized it.
  • [ x] All filenames are in PascalCase.
  • [x ] All functions and variable names follow Java naming conventions.
  • [ x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • [ x] All new code is formatted with clang-format -i --style=file path/to/your/file.java

Summary: This PR adds an implementation of centroid decomposition for trees. The algorithm recursively finds centroids of subtrees and builds a centroid tree with height O(log n). This structure is useful for answering distance, nearest node, and other divide and conquer tree queries.

Details:

Supports adding edges to an undirected tree

Builds the centroid decomposition via build()

Provides access to centroid parents and children

Includes a helper to iterate over centroid ancestors

Tests cover subtree sizes, parent relationships, children, reset behaviour, and consistency

lens161 avatar Nov 21 '25 11:11 lens161

Codecov Report

:x: Patch coverage is 92.50000% with 6 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 78.51%. Comparing base (e6c576c) to head (7151242).

Files with missing lines Patch % Lines
.../com/thealgorithms/tree/CentroidDecomposition.java 92.50% 4 Missing and 2 partials :warning:
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7089      +/-   ##
============================================
+ Coverage     78.46%   78.51%   +0.05%     
- Complexity     6746     6776      +30     
============================================
  Files           758      759       +1     
  Lines         22339    22419      +80     
  Branches       4384     4397      +13     
============================================
+ Hits          17528    17602      +74     
- Misses         4107     4111       +4     
- Partials        704      706       +2     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Nov 21 '25 11:11 codecov-commenter

Everything regarding my changes passes. the build fail comes from: "MonteCarloIntegrationTest.testNegativeInterval:60 expected: <0.0> but was: <-0.030964689606784793>"

lens161 avatar Nov 21 '25 11:11 lens161

This pull request has been automatically closed because its workflows or checks failed and it has been inactive for more than 14 days. Please fix the workflows and reopen if you'd like to continue. Merging from main/master alone does not count as activity.

github-actions[bot] avatar Dec 06 '25 03:12 github-actions[bot]