diff --git a/priorityqueue/heap/HeapPriorityQueue.java b/priorityqueue/heap/HeapPriorityQueue.java new file mode 100644 index 0000000..3038628 --- /dev/null +++ b/priorityqueue/heap/HeapPriorityQueue.java @@ -0,0 +1,146 @@ +package priorityqueue.heap; +import exceptions.EmptyPriorityQueueException; +import position.Position; +import priorityqueue.Entry; +import priorityqueue.PriorityQueue; +import tree.binarytree.heap.ArrayListCompleteBinaryTree; +import tree.binarytree.heap.CompleteBinaryTree; +import utility.DefaultComparator; + +import java.security.InvalidKeyException; +import java.util.Comparator; + + +public class HeapPriorityQueue implements PriorityQueue { + + protected CompleteBinaryTree> heap; + protected Comparator comp; + + + protected static class MyEntry implements Entry { + protected K key; + protected V value; + public MyEntry(K k, V v) { key = k; value = v; } + public K getKey() { return key; } + public V getValue() { return value; } + public String toString() { return "(" + key + "," + value + ")"; } + } + + + public HeapPriorityQueue() { + heap = new ArrayListCompleteBinaryTree>(); + comp = new DefaultComparator(); + } + + public HeapPriorityQueue(Comparator c) { + heap = new ArrayListCompleteBinaryTree>(); + comp = c; + } + + //elementi gia' ordinati + public HeapPriorityQueue (K k[], V v[], Comparator C){ + heap = new ArrayListCompleteBinaryTree>(); + comp = C; + + if ( k == null || v == null || v.length != k.length ){ + + } else { + + for (int i = 0; i < k.length; i++) { + heap.add(new MyEntry(k[i],v[i])); // gia' ordinati. evitiamo l'upheap + } + } + + } + + public void setComparator(Comparator c) throws IllegalStateException { + if(!isEmpty()) + throw new IllegalStateException("Priority queue is not empty"); + comp = c; + } + + public int size() { return heap.size(); } + + public boolean isEmpty() { return heap.size() == 0; } + + public Entry min() throws EmptyPriorityQueueException { + if (isEmpty()) + throw new EmptyPriorityQueueException("Priority queue is empty"); + return heap.root().element(); + } + + + public Entry insert(K k, V x) throws InvalidKeyException { + checkKey(k); + Entry entry = new MyEntry(k,x); + upHeap(heap.add(entry)); + return entry; + } + + + + public Entry removeMin() throws EmptyPriorityQueueException { + if (isEmpty()) + throw new EmptyPriorityQueueException("Priority queue is empty"); + Entry min = heap.root().element(); + if (size() == 1) + heap.remove(); + else { + heap.replace(heap.root(), heap.remove()); + downHeap(heap.root()); + } + return min; + } + + + + + protected void checkKey(K key) throws InvalidKeyException { + try { + comp.compare(key,key); + } + catch(Exception e) { + throw new InvalidKeyException("Invalid key"); + } + } + + + protected void upHeap(Position> v) { + Position> u; + while (!heap.isRoot(v)) { + u = heap.parent(v); + if (comp.compare(u.element().getKey(), v.element().getKey()) <= 0) break; + swap(u, v); + v = u; + } + } + + protected void downHeap(Position> r) { + while (heap.isInternal(r)) { + Position> s; + if (!heap.hasRight(r)) + s = heap.left(r); + else if (comp.compare(heap.left(r).element().getKey(), heap.right(r).element().getKey()) <=0) + s = heap.left(r); + else + s = heap.right(r); + if (comp.compare(s.element().getKey(), r.element().getKey()) < 0) { + swap(r, s); + r = s; + } + else + break; + } + } + + protected void swap(Position> x, Position> y) { + Entry temp = x.element(); + heap.replace(x, y.element()); + heap.replace(y, temp); + } + + public String toString() { + return heap.toString(); + } + +}