-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.java
More file actions
49 lines (48 loc) · 1.08 KB
/
Copy pathHeap.java
File metadata and controls
49 lines (48 loc) · 1.08 KB
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
class Heap {
int[] a;
int n;
Heap(int[] a, int n) {
this.a = new int[n+3];
for (int i=1; i<=n; i++)
this.a[i] = a[i-1];
this.n = n;
for (int i=n>>1; i>0; i--)
heapify(i);
}
void heapify(int i) { //max heap
int left = i << 1;
int right = left + 1;
int max = i;
if (left <= n && a[left] > a[max]) max = left;
if (right <= n && a[right] > a[max]) max = right;
if (max != i) {
int temp = a[max];
a[max] = a[i];
a[i] = temp;
heapify(max);
}
}
void insert(int key) {
n++;
int i = n;
while (i>1 && a[i>>1] < key) {
a[i] = a[i>>1];
i >>= 1;
}
a[i] = key;
}
int extractMax() {
int max = a[1];
a[1] = a[n];
n--;
heapify(1);
return max;
}
void increaseKey(int i, long key) {
while (i>1 && a[i>>1] < key) {
a[i] = a[i>>1];
i >>= 1;
}
a[i] = key;
}
}