-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRightMaxHeap.java
More file actions
44 lines (38 loc) · 912 Bytes
/
RightMaxHeap.java
File metadata and controls
44 lines (38 loc) · 912 Bytes
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
package class01;
/**
* @author pacai
* @version 1.0
* 暴力堆
*/
public class RightMaxHeap {
private int[] heap;
private int size;
private int heapSize;
public RightMaxHeap(int size) {
this.size = size;
heap = new int[size];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == heapSize;
}
public void push(int value) {
if (isFull()) {
throw new RuntimeException("Heap is full");
}
heap[heapSize++] = value;
}
public int pop() {
int maxIndex = 0;
for (int i = 0; i < heapSize; i++) {
if (heap[i] > heap[maxIndex]) {
maxIndex = i;
}
}
int ans = heap[maxIndex];
heap[maxIndex] = heap[heapSize--];
return ans;
}
}