Skip to content

Add Fenwick tree implementation and tests - #7637

Merged
alxkm merged 2 commits into
TheAlgorithms:masterfrom
Rohan45create:add-fenwicktree-tests
Oct 6, 2026
Merged

alxkm merged 2 commits into
TheAlgorithms:masterfrom
Rohan45create:add-fenwicktree-tests

Conversation

@Rohan45create

Copy link
Copy Markdown
Contributor

Add FenwickTree tests, bounds checks and Javadoc

  • update(-1, x) made the index 0, so i & -i stayed 0 and the loop never ended. update and query now validate the index and throw IndexOutOfBoundsException outside [0, n). Before this, out-of-range behavior was inconsistent: update(i >= n) was silently ignored, query(-1) returned 0, and other bad indices threw ArrayIndexOutOfBoundsException.
  • The constructor throws IllegalArgumentException for a negative size.
  • Fields are now final, Javadoc was added (with a Wikipedia link), and the misleading query comment was corrected (it returns the sum over [0, i]).
  • Added FenwickTreeTest: fresh tree, single element, prefix sums, repeated updates, negative values, non-power-of-two size, zero size, invalid arguments, and a randomized comparison against naive prefix sums.
  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

codecov-commenter commented Oct 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.01%. Comparing base (f5772e3) to head (0ebac5f).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7637      +/-   ##
============================================
+ Coverage     81.95%   82.01%   +0.06%     
- Complexity     8273     8282       +9     
============================================
  Files           840      840              
  Lines         25865    25872       +7     
  Branches       5052     5053       +1     
============================================
+ Hits          21197    21219      +22     
+ Misses         3881     3865      -16     
- Partials        787      788       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

@alxkm alxkm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix - the infinite loop on update is a real bug and the tests are solid. Two small things before merge: rename the test file to FenwickTreeTest.java (it currently doesn't match the class name), and add some documentation link mentioned in the checklist.

Comment thread src/main/java/com/thealgorithms/datastructures/trees/FenwickTree.java Outdated
@Rohan45create
Rohan45create requested a review from alxkm October 6, 2026 14:57
@alxkm
alxkm merged commit 9d9e348 into TheAlgorithms:master Oct 6, 2026
7 checks passed
@Rohan45create
Rohan45create deleted the add-fenwicktree-tests branch October 6, 2026 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants