-1

I know this is an amaetuer error, i understand what it means but i dont understand why i cant fix it. Ive been trying everything. Im trying to take an array of type T and switch its values around so it correctly corresponds to the rules of a heap, where the parent is always greater than the 2 children. The error is in my while loop

please dont be harsh if its something easily fixable. ive been struggling heavily and cant seem to find an answer.

public class myheap<T extends Comparable<T>> extends heap<T>
{ 
    // constructors of the subclass should be written this way:
    public myheap(int max) { super(max); }
    public myheap(T[] A) {super(A);}  


    public void buildheap(T[] Arr){ 
        int size = Arr.length;
        int startsize = (size-1)/2;
        for(int i=startsize;i>0;i--){
            int l = left(i);
            int r = right(i);
            T temp = null;
            while((Arr[r]!=null) && Arr[i].compareTo(Arr[r])<0){
                if (Arr[l].compareTo(Arr[r])>0){
                    temp = Arr[l];
                    Arr[l] = Arr[i];
                    Arr[i] = temp;
                }//if left is greater than right
                else        //then right must be greater than parent            
                    temp = Arr[r];
                    Arr[r] = Arr[i];
                    Arr[i] = temp;
            }//whileloop
            if((Arr[r]==null) && Arr[i].compareTo(Arr[l])<0)
                temp = Arr[l];
                Arr[l] = Arr[i];
                Arr[i] = temp;
        }//for

    }//buildheap


    public static void main(String[] args){
        String[] array = {"SH","AH","AB","YA","AY","AA","AB","LM","LL","LO"};
        myheap<String> New = new myheap<String>(array.length);
        for(int i=0;i<array.length;i++){
            New.insert(array[i]);
        }//insert
        New.buildheap(array);
        New.drawheap();
        for(int i=0;i<array.length;i++){
            System.out.println(New.deletemax() + "  ");
        }//for
        System.out.println();

    } //main

}

Heap superclass that myheap is extending

/*
  Polymorphic priority heaps, largest value on top.
  Heap axiom.  The value at every node cannot be smaller than the values
  at each of its children nodes.
  Use internal array to implement heap "tree", with index 0 representing
  the root.  Given node index i, left(i)= 2*i+1 and right(i)=2*i+2, while
  parent(i) = (i-1)/2.
*/

class heap<T extends Comparable<T>>
{
  protected T[] H; // internal array representing heap.
  protected int size; // size of current heap, not same as H.length!
  public int size() { return size; } // size is read-only externally.
  public int maxsize() { return H.length; }
  public heap(T[] A) { H = A; size=0; } // preferred constructor
  public heap(int m) // will cause compiler warning (ok to ignore)
  {
     H = (T[]) new Comparable[m]; // downcast from Object is OK.
     size = 0;
  }

  protected int left(int i) { return 2*i+1; }
  protected int right(int i) { return 2*i+2; }
  protected int parent(int i) { return (i-1)/2; }

  // protected is important!

  // lookup heap, without delete
  public T getmax()
  { 
    if (size<1) return null; 
    return H[0];
  }

  // insert x into heap: place at end, then propagate upwards
  // returns false on failure.
  public boolean insert(T x)
  {
     if (size > H.length-1) return false;
     H[size++] = x; // place at end, inc size
     // propagate upwards
     int cur = size-1; // current position
     int p = parent(cur);
     while (cur>0 && H[cur].compareTo(H[p])>0)
     { // propagate upwards
       T temp = H[cur];
       H[cur] = H[p];  H[p] = temp;
       cur = p;  // switch current to parent
       p = parent(cur); // recalc parent
     }//while
     return true;
  }//insert

// deletetop: take last element, move to top, propagate downwards:
  public T deletemax()
  {
     if (size<0) return null;
     T answer = H[0];
     H[0] = H[--size]; // place at top:
     // now propagate downwards.
     boolean done = false;
     int i = 0; // current position
     int c = 0; // swap candidate
     while (c != -1)
     {
     int l = left(i);
     int r = right(i);
     c = -1; // swap candidate
     if (l<size && H[l].compareTo(H[i])>0) c = l; // set candidate to left
     if (r<size && H[r].compareTo(H[i])>0 && H[r].compareTo(H[l])>0) c=r;
     if (c!= -1) 
         {
         T temp = H[i];  H[i] = H[c]; H[c] = temp;
         i = c; 
         }
     }//while
     return answer;
  }//deletemax


