Repository navigation
R23-T060: Explain the randomized heap and add a module docstring with a seeded doctest - #70
Open
code-paul-creator wants to merge 1 commit into
Open
code-paul-creator wants to merge 1 commit into
code-paul-creator wants to merge 1 commit into
Conversation
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.
Description
Explains
data_structures/heap/randomized_heap.pywell enough to re-implement it (idea, worked example, diagram, why merging is randomised) and improves the file's own documentation: a module docstring with a summary, a reference link, and a seeded doctest that builds a heap from 1,000 random values, pops them all and checks the order is sorted.Related Issue
Fixes: #60
Type of Change
Changes Made
data_structures/heap/randomized_heap.py: added a module docstring (one-paragraph summary, Wikipedia reference) and a seeded doctest (1,000 random values, pop all, check== sorted(values)). No code changes; the 54 existing doctests are unchanged (61 total).work/R23-T060/EXPLAINER.md: the idea, the algorithm, a worked merge and pop by hand with diagrams, why merging is randomised (O(log n) expected path length), pitfalls.work/R23-T060/experiment.pyandexperiment_output.txt: sorted-pop check and a comparison of merge cost with and without the coin flip.work/R23-T060/FEEDBACK.md: a classmate's attempt at re-deriving a doctest from the explainer.Screenshots / Recordings
Not applicable (no UI changes; diagrams are text in
EXPLAINER.md).Testing
python -m pytest --doctest-modules data_structures/heap/randomized_heap.py-> passed (61 doctests)ruff check data_structures/heap/randomized_heap.py-> all checks passedpython work/R23-T060/experiment.py-> 200/200 seeds: popping a heap built from 1,000 random values returnssorted(values)Checklist
Additional Notes
Findings: with the coin flip, an insert costs about 7-11 merge calls and a pop 10-13 for n = 1,000 to 4,000 (about log2 n); with a "never swap" variant, inserting ascending values costs about n/2 calls each, which is linear. Without the flip the tree becomes a left chain and
popis O(1) whileinsertis O(n); the coin flip balances the two. Classmate feedback is recorded inwork/R23-T060/FEEDBACK.md.