Binary Heap in Java


Here is a example of Binary Heap in Java which my friend CodeZero wrote.
Copy it in your IDE or notepad and save it as "Test2.java"
You will need JDK to run this. Download it form here.
To run this open up the terminal or command prompt and type :
javac Test2.java
java Test2
 /*  
  * Author:_ash__  
  * Title:BinaryHeap[MaxHeap] implementation in java.  
  */  
 import java.util.Arrays;  
 public class Test2 {  
      public static void main(String args[]){  
           //Binary Heap with array passed in constructor.  
           System.out.println("bh1 operations.");  
           BinaryHeap bh1=new BinaryHeap(new int[]{8,10,12,13,14,15,16});  
           bh1.showHeap();  
           System.out.println(bh1.remove());  
           System.out.println(bh1.remove());  
           bh1.showHeap();  
           bh1.incrementElement(4, 100);  
           bh1.add(30);  
           bh1.showHeap();  
           bh1.add(40);  
           bh1.showHeap();  
           bh1.add(31);  
           bh1.showHeap();  
           System.out.println(  
                     "\n----------------------------------------------------------------\n"  
                     + "bh2 operations."  
                     );  
           //binaryHeap with capacity passed in constructor.  
           BinaryHeap bh2=new BinaryHeap(10);  
           bh2.add(8);  
           bh2.add(10);  
           bh2.add(12);  
           bh2.add(13);  
           bh2.add(14);  
           bh2.add(15);  
           bh2.add(100);  
           bh2.showHeap();  
      }  
 }  
 class BinaryHeap{  
      int data[];  
      int capacity=0;  
      int heap_size;  
      //set data array  
      public void setData(int data[]){  
           this.data=data;  
           heap_size=data.length;  
      }  
      //get data array  
      public int[] getData(){return data;}  
      //show data in array format.  
      public void showHeap(){  
           String val="[";  
           for(int i=0;i<heap_size;i++){  
                val+=""+data[i];  
                if(i+1!=heap_size)val+=",";  
           }  
           val+="]";  
           System.out.println(val);  
      }  
      //convert an given array to heap  
      //in a given binary Heap node form [0 to (data.length/2)-1] are all non-leaf nodes.  
      public int[] buildHeap(int data[]){  
           for(int i=data.length/2-1;i>=0;i--)  
                heapify(data,i,data.length);  
           return data;  
      }  
      //returns the [MAX --> ROOT] from the the heap.  
      //if Empty returns -1.  
      public int remove(){  
           if(heap_size<1)return -1;  
           //decrease heap size.  
           heap_size--;  
           //return first element.  
           int key=data[0];  
           //swap first and last elements.  
           data[0]=data[heap_size];;  
           heapify(data,0,heap_size);  
           return key;  
      }  
      //adding elements to a given heap;  
      public void add(int val){  
           if(heap_size<capacity){  
                //add element at last position.  
                data[heap_size]=val;  
                int i=heap_size;  
                //[i/2] gives index of the parent of a given child node 'i';  
                while(i>0 && data[i/2]<data[i]){  
                     int tmp=data[i/2];  
                     data[i/2]=data[i];  
                     data[i]=tmp;  
                     i=i/2;  
                }  
                heap_size++;  
           }  
      }  
      //increment an element at a given indexl  
      public void incrementElement(int i,int val){  
           //check given value is greater than original indexed value;  
           if(val>data[i] && i<heap_size){  
                data[i]=val;  
                //[i/2] gives index of the parent of a given child node 'i';  
                while(i>0 && data[i/2]<data[i]){  
                     int tmp=data[i/2];  
                     data[i/2]=data[i];  
                     data[i]=tmp;  
                     i=i/2;  
                }  
           }  
      }  
      //assign max capacity of an heap.  
      BinaryHeap(int l){  
           data=new int[l];  
           capacity=l;  
      }  
      //assign array to data.  
      //and convert it into a heap.  
      BinaryHeap(int data[]){  
           heap_size=data.length;  
           capacity=heap_size;  
           this.data=buildHeap(data);  
      }  
      //convert to or impose heaps rules on a given node.  
      //i.e parent node is [>=] its child nodes  
      private void heapify(int data[],int i,int length){  
           int l=2*i+1;//2i+1-->gives left child index of an parent.   
           int r=l+1;//(2i+2)==l+1-->gives right child index of an parent.  
           int largest;  
           if(l<length && data[l]>data[i])  
                largest=l;  
           else   
                largest=i;  
           if(r<length && data[r]>data[largest])  
                largest=r;  
           //now swap largest with i  
           int tmp=data[largest];  
           data[largest]=data[i];  
           data[i]=tmp;  
           //this can be remove if only u want to build the heap.  
           //but this statement is useful will removing a node from the heap(in our case the root node).  
           if(largest!=i)  
                heapify(data,largest,length);  
      }  
 }  
Previous
Next Post »