Skip to content

Update MinHeap.java #3162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update MinHeap.java
getElementKey method is decreasing the value of elementIndex by 1, so we can either add +1 while calling toggleUp or avoid decreasing the value of elementIndex.
  • Loading branch information
gkgaurav31 authored Jun 22, 2022
commit 546ab67f7111e4ab9ad2453ea304b3eadc7fa1fe
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void swap(int index1, int index2) {
// Toggle an element up to its right place as long as its key is lower than its parent's
private void toggleUp(int elementIndex) {
double key = minHeap.get(elementIndex - 1).getKey();
while (getElementKey((int) Math.floor(elementIndex / 2.0)) > key) {
while (getElementKey((int) Math.floor(elementIndex / 2.0) + 1) > key) {
swap(elementIndex, (int) Math.floor(elementIndex / 2.0));
elementIndex = (int) Math.floor(elementIndex / 2.0);
}
Expand Down