forked from Hithomelabs/Princeton1
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
38de5fe0a5 |
@ -0,0 +1,18 @@
|
||||
package com.hithomelabs.clients.module8;
|
||||
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import com.hithomelabs.princeton1.module8.PriorityQueue;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class PriorityQueueClient {
|
||||
public static void main(String[] args){
|
||||
PriorityQueue<Apple> priorityQueue = new PriorityQueue<>();
|
||||
for(int i=0; i<100; i++){
|
||||
priorityQueue.enqueue(new Apple(new Random().nextInt(100)));
|
||||
}
|
||||
while (!priorityQueue.isEmpty()){
|
||||
System.out.println(priorityQueue.dequeue());
|
||||
}
|
||||
}
|
||||
}
|
@ -13,6 +13,7 @@ dependencies {
|
||||
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||
implementation project(':module5')
|
||||
implementation project(':module4')
|
||||
testImplementation project(':common')
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ public class Heap implements MeasurableHelper {
|
||||
}
|
||||
|
||||
// * * Swim if element is not root, and parent is lesser than node
|
||||
public static <T extends Comparable<T>> void swim(T[] arr, int node, int N, Comparator<T> cmp, SortingMetaData metaData){
|
||||
public static <T> void swim(T[] arr, int node, int N, Comparator<T> cmp, SortingMetaData metaData){
|
||||
while(node > 1){
|
||||
if(! MeasurableHelper.less(arr[node/2],arr[node], cmp, metaData)) break;
|
||||
MeasurableHelper.exch(arr, node, node/2, metaData);
|
||||
|
@ -0,0 +1,66 @@
|
||||
package com.hithomelabs.princeton1.module8;
|
||||
|
||||
import com.hithomelabs.princeton1.module4.Queue;
|
||||
import com.hithomelabs.princeton1.module5.ComparableHelper;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class PriorityQueue<E> extends Queue<E> implements ComparableHelper {
|
||||
private int size;
|
||||
private E[] arr;
|
||||
private static final int CAPACITY = 100;
|
||||
private int capacity;
|
||||
|
||||
public PriorityQueue(){
|
||||
this(CAPACITY);
|
||||
}
|
||||
|
||||
public PriorityQueue(int capacity){
|
||||
arr = (E[]) new Object[capacity+1];
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E dequeue() {
|
||||
if(isEmpty())
|
||||
return null;
|
||||
E value = arr[1];
|
||||
ComparableHelper.exch(arr, 1, size);
|
||||
arr[size--] = null;
|
||||
Heap.sink(arr, 1, size, null, null);
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enqueue(E element) {
|
||||
arr[++size] = element;
|
||||
Heap.swim(arr, size, size, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator iterator() {
|
||||
return new Iterator() {
|
||||
|
||||
int index = 1;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return index <= size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object next() {
|
||||
return arr[index++];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user