Array index out of bounds java heap -
Array index out of bounds java heap -
i know amaetuer error, understand means dont understand why cant prepare it. ive been trying everything. im trying take array of type t , switch values around correctly corresponds rules of heap, parent greater 2 children. error in while loop
please dont harsh if fixable. ive been struggling heavily , cant seem find answer.
public class myheap<t extends comparable<t>> extends heap<t> { // constructors of subclass should written 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 greater right else //then right must greater 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 myheap extending /* polymorphic priority heaps, largest value on top. heap axiom. value @ every node cannot smaller values @ each of children nodes. utilize internal array implement heap "tree", index 0 representing root. given node index i, left(i)= 2*i+1 , 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 h.length! public int size() { homecoming size; } // size read-only externally. public int maxsize() { homecoming h.length; } public heap(t[] a) { h = a; size=0; } // preferred constructor public heap(int m) // cause compiler warning (ok ignore) { h = (t[]) new comparable[m]; // downcast object ok. size = 0; } protected int left(int i) { homecoming 2*i+1; } protected int right(int i) { homecoming 2*i+2; } protected int parent(int i) { homecoming (i-1)/2; } // protected important! // lookup heap, without delete public t getmax() { if (size<1) homecoming null; homecoming h[0]; } // insert x heap: place @ end, propagate upwards // returns false on failure. public boolean insert(t x) { if (size > h.length-1) homecoming false; h[size++] = x; // place @ 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 parent p = parent(cur); // recalc parent }//while homecoming true; }//insert // deletetop: take lastly element, move top, propagate downwards: public t deletemax() { if (size<0) homecoming null; t reply = h[0]; h[0] = h[--size]; // place @ top: // propagate downwards. boolean done = false; int = 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 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; = c; } }//while homecoming answer; }//deletemax // search not log(n). why? public boolean search(t x) { for(int i=0;i<size;i++) {if (x.compareto(h[i])==0) homecoming true;} homecoming false; } public void drawheap() // utilize heapdisplay.java programme { 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**/ }
you may check null in while
loop, (arr[r]!=null
) problem can't value array determine if it's null
or not. should check index within range before trying access value array, using r < arr.length
or similar.
java arrays heap
Comments
Post a Comment