1

What corrections should I make to the following MaxHeap Implementation in Java?The Insert function keeps running when called. What is the error in the Insert and the BuildHeap functions?I have edited the PercolateDown function. I think it should be correct now..?

public class MaxHeap {
    public int[] array;
    public int count;
    public int capacity;
    public MaxHeap(int capacity){
        this.capacity=capacity;
        this.count=0;
        this.array=new int[capacity];
    }
    public int Parent(int i){
        if(i<=0 || i>=this.count)
            return -1;
        return
                (i-1)/2;
    }
    public int LeftChild(int i){
        int left=2*i+1;
        if(left>=this.count)
            return -1;
        return left;
    }
    public int RightChild(int i){
        int right=2*i+2;
        if(right>=this.count)
            return -1;
        return right;
    }
    public int GetMaximum(){
        if(this.count==0)
            return -1;
        return this.array[0];
    }
    public void PercolateDown(int i){
        int l,r,max,temp;
        l=LeftChild(i);
        r=RightChild(i);
        if(l!=-1 && this.array[l]>this.array[i])
            max=l;
        else
            max=i;
        if(r!=-1 && this.array[r]>this.array[max])
            max=r;
        if(max!=i){
            temp=this.array[i];
            this.array[i]=this.array[max];
            this.array[max]=temp;
        }
        if(max==i){return;}
        PercolateDown(max);
    }
    public int DeleteMax(){
        if(this.count==0)
            return -1;
        int data=this.array[0];
        this.array[0]=this.array[this.count-1];
        this.count--;
        PercolateDown(0);
        return data;
    }
    public void Insert(int data){
        int i;
        if(this.count==this.capacity){
            ResizeHeap();
        }
        this.count++;
        i=this.count-1;
        while(i>=0 && data>this.array[(i-1)/2]){
            this.array[i]=this.array[(i-1)/2];
            i=(i-1)/2;

        }
        this.array[i]=data;
    }
        public void ResizeHeap(){
            int[] array_old=new int[this.capacity];
            for(int i=0;i<this.capacity;i++){
                array_old[i]=this.array[i];
            }
            this.array=new int[this.capacity*2];
            for(int i=0;i<this.capacity;i++){
                this.array[i]=array_old[i];
            }
            this.capacity*=2;
            array_old=null;

    }
        public static MaxHeap BuildHeap(int[]A,int n){
            MaxHeap h=new MaxHeap(n*2);
            if(A==null)return h;
            //while(n>h.capacity)
                //h.ResizeHeap();
            h.capacity=n*2;
            for(int i=0;i<n;i++){
                h.array[i]=A[i];
            }
            h.count=n;
            for(int i=(n/2)-1;i>=0;i++){
                h.PercolateDown(i);
            }
            return h;
    }
        public void Delete(int i){          
            if(this.count<i){
                System.out.println("Wrong Position");
                return;
            }
            this.array[i]=this.array[this.count-1];
            this.count--;
            PercolateDown(i);

        }

        public static void main(String[] args){
            //int[] A={7,6,5,4,3,2,1};
            //int len=A.length;
            //MaxHeap h=BuildHeap(A,len);
            //for(int i=0;i<len;i++){
                //System.out.print(h.array[i]+" ");
            //}
            MaxHeap h=new MaxHeap(10);
            h.Insert(7);
            System.out.print(h.array[0]);
        }
}
codeislife
  • 319
  • 2
  • 8
  • 1
    "none of my functions seem to be working" - please be more precise about what you expect and what is actually happening – Gernot Oct 05 '16 at 17:29
  • Among other things, PercolateDown will never terminate. – Vlad Oct 05 '16 at 17:29
  • Reading through some of your code convinces me that some of your methods are working correctly. So if you have a specific question, please ask it specifically. Upvoting @Gernot ’s comment. – Ole V.V. Oct 05 '16 at 17:57
  • 1
    If you’ll forgive my frankness, OOP doesn’t seem to be a problem here, and for Java, except a few style issues you’re doing fine. I would guess the problem is more with logic and perhaps particularly with debugging. You may consider learning to use your debugger — I think it will supplement the strong points you already have in a good and practical way. – Ole V.V. Oct 05 '16 at 18:08
  • @Gernot -I have edited the question to be more precise. – codeislife Oct 07 '16 at 04:34

1 Answers1

0

I inserted this statement inside the while loop in Insert():

        System.out.println(i);

It prints 0 every time. Since 0 >= 0 and also your array comes with 0s in it, so when data is 7 and 7 > 0, the while condition is true. Inside your loop you are setting i to (i - 1) / 2, this is -1 / 2, which is rounded towards zero, yielding 0 again. So i is not changed, and your while condition continues to be true. This is why your loop never stops. I have not understood how you had intended the method to work, so I dare not give suggestions.

Trying the same print statement inside the for loop of BuildHeap() reveals that i is ever increasing until it overflows and suddenly is negative (if you add one to the highest int you can have, you get the lowest possible negative value, -2147483648). I think you had intended i-- rather then i++ in for(int i=(n/2)-1;i>=0;i++){.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161