fix(binomial_heap): fix data loss on duplicate keys and maintain min_… - #15470
Closed
Kanika0306 wants to merge 3 commits into
Closed
Kanika0306 wants to merge 3 commits into
Kanika0306 wants to merge 3 commits into
Conversation
for more information, see https://pre-commit.ci
Member
|
Clearing all open pull requests to prepare for Hacktoberfest 2026 -- https://hacktoberfest.com |
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
Related issue: #15469
This PR fixes data loss in
BinomialHeap.delete_min()when handling duplicate elements.Issue
When inserting multiple elements, including duplicate values, into a
BinomialHeap, repeatedly callingdelete_min()until the heap is empty should return all inserted elements in non-decreasing order without losing any elements.For example:
However, with duplicate values,
h.delete_min()silently drops nodes from the heap:Output:
Only 4 elements are extracted instead of the 5 elements that were inserted.
Root Cause
BinomialHeaprelies onself.min_nodepointing to a root in the root list.During tree consolidations in
merge_heaps()andinsert(), equal-key roots are merged viamerge_trees(). If the node referenced byself.min_nodeis demoted to a child inside another binomial tree,self.min_nodeis left referencing an internal child node rather than a root.Subsequent
delete_min()calls misinterpret the child/sibling links onself.min_nodeas root-list links. This can sever entire binomial trees from the heap and corruptself.size, resulting in elements being lost.Additionally, line 359 in
delete_min()casts the returned value using:This truncates floating-point values. For example, a minimum value of
3.7would be returned as3.Changes
The fix addresses the incorrect
self.min_nodereference after tree consolidation so that it continues to refer to a valid root.It also removes the unnecessary integer conversion in
delete_min()so that floating-point values are returned without truncation.Expected result
After the fix, duplicate elements should remain in the heap and repeated calls to
delete_min()should return every inserted element in non-decreasing order while keepingself.sizeconsistent with the actual number of elements.Fixes #15469
Checklist:
BinomialHeap.delete_min()with duplicate elements #15469".