forked from gyoogle/tech-interview-for-developer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeap.java
62 lines (51 loc) ยท 940 Bytes
/
Heap.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
50
51
52
53
54
55
56
57
58
59
60
61
62
public class Heap {
static int N, heapSize;
static int[] arr;
static void init(int n) {
N = n;
arr = new int[N+1];
heapSize = 0;
}
static void add(int n) {
arr[++heapSize] = n;
for (int i = heapSize; i > 1; i/=2) {
if(arr[i] < arr[i/2]) {
swap(i/2, i);
}
else break;
}
}
static int remove(int[] arr) {
if(heapSize == 0) return 0;
int rm = arr[1];
arr[1] = arr[heapSize];
arr[heapSize--] = 0;
for (int i = 1; i*2 <= heapSize;) {
if(i*2+1 <= heapSize) {
if(arr[i] < arr[i*2] && arr[i] < arr[i*2+1]) break;
else if(arr[i*2] < arr[i*2+1]) {
swap(i, i*2);
i = i*2;
}
else {
swap(i, i*2+1);
i = i*2+1;
}
}
else {
if(arr[i] > arr[i*2]) {
swap(i, i*2);
i = i*2;
}
else
break;
}
}
return rm;
}
static void swap(int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}