- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.java
More file actions
Latest commit
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
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Collections;
importjava.util.Comparator;
/**
* Implementation of a PriorityQueue using internal ArrayList
* Min-heap
*/
publicclassPriorityQueue<EextendsComparable<E>> {
/**
* the List containing the elements of the PriorityQueue
*/
privateList<E> elements;
/**
* Comparator to determine priority of elements
*/
privateComparator<? superE> compare;
/**
* Constructs a PriorityQueue of size 0, adds null - index zero will
* not be used, sets compare to null (will use comparable)
*/
publicPriorityQueue() {
elements = newArrayList<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
*/
publicPriorityQueue(Comparator<? superE> comparator) {
elements = newArrayList<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
*/
privateintcomp(Eelement, intind) {
if(compare == null) {
if(element.compareTo(elements.get(ind)) < 0) {
return -1;
}
return1;
}
if(compare.compare(element, elements.get(ind)) < 0) {
return -1;
}
return1;
}
/**
* Adds an element to the PriorityQueue
* @param e the element to be added to the PriorityQueue
* @return true - the element has been added
*/
publicbooleanpush(Ee) {
elements.add(e);
intindex = elements.size()-1;
while(index/2 != 0 && comp(e, index/2) < 0 ) {
Collections.swap(elements,index, index/2);
index /= 2;
}
returntrue;
}
/**
* Clears the PriorityQueue
*/
publicvoidclear() {
elements = newArrayList<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
*/
publicComparator<? superE> comparator() {
returncompare;
}
/**
* Checks if an element is present in the PriorityQueue
* @param o the element to check
* @return true if the element is present, false otherwise
*/
publicbooleancontains(Objecto) {
returnelements.contains(o);
}
/**
* Returns the first element in the PriorityQueue (without removing)
* @return the first element
*/
publicEpeek() {
returnelements.get(1);
}
/**
* Removes the first element in the PriorityQueue
* @return the first element
*/
publicEpop() {
finalEelement = elements.get(1);
Collections.swap(elements, 1, elements.size()-1);
elements.remove(elements.size()-1);
intcurrInd = 1;
intnewInd = findNewInd(currInd);
if(newInd == -1) {
returnelement;
}
while(comp(elements.get(currInd), newInd) >= 0) {
Collections.swap(elements, currInd, newInd);
currInd = newInd;
newInd = findNewInd(currInd);
if(newInd == -1) break;
}
returnelement;
}
privateintfindNewInd(intcurr) {
if(curr * 2 >= elements.size()){
return -1;
}
if(curr * 2 + 1 >= elements.size()) {
if(curr * 2 < elements.size()){
returncurr * 2;
}
return -1;
}
Eel1 = elements.get(curr*2);
Eel2 = elements.get(curr*2+1);
if(compare == null) {
if(el1.compareTo(el2) <= 0) {
returncurr*2;
}
returncurr*2+1;
}
elseif(compare.compare(el1,el2) <= 0) {
returncurr * 2;
}
returncurr*2 + 1;
}
/**
* Gives the size of the PriorityQueue(including null, index 0)
* @return the size of elements
*/
publicintsize() {
returnelements.size()-1;
}
publicStringtoString() {
returnelements.toString();
}
}