-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathourPriorityQueue.java
67 lines (57 loc) · 1.5 KB
/
ourPriorityQueue.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
63
64
65
66
67
package dataStructure;
import java.util.ArrayList;
public class ourPriorityQueue<T extends Comparable<? super T>> {
ArrayList<T> data;
public ourPriorityQueue() {
data = new ArrayList<T>();
data.add(null);
}
void insert(T element) {
data.add(element);
int childIndex = data.size() - 1;
while(childIndex > 1) {
int parentIndex = childIndex / 2;
if((data.get(parentIndex)).compareTo(data.get(childIndex))<0) {
return;
}
T temp = data.get(childIndex);
data.set(childIndex, data.get(parentIndex));
data.set(parentIndex, temp);
childIndex = parentIndex;
}
}
public T getMin() {
if(data.size() == 1) {
return null;
}
return data.get(1);
}
public T removeMin() {
if(data.size() == 1) {
return null;
}
T ans = data.get(1);
data.set(1, data.get(data.size() - 1));
data.remove(data.size() - 1);
int parentIndex = 1;
while(true) {
int leftIndex = parentIndex * 2;
int rightIndex = leftIndex + 1;
int minIndex = parentIndex;
if(leftIndex < data.size() && (data.get(leftIndex)).compareTo(data.get(minIndex))<0) {
minIndex = leftIndex;
}
if(rightIndex < data.size() && (data.get(rightIndex).compareTo(data.get(minIndex))<0)) {
minIndex = rightIndex;
}
if(minIndex == parentIndex) {
break;
}
T temp = data.get(minIndex);
data.set(minIndex, data.get(parentIndex));
data.set(parentIndex, temp);
parentIndex = minIndex;
}
return ans;
}
}