-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinHeap.java
More file actions
47 lines (39 loc) · 858 Bytes
/
MinHeap.java
File metadata and controls
47 lines (39 loc) · 858 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
45
46
47
public class MinHeap {
MaxHeap heapster;
public MinHeap() {
heapster = new MaxHeap();
}
public void insert(int id, int cost) {
heapster.insert(id, -cost);
}
public void extract() {
heapster.extract();
}
public void decrease(int id, int cost) {
heapster.decrease(id, -cost);
}
public void increase(int id, int cost) {
heapster.increase(id, -cost);
}
public int getSize(){
return heapster.size;
}
public int getNode(int index){
return heapster.nodes.get(index).cost;
}
public static void main(String[] args) {
MinHeap hp = new MinHeap();
hp.insert(1, 100);
hp.insert(2, 300);
hp.insert(3, 250);
hp.insert(4, 1000);
hp.insert(5, 500);
hp.insert(6, 254);
hp.insert(7, 15);
// hp.decrease(5, 302);
hp.extract();
for (int i = 1; i <= hp.getSize(); i++) {
System.out.println(-hp.getNode(i));
}
}
}