    // but search is not log(n). Why?
  public boolean search(T x)
    {
    for(int i=0;i<size;i++) {if (x.compareTo(H[i])==0) return true;}
    return false;
    }

  public void drawheap() // use only with heapdisplay.java program
  {
      heapdisplay W = new heapdisplay(1024,768);
      W.drawtree(H,size);
  }

}//heap


public class heaps14
{
    /**public static void main(String[] args){
    heap<Integer> HI = new heap<Integer>(200);
    for(int i=0;i<100;i++) HI.insert((int)(Math.random()*1000));
    HI.drawheap();
    for(int i=0;i<100;i++) System.out.print(HI.deletemax() + "  ");
    System.out.println();
    }//main**/
}
trincot
  • 317,000
  • 35
  • 244
  • 286
Benji Weiss
  • 406
  • 2
  • 6
  • 19
  • 2
    Have you tried _debugging_ your app? – Sotirios Delimanolis Oct 10 '14 at 15:38
  • 1
    `2*i+2` is eventually going to be bigger than the size of your array. e.g. `11` element array, `i` will loop `0->5`, and `2*i+2` will eval out to `12` – Marc B Oct 10 '14 at 15:41
  • Array index out of bounds exception : 10, and @MarcB thats why i specify if Arr[r]!= null or if it null, – Benji Weiss Oct 10 '14 at 15:42
  • 1
    which is pointless. Arr[12] != null will fail because you've gone past the end of the array. java won't even bother TRYING to test for nullity, because you've obviously exceeded the size of the array. You're basically acting like wiley coyote, wondering why you're falling after running off the end of the sidewalk. – Marc B Oct 10 '14 at 15:43
  • This is a lot of code for us to look through. @SotiriosDelimanolis suggestion of debugging the code is the best first step IMO – But I'm Not A Wrapper Class Oct 10 '14 at 15:50

2 Answers2

0

You may check for null in your while loop, (Arr[r]!=null) but the problem is that you can't even get a value from the array to determine if it's null or not. You should check the index is within the range before trying to access the value from the array, using r < Arr.length or similar.

Edd
  • 3,724
  • 3
  • 26
  • 33
  • so i want to make sure that my Right and left child dont return an i value thats not in the size of my array? – Benji Weiss Oct 10 '14 at 16:19
  • @BenjiWeiss Possibly? I didn't have time to read all of your code, but the above is what jumped out. Generally whenever you access an element of an array you need to make sure the index is a legal value. – Edd Oct 10 '14 at 16:24
  • im correctly making sure that r and l are < size but now im getting a null pointer exception now in the same while loop, – Benji Weiss Oct 10 '14 at 16:33
  • i got it running thanks to your help, thanks man, appreciate it alot – Benji Weiss Oct 10 '14 at 17:03
0

(If null) isnt the problem, arrayIndexOutofBounds means you are geting a value of an array that isnt there Eg. Array.length =5; and you search Array[6]; - out of bounds....

The problem i think is your method right(i); which is. i*2+2 and the array

So change the for loop to this

 for(int i=startsize-2;i>0;i--)

comment if this helps.

kyle england
  • 118
  • 10
  • instead of changing my for loop, i nested my while loop to make sure that my indexes stay in bound. my problem now is that im getting a nullpointer exception in the same while loop – Benji Weiss Oct 10 '14 at 16:54