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

MinBinaryHeap heapify calls compare function when unnecessary #89

Open
timvandam opened this issue Aug 9, 2021 · 0 comments
Open

MinBinaryHeap heapify calls compare function when unnecessary #89

timvandam opened this issue Aug 9, 2021 · 0 comments

Comments

@timvandam
Copy link

timvandam commented Aug 9, 2021

I'm talking about this call: https://github.com/jeffzh4ng/iruka/blob/02acaa0c621a6db9e874d6a7b4cc1bf2fd87f99a/src/data-structures/priority-queues/min-binary-heap.ts#L161

This call should not always happen here, as it can cause the compare function to be called with undefined values while the compare function should only receive whatever the generic heap value type is.

This is causing an issue for me when constructing a MinHeap with heapify as I'm storing key-value pairs in a length-2 array. My compare function ((a, b) => a[0] - b[0]) is being called even though indexOutOfBounds is true, which causes undefined to be passed as b, causing TypeError: Cannot read property '0' of undefined.

Inlining the function call fixes it:

const indexOutOfBounds = leftChildIndex >= this.size()
if (indexOutOfBounds || this.less(k, smallestIndex)) break

Alternatively the bound check can be put in the while condition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant