0

In Java Software Structures 3rd Edition by Lewis and Chase the array implementation of a heap works for small numbers of items, but in cases with a large number of items sometimes throws a ArrayIndexOutOfBoundsException. It occurs in line 113 of the method heapifyRemove() in ArrayHeap (which extends ArrayBinaryTree).

Line 113:

if ((tree[left] == null) && (tree[right] == null))

It seems that left sometimes walks off the end of the array. How could this be fixed?

For reference:

ArrayHeap.java

ArrayBinaryTree.java

1 Answer 1

1

Before checking if the index for left is null it should check if the the size of the array contains left (from how the array grows it looks like it would always include left and right). I think that case should be interpreted the same as if left and right are null.

So the the code should be

if ((left > count) || ((tree[left] == null) && (tree[right] == null)))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much sir. This works perfectly! I'll have to submit it to the Pearson Books errata page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.