Min Heap



Min Heap
public interface Heap{
    public int size();
    public boolean isEmpty();
    public void insert(int element);
    public int removeMin();
    public int peek();
    public void minHeapify(int pos);
}

import java.util.Arrays;
public class MinHeap implements Heap{
    private int[] heap;
    private int size;
    private int maxsize;
 
    private static final int FRONT = 0;
 
    public MinHeap(int maxsize)
    {
        if(maxsize<1)
            throw new IllegalArgumentException("Initial capacity must be >=1");
        this.maxsize = maxsize;
        this.size = 0;
        heap = new int[this.maxsize];
    }
    private int parent(int pos)
    {
        return (pos-1)/ 2;
    } 
    private int leftChild(int pos)
    {
        return (2 * pos)+1;
    } 
    private int rightChild(int pos)
    {
        return (2 * pos) + 2;
    }
    private boolean isLeaf(int pos)
    {
        if (pos >=  (size / 2)  &&  pos <= size)
        {
            return true;
        }
        return false;
    }
    private void swap(int pos1,int pos2)
    {
        int tmp;
        tmp = heap[pos1];
        heap[pos1] = heap[pos2];
        heap[pos2] = tmp;
    }
    public void minHeapify(int pos)
    {
        if (!isLeaf(pos))
        { 
            if ( heap[pos] > heap[leftChild(pos)]  || heap[pos] > heap[rightChild(pos)])
            {   
                if (heap[leftChild(pos)] < heap[rightChild(pos)])
                {
                    swap(pos, leftChild(pos));
                    minHeapify(leftChild(pos));
                }else
                {
                    System.out.println(pos);
                    swap(pos, rightChild(pos));
                    minHeapify(rightChild(pos));
                }
            }
        }
    }
    public void MinHeap()
    {
        for (int pos = (size / 2); pos >= 0; pos--)
        {
            minHeapify(pos);
        }
    }
    public int size(){
        return size;
    }
    public boolean isEmpty(){
        return size == 0;
    }
    public int[] changeLength1D()
    {
        int heap1[]=new int[2*size];
        for(int i=0;i<size/2;i++)
        {
            heap1[i]=heap[i];
        }
        return  heap1;
    }
    public void insert(int element)
    {
        int current = size;
        if (size == heap.length)
       
            heap = changeLength1D();
            heap[size++] = element;
        while(heap[current] < heap[parent(current)])
        {
            swap(current,parent(current));
            current = parent(current);
        }       
    }
    //extractMin
    public int removeMin()
    {
        int popped = heap[FRONT];
        heap[FRONT] = heap[--size]; 
        minHeapify(FRONT);
        heap[size]=0;
        return popped;
    }
    //findMin
    public int peek(){
        return heap[FRONT];
    }
    
    public void print()
    {
        for (int i = 0; i < size / 2; i++ )
        {
            System.out.print(" PARENT : " + heap[i] + " LEFT CHILD : " + heap[leftChild(i)]
                  + " RIGHT CHILD :" + heap[rightChild(i)]);
            System.out.println();
        }
    }
    public static void main(String args[]){
        System.out.println("The Min heap is ");
        MinHeap MinHeap = new MinHeap(15);
        MinHeap.insert(5);
        MinHeap.insert(3);
        MinHeap.insert(17);
        MinHeap.insert(22);
        MinHeap.insert(13);
        MinHeap.insert(19);
        MinHeap.insert(6);
        MinHeap.insert(22);
        MinHeap.insert(7);
        MinHeap.insert(1);
        System.out.println(Arrays.toString(MinHeap.heap));
        MinHeap.print();
        System.out.println("Size of Heap is "+ MinHeap.size());
        System.out.println("Minimum is "+ MinHeap.peek());
        System.out.println(MinHeap.removeMin() + " is removed");
        System.out.println(Arrays.toString(MinHeap.heap));
        MinHeap.print();
        System.out.println(MinHeap.removeMin() + " is removed");
        System.out.println(Arrays.toString(MinHeap.heap));
        MinHeap.print();        
    }
}
/*
Output
The Min heap is
[1, 3, 6, 7, 5, 19, 17, 22, 22, 13, 0, 0, 0, 0, 0]
 PARENT : 1 LEFT CHILD : 3 RIGHT CHILD :6
 PARENT : 3 LEFT CHILD : 7 RIGHT CHILD :5
 PARENT : 6 LEFT CHILD : 19 RIGHT CHILD :17
 PARENT : 7 LEFT CHILD : 22 RIGHT CHILD :22
 PARENT : 5 LEFT CHILD : 13 RIGHT CHILD :0
Size of Heap is 10
Minimum is 1
1
1 is removed
[3, 5, 6, 7, 13, 19, 17, 22, 22, 0, 0, 0, 0, 0, 0]
 PARENT : 3 LEFT CHILD : 5 RIGHT CHILD :6
 PARENT : 5 LEFT CHILD : 7 RIGHT CHILD :13
 PARENT : 6 LEFT CHILD : 19 RIGHT CHILD :17
 PARENT : 7 LEFT CHILD : 22 RIGHT CHILD :22
3 is removed
[5, 7, 6, 22, 13, 19, 17, 22, 0, 0, 0, 0, 0, 0, 0]
 PARENT : 5 LEFT CHILD : 7 RIGHT CHILD :6
 PARENT : 7 LEFT CHILD : 22 RIGHT CHILD :13
 PARENT : 6 LEFT CHILD : 19 RIGHT CHILD :17
 PARENT : 22 LEFT CHILD : 22 RIGHT CHILD :0
 */



0 comments:

Post a Comment

Search This Blog

Powered by Blogger.