Skip to content

fix(sorts): keep duplicate values in TreeSort - #7661

Merged
alxkm merged 1 commit into
TheAlgorithms:masterfrom
Rishavbhattarai:fix/treesort-duplicates
Oct 27, 2026
Merged

alxkm merged 1 commit into
TheAlgorithms:masterfrom
Rishavbhattarai:fix/treesort-duplicates

Conversation

@Rishavbhattarai

@Rishavbhattarai Rishavbhattarai commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

TreeSort loses duplicates. BSTRecursiveGeneric ignores a key that is already in the tree (on purpose, and BSTRecursiveGenericTest.testDuplicateInsertions checks it), so sort({-1, -1, 0}) returns [-1, 0, 0]: one -1 is dropped and the array keeps a stale 0 in the last slot. The List overload returns a shorter list.

The fix leaves the BST alone and pairs each element with its input position in a private record, ordered by value and then by position. Equal values stay separate, and the sort is now stable.

TreeSortTest now extends SortingAlgorithmTest, like most other sort tests. On master that brings 6 failures (duplicates, all-identical values, empty strings). With the fix all 44 pass.

  • 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. (No new algorithm.)
  • All new algorithms include a corresponding test class that validates their functionality. (Regression test added.)
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.21%. Comparing base (96ed2d7) to head (66ecf25).

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #7661   +/-   ##
=========================================
  Coverage     82.20%   82.21%           
+ Complexity     8397     8396    -1     
=========================================
  Files           843      843           
  Lines         26133    26134    +1     
  Branches       5097     5097           
=========================================
+ Hits          21482    21485    +3     
+ Misses         3859     3854    -5     
- Partials        792      795    +3     

☔ 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.

@Rishavbhattarai
Rishavbhattarai marked this pull request as ready for review October 27, 2026 03:09

@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.

Looks good. Thank you for the contribution.

@alxkm
alxkm merged commit 12f69b3 into TheAlgorithms:master Oct 27, 2026
7 checks passed
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