-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryHeap.java
49 lines (49 loc) · 1.24 KB
/
BinaryHeap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package interview;
public class BinaryHeap{
private final int heap[];
private final int SIZE;
private int TOP;
public BinaryHeap(int N){
heap=new int[N];
this.SIZE=N;
TOP=-1;
}
public BinaryHeap(){
heap=new int[10];
this.SIZE=10;
TOP=-1;
}
public void insert(int e){
heap[++TOP]=e;
}
private void heapify(int heap[],int index,int n){
int largest=index;
int left=(2*index+1);
int right=(2*index+2);
if(left < n && heap[left] > heap[largest])
largest=left;
if(right < n && heap[right] > heap[left])
largest=right;
if(largest!=index){
int temp=heap[largest];
heap[largest]=heap[index];
heap[index]=temp;
heapify(heap,largest,n);
}
}
public int getMax(){
/* HEAPIFY HERE */
for(int i=(TOP+1/2)-1;i>=0;i--)
heapify(heap,i,TOP+1);
return heap[0];
}
public int remove(){
int x=heap[TOP];
heap[TOP--]=0;
return x;
}
public void print(){
for(int i=0;i<=TOP;i++)
System.out.print(" "+heap[i]);
}
}