-3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 110 

    at HeapPriorityQueue.hasLeft(HeapPriorityQueue.java:168)
    at HeapPriorityQueue.bubbleDown(HeapPriorityQueue.java:111)
    at HeapPriorityQueue.removeMin(HeapPriorityQueue.java:73)
    at a4tester.testRandomArray(a4tester.java:224)
    at a4tester.stressTest(a4tester.java:237)
    at a4tester.main(a4tester.java:283)

I have been trying to find where the Out of bounds exception is coming from and it is driving me insane! It only arises when my HeapPriorityQueue is put through the stress test below. Basically getting an array out of bounds exception when my code is ran through this stress test:

public static boolean testRandomArray (int count) {

    PriorityQueue q = createNewPriorityQueue(count);

    System.out.println("Testing size: " + count);
    Random r = new Random();

    for ( int i = 0; i < count; i++ )
    {
        int val = r.nextInt(1000000);
        q.insert (val);
    }

    int oldVal = -1;

    while (!q.isEmpty() )
    {
        int val = (int)((Integer)q.removeMin()).intValue(); // or a bug

        if ( oldVal > val )
            return false;
        oldVal = val;
    }
    return true;

}

This is my Program:

public class HeapPriorityQueue implements PriorityQueue {
protected final static int DEFAULT_SIZE = 10000;

/* This array is where you will store the elements in the heap */
protected Comparable storage[];

/* Keep track of the current number of elements in the heap */
protected int currentSize;

/* You do not need to change this constructor */
public HeapPriorityQueue () 
{
    this(DEFAULT_SIZE);
}

/* You do not need to change this constructor */
public HeapPriorityQueue(int size)
{
    storage = new Comparable[size + 1];
    currentSize = 0;
}

/*
 * You need to change the implementation of every public method
 * below this comment.
 *
 */
public int size () {
    return currentSize;
}

public boolean isEmpty () {
    if(size() == 0)
        return true;
    return false;
}

public Comparable removeMin () throws HeapEmptyException {
    if(isEmpty())
        throw new HeapEmptyException();
    Comparable returnValue = storage[1];
    storage[1] = storage[currentSize];
    storage[currentSize] = null;
    currentSize--;
    bubbleDown();
    return returnValue;
}

public void insert ( Comparable k  ) throws HeapFullException {
    if(currentSize >= storage.length - 1)
        throw new HeapFullException();
    currentSize++;
    storage[currentSize] = k;
    bubbleUp();

}

/* Your instructor's solution used the following helper methods
 * 
 * You do not need to use the same methods, but you may want to.
 */

/* 
 * A new value has just been added to the bottom of the heap
 * "bubble up" until it is in the correct position
 */
private void bubbleUp () {
    int index = currentSize;
    while(parent(index) != 0 && storage[parent(index)].compareTo(storage[index]) > 0) {
        swapElement(index, parent(index));
        index = parent(index);
    }
}

/*
 * Because of a removeMin operation, a value from the bottom
 * of the heap has been moved to the root.
 * 
 * "bubble down" until it is in the right position
 */
private void bubbleDown() {  
    int index = 1;   
    while (hasLeft(index)) {    
        int sc = leftChild(index);  
        if (hasRight(index) && storage[leftChild(index)].compareTo(storage[rightChild(index)]) > 0) {  
            sc = rightChild(index);  
        }   
        if (storage[index].compareTo(storage[sc]) > 0) {  
            swapElement(index, sc);  
        } 
        else{
        }
        index = sc;  
    }          
}

/*
 * Swap the element at position p1 in the array with the element at 
 * position p2
 */
private void swapElement ( int p1, int p2 ) {
    Comparable temp = storage[p1];
    storage[p1] = storage[p2];
    storage[p2] = temp;
}

/*
 * Return the index of the parent of the node at pos
 */
private int parent ( int pos )
{
    return (pos/2); // replace this with working code
}

/* 
 * Return the index of the left child of the node at pos
 */
private int leftChild ( int pos )
{
    return (pos*2); // replace this with working code
}

/* 
 * Return the index of the right child of the node at pos
 */
private int rightChild ( int pos )
{   
    return (pos * 2)+1; // replace this with working code
}

/*
 * Given the current number of elements in the heap, does the
 * node at pos have a left child?
 *
 * Note that all internal nodes have at least a left child.
 *
 */
private boolean hasLeft ( int pos )
{
    if(storage[leftChild(pos)] != null)
        return true;
    return false; // replace this with working code
}

/*
 * Given the current number of elements in the heap, does the
 * node at pos have a right child?
 */ 
private boolean hasRight ( int pos ) {
    if(storage[rightChild(pos)] != null)
        return true;
    return false; // replace this with working code
}

}

Athind
  • 1
  • 1
    Make sure you use an editor that displays line numbers. That will make it easy to find the faulty line. – President James K. Polk Aug 02 '14 at 01:07
  • 2
    So, what is line 168??? The error occurred on line 168. Knowing which line that is would make it fairly easy to at least see the cause of the exception. Without that info you force your readers to guess. – Hot Licks Aug 02 '14 at 02:56

1 Answers1

2

There are a few things that are potentially problematic:

private boolean hasLeft ( int pos )
{
    if(storage[leftChild(pos)] != null)
        return true;
    return false; // replace this with working code
}

should be changed to something like:

private boolean hasLeft ( int pos )
{
    if (storage.length > leftChild(pos) && storage[leftChild(pos)] != null)
        return true;
    return false; // replace this with working code
}

since you could be going out of bounds in your original code. The same logic applies to hasRight(). Additionally, you're also attempting to access storage, but you're never checking its length, ie you have storage[1], but there's not guarantee that you didn't pass 0 to your constructor. Hope that helps.

Steve P.
  • 14,489
  • 8
  • 42
  • 72
  • Yeah I just noticed that hasLeft dose not check if the left child's index is out of bounds. That fixed everything. Thank you! – Athind Aug 02 '14 at 01:13
  • @Athind No problem. Since you're new to SO, check out the [tour](http://www.stackoverflow.com/tour). – Steve P. Aug 02 '14 at 01:14