Max Heap
public interface Heap{ public int size(); public boolean isEmpty(); public void insert(int element); public int removeMax(); public int peek(); public void maxHeapify(int pos); }
import java.util.Arrays; public class MaxHeap implements Heap{ private int[] heap; private int size; private int maxsize; private static final int FRONT = 0; public MaxHeap(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 maxHeapify(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)); maxHeapify(leftChild(pos)); }else { swap(pos, rightChild(pos)); maxHeapify(rightChild(pos)); } } } } public void maxHeap() { for (int pos = (size / 2); pos >= 0; pos--) { maxHeapify(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); } } //extractMax public int removeMax() { int popped = heap[FRONT]; heap[FRONT] = heap[--size]; maxHeapify(FRONT); heap[size] = 0; return popped; } //findMax 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 Max heap is "); MaxHeap maxHeap = new MaxHeap(15); maxHeap.insert(5); maxHeap.insert(3); maxHeap.insert(17); maxHeap.insert(10); maxHeap.insert(84); maxHeap.insert(19); maxHeap.insert(6); maxHeap.insert(22); maxHeap.insert(9); System.out.println(Arrays.toString(maxHeap.heap)); maxHeap.print(); System.out.println("Size of Heap is "+ maxHeap.size()); System.out.println("Maximum is "+ maxHeap.peek()); System.out.println(maxHeap.removeMax() + " is removed"); System.out.println(Arrays.toString(maxHeap.heap)); maxHeap.print(); System.out.println(maxHeap.removeMax() + " is removed"); System.out.println(Arrays.toString(maxHeap.heap)); maxHeap.print(); } } /* Output The Max heap is [84, 22, 19, 17, 10, 5, 6, 3, 9, 0, 0, 0, 0, 0, 0] PARENT : 84 LEFT CHILD : 22 RIGHT CHILD :19 PARENT : 22 LEFT CHILD : 17 RIGHT CHILD :10 PARENT : 19 LEFT CHILD : 5 RIGHT CHILD :6 PARENT : 17 LEFT CHILD : 3 RIGHT CHILD :9 Size of Heap is 9 Maximum is 84 84 is removed [22, 17, 19, 9, 10, 5, 6, 3, 0, 0, 0, 0, 0, 0, 0] PARENT : 22 LEFT CHILD : 17 RIGHT CHILD :19 PARENT : 17 LEFT CHILD : 9 RIGHT CHILD :10 PARENT : 19 LEFT CHILD : 5 RIGHT CHILD :6 PARENT : 9 LEFT CHILD : 3 RIGHT CHILD :0 22 is removed [19, 17, 6, 9, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0] PARENT : 19 LEFT CHILD : 17 RIGHT CHILD :6 PARENT : 17 LEFT CHILD : 9 RIGHT CHILD :10 PARENT : 6 LEFT CHILD : 5 RIGHT CHILD :3 */
0 comments:
Post a Comment