Group Group Group Group Group Group Group Group Group

Heap Challenge 4 Solution bug

#1

Hello,

In Heap Challenge 4 solution (check if a heap is a min heap), if we pass in elements = [6, 8, 11, 15, 12, 20], it will “out of range”

The book solution is on page 353

I believe we did not filter out the case below:
Parent start at element.count / 2 - 1
So right start at ( 2 * parent) + 2 = element.count
So elements[right] = elements[element.count] will be out of range !

So instead of if elements[right] < elements[i] { // 5
We can do if right < elements.count && elements[right] < elements[i] { // 5

Any comment ?

Thanks

1 Like
#2

@jomoka Can you please help with this when you get a chance? Thank you - much appreciated! :]

#3

Hi @oaranger sorry for the late reply. Yes this is a good catch, you are right. That should do the trick. We will make a note of this and fix it in the next edition of the book, much appreciated!