Skip to content

Fix binomial heap losing track of the minimum when values are equal - #15792

Open
JayPokale wants to merge 1 commit into
TheAlgorithms:masterfrom
JayPokale:fix-binomial-heap-duplicate-min
Open

JayPokale wants to merge 1 commit into
TheAlgorithms:masterfrom
JayPokale:fix-binomial-heap-duplicate-min

Conversation

@JayPokale

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

data_structures/heap/binomial_heap.py::BinomialHeap keeps min_node pointing at a root, but merge_trees makes the second tree the root when two roots hold equal values. If min_node was the first root, it ends up as a child inside the merged tree. insert and merge_heaps both did this and neither fixed min_node up, so after inserting duplicates peek() returned a larger value and a later delete_min() dropped a node (delete_min then also crashed on None.val once the heap emptied).

Reproducer on master: insert 4, 1, 8, 2, 2, then delete_min() twice. They return 1, 2, but peek() is 4 instead of 2, and the remaining 2 is gone from the tree.

When a merge involves min_node, min_node is now set to the merged root. That root has the same value, since nothing is smaller than the minimum, so this stays O(1). I added a doctest to delete_min with the sequence above; it fails on master. I kept it in this PR because it is the regression evidence for this one fix.

Checked against sorted reference output: 60,000 random insert/delete_min sequences (values 0..9, up to 30 ops) and 20,000 random merge_heaps of two heaps with duplicate values, then drained. After the change: 0 mismatches. On master the merge runs mismatched in 9,878 of 20,000. ruff check, ruff format --check and python -m doctest pass. The file uses CRLF line endings and I kept them, so the diff is only the changed lines.

Written with the help of Claude Code (Claude Sonnet 5.5).

Checklist

  • I have read 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 new Python files are placed inside an existing directory.
  • 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 #ISSUE-NUMBER".

This branch has not been deployed

No deployments
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.

1 participant