3import java.util.Comparator;
52 private static final int MINENTRIES=64;
90 @Override
public T
pop() {
95 position_.
insert((heap_.data_[0]=heap_.
back()),0);
106 int getParent(
int k) {
return ((k+1)/2)-1; }
108 int getLeftChild(
int k) {
return ((k+1)*2)-1; }
110 boolean less(
int i,
int j) {
111 return super.less(heap_.
at(i),heap_.
at(j));
114 boolean leq(
int i,
int j) {
115 if (less(i,j))
return true;
116 else return !less(j,i);
119 boolean less(T x,
int k) {
120 return super.less(x,heap_.
at(k));
123 void move(
int from,
int to) {
124 T x=heap_.data_[from];
129 void set(
int k,T x) {
135 void downheap(
int k) {
137 int j=getLeftChild(k);
139 while (j<heap_.
size()) {
140 if (j<heap_.
size()-1 && less(j+1,j)) j=j+1;
141 if (less(x,j))
break;
153 @Override
public void push(T x) {
155 throw new RuntimeException(x.toString()+
" already contained in PQ");
168 while (k>0 && less(x,p=getParent(k))) {
177 public void raise(T x) {
179 int k=position_.
find(x);
181 assert(k==0 || less(getParent(k),k));
189 int k=position_.
find(x);
192 assert(getLeftChild(k) >=heap_.
size() || less(k,getLeftChild(k)));
193 assert(getLeftChild(k)+1>=heap_.
size() || less(k,getLeftChild(k)+1));
204 System.err.println(
"size="+heap_.
size());
207 throw new RuntimeException(
"size mismatch");
208 for (
int i=0;i<heap_.
size();++i)
209 if (position_.
find(heap_.
at(i))==
null || position_.
find(heap_.
at(i))!=i)
210 throw new RuntimeException(
"invalid mapping for key='"+heap_.
at(i)+
"'");
213 for (
int i=0;i<heap_.
size();++i) {
217 throw new RuntimeException(
"heap property violated (parent");
219 int left=getLeftChild(i);
220 if (left<heap_.
size()) {
222 throw new RuntimeException(
"heap property violated (left)");
225 if (right<heap_.
size()) {
227 throw new RuntimeException(
"heap property violated (right)");
234 public static void main(String[] args) {
241 pq.
push(2); System.out.println(pq);
242 pq.
push(4); System.out.println(pq);
243 pq.
push(3); System.out.println(pq);
244 pq.
push(1); System.out.println(pq);
246 System.out.println(pq.
pop());
247 System.out.println(pq);
249 System.out.println(pq.
pop());
250 System.out.println(pq);
262 (
new Comparator<String>() {
263 @Override
public int compare(String a,String b) {
264 return priority.
find(a)-priority.
find(b);
270 pq.
push(
"b"); System.out.println(pq);
271 pq.
push(
"d"); System.out.println(pq);
272 pq.
push(
"c"); System.out.println(pq);
273 pq.
push(
"a"); System.out.println(pq);
275 System.out.println(pq.
pop());
276 System.out.println(pq);
278 System.out.println(pq.
pop());
279 System.out.println(pq);
282 pq.
push(
"b"); System.out.println(pq);
283 pq.
push(
"a"); System.out.println(pq);
287 System.out.println(
"front="+pq.
front());
289 System.out.println(
"new prioities: "+priority);
291 System.out.println(
"front="+pq.
front());
293 System.out.println(
"new prioities: "+priority);
295 System.out.println(
"front="+pq.
front());
Implementation of an unordered map based on a hash table.
Value insert(Key key, Value value)
insert key-value pair
Value find(Key key)
find value for key @endiliteral
Value remove(Key key)
remove entry (equivalent to insert(key,null))
boolean contains(Key key)
Is key contained in map?
int size()
get current number of entries
Priority queue based on binary min-heap.
int size()
get number of entries
boolean is_empty()
Is PQ empty?
void push(T x)
Add entry to PQ.
void checkConsistency()
check consistency
boolean contains(T x)
Is x contained in PQ?
PriorityQueue(int n, Comparator< T > cmp)
Create empty priority queue.
PriorityQueue(Comparator< T > cmp)
void raise(T x)
update heap after raising priority of x
T pop()
Pop minimal element from PQ.
T front()
Get minimal element.
void lower(T x)
update heap after lowering priority of x
static void main(String[] args)
example and test
Implementation of an array-based vector.
void pop_back()
remove last enytry [O(1)]
T back()
get last entry [O(1)]
void push_back(T obj)
insert new entry obj at the end [O(1) for sufficient capacity]
T at(int i)
get i-th entry [O(1)]
int size()
get number of entries [O(1)]
T front()
get first entry [O(1)]
void reserve(int n)
Ensure capacity for n entries.
boolean empty()
Is vector empty? [O(1)].
Interface for an ADT priority queue.
AuD lecture: Data structures, algorithms, examples.