Skip to content

fix(binomial_heap): fix data loss on duplicate keys and maintain min_… - #15470

Closed
Kanika0306 wants to merge 3 commits into
TheAlgorithms:masterfrom
Kanika0306:fix-binomial-heap-data-loss
Closed

Kanika0306 wants to merge 3 commits into
TheAlgorithms:masterfrom
Kanika0306:fix-binomial-heap-data-loss

Conversation

@Kanika0306

Copy link
Copy Markdown
Contributor

Describe your change:

Related issue: #15469

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?
  • Documentation change?

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 calling delete_min() until the heap is empty should return all inserted elements in non-decreasing order without losing any elements.

For example:

from data_structures.heap.binomial_heap import BinomialHeap

h = BinomialHeap()

for value in [1, 1, 1, 1, 1]:
    h.insert(value)

extracted = []

while not h.is_empty():
    extracted.append(h.delete_min())

assert extracted == [1, 1, 1, 1, 1]
assert h.size == 0
assert h.is_empty() is True

However, with duplicate values, h.delete_min() silently drops nodes from the heap:

from data_structures.heap.binomial_heap import BinomialHeap

h = BinomialHeap()

for value in [1, 1, 1, 1, 1]:
    h.insert(value)

extracted = []

while not h.is_empty():
    extracted.append(h.delete_min())

print(extracted)

Output:

[1, 1, 1, 1]

Only 4 elements are extracted instead of the 5 elements that were inserted.

Root Cause

BinomialHeap relies on self.min_node pointing to a root in the root list.

During tree consolidations in merge_heaps() and insert(), equal-key roots are merged via merge_trees(). If the node referenced by self.min_node is demoted to a child inside another binomial tree, self.min_node is left referencing an internal child node rather than a root.

Subsequent delete_min() calls misinterpret the child/sibling links on self.min_node as root-list links. This can sever entire binomial trees from the heap and corrupt self.size, resulting in elements being lost.

Additionally, line 359 in delete_min() casts the returned value using:

return int(min_value)

This truncates floating-point values. For example, a minimum value of 3.7 would be returned as 3.

Changes

The fix addresses the incorrect self.min_node reference 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 keeping self.size consistent with the actual number of elements.

Fixes #15469

Checklist:

  • I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes # [Bug]: Data loss in BinomialHeap.delete_min() with duplicate elements #15469".

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Sep 30, 2026
@algorithms-keeper algorithms-keeper Bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Sep 30, 2026
@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Sep 30, 2026
@cclauss

cclauss commented Oct 1, 2026

Copy link
Copy Markdown
Member

Clearing all open pull requests to prepare for Hacktoberfest 2026 -- https://hacktoberfest.com

@cclauss cclauss closed this Oct 1, 2026
@Kanika0306

Copy link
Copy Markdown
Contributor Author

HI @cclauss ,since PR #15470 was closed as part of the Hacktoberfest cleanup, should I resubmit this fix as a new PR during Hacktoberfest? Or should I wait for further instructions on what contributions are needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

# [Bug]: Data loss in BinomialHeap.delete_min() with duplicate elements

2 participants