Skip to content
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

bug: MaxHeap.toggleUp should determine if there is a parent node. #775

Open
MrHeer opened this issue Jun 3, 2019 · 1 comment
Open

bug: MaxHeap.toggleUp should determine if there is a parent node. #775

MrHeer opened this issue Jun 3, 2019 · 1 comment

Comments

@MrHeer
Copy link

@MrHeer MrHeer commented Jun 3, 2019

// 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 = maxHeap.get(elementIndex - 1).getKey();
while (getElementKey((int) Math.floor(elementIndex / 2)) < key) {
swap(elementIndex, (int) Math.floor(elementIndex / 2));
elementIndex = (int) Math.floor(elementIndex / 2);
}
}

should determine if there is a parent node while looping. Like this:

while ((int) Math.floor(elementIndex / 2) > 0 && getElementKey((int) Math.floor(elementIndex / 2)) < key) {
    ......
}
@1170500820
Copy link

@1170500820 1170500820 commented Jul 13, 2019

I am working on this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.