Repository navigation
Conversation
This branch has not been deployed
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
data_structures/heap/binomial_heap.py::BinomialHeapkeepsmin_nodepointing at a root, butmerge_treesmakes the second tree the root when two roots hold equal values. Ifmin_nodewas the first root, it ends up as a child inside the merged tree.insertandmerge_heapsboth did this and neither fixedmin_nodeup, so after inserting duplicatespeek()returned a larger value and a laterdelete_min()dropped a node (delete_minthen also crashed onNone.valonce the heap emptied).Reproducer on
master: insert4, 1, 8, 2, 2, thendelete_min()twice. They return1, 2, butpeek()is4instead of2, and the remaining2is gone from the tree.When a merge involves
min_node,min_nodeis 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 todelete_minwith the sequence above; it fails onmaster. 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_heapsof two heaps with duplicate values, then drained. After the change: 0 mismatches. Onmasterthe merge runs mismatched in 9,878 of 20,000.ruff check,ruff format --checkandpython -m doctestpass. 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