-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPQ.java
156 lines (131 loc) · 4.59 KB
/
PQ.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Objects;
import java.util.PriorityQueue;
/**
* Kien Do 300163370
* CSI2510 - Structures de données et algorithms
* Devoir Programmation
*
* This Priority Queue represents who the employer ranks the highest.
*
* Implementation based on the class HeapPriorityQueue from the textbook Data Structures and Algorithms
* by Goodrich, page 377.
*/
public class PQ extends PriorityQueue<Pair> {
/**
* primary collection of priority queue entries
*/
protected ArrayList<Pair> heap = new ArrayList<>();
/**
* Creates an empty priority queue based on the natural ordering of its keys.
*/
public PQ() {
super();
}
/**
* Creates an empty priority queue using the given comparator to order keys.
*/
public PQ(Comparator<Pair> comp) {
super(comp);
}
// protected utilities
protected int parent(int j) {
return (j-1)/2;
} // truncating division
protected int left(int j) {
return 2 * j + 1;
}
protected int right(int j) {
return 2 * j + 2;
}
protected boolean hasLeft(int j) {
return left(j) < heap.size();
}
protected boolean hasRight(int j) {
return right(j) < heap.size();
}
/**
* Exchanges the entries at indices i and j of the array list.
*/
protected void swap(int i, int j) {
Pair temp = heap.get(i);
heap.set(i, heap.get(j));
heap.set(j, temp);
}
/**
* Moves the entry at index j higher, if necessary, to restore the heap property.
*/
protected void upheap(int j) {
while (j > 0) { // continue until reaching root (or break statement)
int p = parent(j);
//if (compare(heap.get(j), heap.get(p)) >= 0) break; // heap property verified
if (heap.get(j).getEmployerRanking() >= heap.get(p).getEmployerRanking()) break; // heap property verified
swap(j, p);
j = p; // continue from the parent's location
}
}
/**
* Moves the entry at index j lower, if necessary, to restore the heap property.
*/
protected void downheap(int j) {
while (hasLeft(j)) { // continue to bottom (or break statement)
int leftIndex = left(j);
int smallChildIndex = leftIndex; // although right may be smaller
if (hasRight(j)) {
int rightIndex = right(j);
//if (compare(heap.get(leftIndex), heap.get(rightIndex)) > 0)
if (heap.get(leftIndex).getEmployerRanking() >= heap.get(rightIndex).getEmployerRanking())
smallChildIndex = rightIndex; // right child is smaller
}
if (heap.get(smallChildIndex).getEmployerRanking() >= heap.get(j).getEmployerRanking())
break; // heap property has been restored
swap(j, smallChildIndex);
j = smallChildIndex; // continue at position of the child
}
}
// public methods
/**
* Returns the number of items in the priority queue.
*/
public int size() {
return heap.size();
}
/**
* Returns (but does not remove) an entry with minimal key (if any).
*/
@Override
public Pair peek() {
if (heap.isEmpty()) return null;
return heap.get(0);
}
/**
* Inserts a key-value pair and returns the entry created.
*/
public void insert(int employer_ranking, int student) throws IllegalArgumentException {
//checkKey(key); // auxiliary key-checking method (could throw exception)
Pair newest = new Pair(employer_ranking, student);
heap.add(newest); // add to the end of the list
upheap(heap.size() - 1); // upheap newly added entry
//return newest;
}
/**
* Removes and returns an entry with minimal key (if any).
*/
@Override
public Pair poll() {
if (heap.isEmpty()) return null;
Pair answer = heap.get(0);
swap(0, heap.size() - 1); // put minimum item at the end
heap.remove(heap.size() - 1); // and remove it from the list;
downheap(0); // then fix new root
return answer;
}
/**
* Removes the minimum Pair and returns the employer ranking (integer).
* @return The ranking of the employer for the student in this Pair.
*/
public int removeMin() {
return Objects.requireNonNull(poll()).getStudent();
}
}