Skip to content

Instantly share code, notes, and snippets.

@t3rmin4t0r
Last active October 19, 2020 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t3rmin4t0r/a1291c5bb00f44832cb097393c065372 to your computer and use it in GitHub Desktop.
Save t3rmin4t0r/a1291c5bb00f44832cb097393c065372 to your computer and use it in GitHub Desktop.
You would expect a PriorityHeap to push down an item, but what if it actually does it upwards & is a PriorityQueue instead
package org.notmysock.test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.PriorityQueue;
import org.junit.jupiter.api.Test;
class PriorityQueueTest {
public static class EqObject implements Comparable {
private final int n;
public EqObject(int v) {
this.n = v;
}
@Override
public int compareTo(Object o) {
return 0;
}
@Override
public String toString() {
return String.format("EqObject#%d", n);
}
}
void cycle(PriorityQueue<EqObject> priorityQueue, int c) {
EqObject horse = priorityQueue.poll();
EqObject next = priorityQueue.peek();
priorityQueue.add(horse);
EqObject current = priorityQueue.peek();
System.out.println(String.format(
"Cycle%d - took out %s, top of heap %s and replaced to get %s again", c,
horse, next, current));
}
@Test
void test() {
PriorityQueue<EqObject> priorityQueue = new PriorityQueue<EqObject>();
EqObject span1 = new EqObject(1);
EqObject span2 = new EqObject(2);
EqObject span3 = new EqObject(3);
priorityQueue.add(span1);
priorityQueue.add(span2);
priorityQueue.add(span3);
for (int i = 0; i < 10; i++) {
cycle(priorityQueue, i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment