Add binary heap (min-heap) implementation in R#296
Open
sora16bit wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new data_structures/binary_heap.r script implementing a vector-backed min binary heap in R, intended to support priority-queue style usage consistent with other simple data-structure scripts in this repository.
Changes:
- Introduces
push(heap, value),pop(heap), andpeek(heap)operations for a min-heap stored in a plain R vector. - Adds inline documentation describing heap properties, complexities, applications, and example usage.
siriak
requested changes
Jun 11, 2026
siriak
left a comment
Member
There was a problem hiding this comment.
Please address Copilot's comments
Comment on lines
+1
to
+2
| # A binary heap is a complete binary tree where each node is smaller than or | ||
| # equal to its children (min-heap). The minimum is always at the root. |
Comment on lines
+65
to
+67
| i <- 1 | ||
| child <- 0 | ||
|
|
Comment on lines
+95
to
+111
| # Test | ||
| if (sys.nframe() == 0){ | ||
| h <- c() | ||
| h <- push(h, 5) | ||
| h <- push(h, 3) | ||
| h <- push(h, 8) | ||
| h <- push(h, 1) | ||
| h <- push(h, 9) | ||
| h <- push(h, 2) | ||
|
|
||
| print(peek(h)) | ||
| print(h) | ||
|
|
||
| res <- pop(h) | ||
| print(res$value) | ||
| print(res$heap) | ||
| } |
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
Adds a binary heap (min-heap) implementation to the
data_structuresdirectory.The heap is represented as a plain R vector using value-passing semantics,
consistent with the existing simple data-structure implementations in the repo.
Operations
push(heap, value)— insert a value and sift up — O(log n)pop(heap)— extract the minimum and sift down — O(log n), returnslist(value, heap)peek(heap)— view the minimum without removing it — O(1)Checklist
.rfile extension