-1

I am trying to make min-heap algorithm and i am able to get insert,delete,pop ,trickleUp and trickleDown methods to work ,but i am having problems with the constructor which is to make use of build-heap algorithm.

The method is simple as it just wants to build a new heap from an array .Further,i am going to implement heap sort algorithm which requires heapify() to maintain the heap property.But,i am stuck if the constructor and so not sure if my heapify() is correct or not.Once constructor works,heap sort will become easy to implement.

/** Construct a new heap from an array of unsorted data.
 * 
 * Complexity: O(n)
 * @param data       Array to populate the heap from
 * @param capacity   Capacity of the heap (must be >= data.length)
 * @param comparator Comparator to use when comparing elements
     */

MinHeap(T[] data, int capacity) {


    for(int i = data.length/2 + 1; i >= 0;i--){       
    heapify(data,i);       
  }
 }


public  void heapify(Comparable[] data,int i){

    int smallest = i;

    if(size >left(i) && data[left(i)].compareTo(data[i])< 0 )

    {
        smallest = left(i);
    }
    else{
        smallest = i;
    }

    if(size>right(i) && data[right(i)].compareTo(data[i])< 0)

    {
        smallest = right(i);
    }

    if(smallest !=i)
    {
        Comparable temp = data[i];
        data[i] = data[smallest];
        data[smallest] = temp; 
        heapify(data,smallest);
    }
}

////// Test File

@Test public void testArrayConstruction() {
    System.out.println("array construction");

    for(int i = 0; i < 100; ++i) {
        Integer[] data = randomArray(i);
        MinHeap h = new MinHeap(data, i*2);

        assertEquals(h.capacity(), i * 2);
        assertEquals(h.size(), i);

        // Collections has min/max, but of course those don't work on arrays.
        Integer smallest = data[0];
        for(Integer x : data)
            if(x < smallest)
                smallest = x;                        

        checkHeapOrder(h);
        assertEquals(h.top(), smallest);           
    }

Integer[] randomArray(int size) {
    Integer[] arr = new Integer[size];
    for (int i = 0; i < size; ++i) {
        arr[i] = Math.round((float) Math.random() * Integer.MAX_VALUE);
    }

    return arr;
}

 void checkHeapOrder(MinHeap h) {
    assertTrue(h != null);

    for(int i = 1; i < h.size() / 2; ++i) 
        assertTrue("Heap order property is broken at element at position " 
 + i,
                   h.data[i].compareTo(h.data[i*2]) < 0 && 
                   h.data[i].compareTo(h.data[i*2 + 1]) < 0);
}

This is the line in the test file where the problem occurs : integer smallest = data[0]; Here,the method is unable to initialize smallest to 0th element of array.I tried tweaking with the program,but every time got the same error.I believe that the test file can also be incorrect,so just want to make sure.

testArrayConstruction caused an Error: 0
java.lang.ArrayIndexOutOfBoundsException
Meet Yeole
  • 71
  • 2
  • 8

1 Answers1

0

You didn't include the code for randomArray() ...

Assuming that this method makes randomized array with size equal to the method's argument, then your call to

Integer[] data = randomArray(0);

makes an array of size 0 .

You can't read the element at index 0 because an array of size 0 has no elements.

LowKeyEnergy
  • 652
  • 4
  • 11
  • I forgot to add randomArray();i have edited my post with code. So,the randomArray() makes an array equal to the method,so there is a problem with the tests ,not the program right ? – Meet Yeole Oct 31 '19 at 19:10