-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.java
More file actions
executable file
·171 lines (154 loc) · 4.1 KB
/
Copy pathPriorityQueue.java
File metadata and controls
executable file
·171 lines (154 loc) · 4.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
/**
* Implementation of a PriorityQueue using internal ArrayList
* Min-heap
*/
public class PriorityQueue<E extends Comparable<E>> {
/**
* the List containing the elements of the PriorityQueue
*/
private List<E> elements;
/**
* Comparator to determine priority of elements
*/
private Comparator<? super E> compare;
/**
* Constructs a PriorityQueue of size 0, adds null - index zero will
* not be used, sets compare to null (will use comparable)
*/
public PriorityQueue() {
elements = new ArrayList<E>();
elements.add(null);
compare = null;
}
/**
* Constructs a PriorityQueue of size 0, adds null - index
* will not be used, sets compare to the specified comparator
* @param comparator the comparator to be used in comparing the elements
*/
public PriorityQueue(Comparator<? super E> comparator) {
elements = new ArrayList<E>();
elements.add(null);
compare = comparator;
}
/**
* Compares the element at ind with the specified element
* @param element the element to compare to the element at index ind
* @param ind the index containing an element being compared
* @return Returns 1 if the element at index ind is greater than
* element, returns -1 otherwise
*/
private int comp(E element, int ind) {
if(compare == null) {
if(element.compareTo(elements.get(ind)) < 0) {
return -1;
}
return 1;
}
if(compare.compare(element, elements.get(ind)) < 0) {
return -1;
}
return 1;
}
/**
* Adds an element to the PriorityQueue
* @param e the element to be added to the PriorityQueue
* @return true - the element has been added
*/
public boolean push(E e) {
elements.add(e);
int index = elements.size()-1;
while(index/2 != 0 && comp(e, index/2) < 0 ) {
Collections.swap(elements,index, index/2);
index /= 2;
}
return true;
}
/**
* Clears the PriorityQueue
*/
public void clear() {
elements = new ArrayList<E>();
elements.add(null);
}
/**
* Returns the comparator used - if no comparator was provided
* in the constructor it returns null
* @return the comparator provided, or null if none was
*/
public Comparator<? super E> comparator() {
return compare;
}
/**
* Checks if an element is present in the PriorityQueue
* @param o the element to check
* @return true if the element is present, false otherwise
*/
public boolean contains(Object o) {
return elements.contains(o);
}
/**
* Returns the first element in the PriorityQueue (without removing)
* @return the first element
*/
public E peek() {
return elements.get(1);
}
/**
* Removes the first element in the PriorityQueue
* @return the first element
*/
public E pop() {
final E element = elements.get(1);
Collections.swap(elements, 1, elements.size()-1);
elements.remove(elements.size()-1);
int currInd = 1;
int newInd = findNewInd(currInd);
if(newInd == -1) {
return element;
}
while(comp(elements.get(currInd), newInd) >= 0) {
Collections.swap(elements, currInd, newInd);
currInd = newInd;
newInd = findNewInd(currInd);
if(newInd == -1) break;
}
return element;
}
private int findNewInd(int curr) {
if(curr * 2 >= elements.size()){
return -1;
}
if(curr * 2 + 1 >= elements.size()) {
if(curr * 2 < elements.size()){
return curr * 2;
}
return -1;
}
E el1 = elements.get(curr*2);
E el2 = elements.get(curr*2+1);
if(compare == null) {
if(el1.compareTo(el2) <= 0) {
return curr*2;
}
return curr*2+1;
}
else if(compare.compare(el1,el2) <= 0) {
return curr * 2;
}
return curr*2 + 1;
}
/**
* Gives the size of the PriorityQueue(including null, index 0)
* @return the size of elements
*/
public int size() {
return elements.size()-1;
}
public String toString() {
return elements.toString();
}
}