forked from gcallah/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPriorityQueue.java
More file actions
57 lines (42 loc) · 1.1 KB
/
MaxPriorityQueue.java
File metadata and controls
57 lines (42 loc) · 1.1 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
50
51
52
53
54
55
56
57
/**
* Created by Zebin Xu.
*
*/
package heapsort;
import java.util.List;
public class MaxPriorityQueue {
public static void main(String[] args) {
MaxPriorityQueue queue = new MaxPriorityQueue();
queue.insert(3);
queue.insert(6);
queue.insert(2);
queue.insert(5);
queue.insert(9);
while (!queue.isEmpty()) {
System.out.printf("maximum: %d%n", queue.maximum());
System.out.printf("extract max: %d%n", queue.extractMax());
}
}
private final MaxHeap heap;
public MaxPriorityQueue() {
this.heap = new MaxHeap();
}
public MaxPriorityQueue(List<Integer> array) {
this.heap = new MaxHeap(array);
}
public void insert(int key) {
heap.maxHeapInsert(key);
}
public int maximum() {
return heap.heapMaximum();
}
public int extractMax() {
return heap.heapExtractMax();
}
public void increaseKey(int i, int key) {
heap.heapIncreaseKey(i, key);
}
public boolean isEmpty() {
return heap.isEmpty();
}
}