# Priority Queues1 Priority Queues (Antrian Berprioritas)

## Presentation on theme: "Priority Queues1 Priority Queues (Antrian Berprioritas)"— Presentation transcript:

Priority Queues1 Priority Queues (Antrian Berprioritas)

PQ Two kinds of priority queues: Min priority queue. Max priority queue.

Min Priority Queue Collection of elements. Each element has a priority or key. Supports following operations: – Empty – Size – insert an element into the priority queue (push) – get element with min priority (top) – remove element with min priority (pop)

Max Priority Queue Collection of elements. Each element has a priority or key. Supports following operations: – Empty – Size – insert an element into the priority queue (push) – get element with max priority (top) – remove element with max priority (pop)

Applications Sorting use element key as priority insert elements to be sorted into a priority queue remove/pop elements in priority order – if a min priority queue is used, elements are extracted in ascending order of priority (or key) – if a max priority queue is used, elements are extracted in descending order of priority (or key)

Sorting Example Sort five elements whose keys are 6, 8, 2, 4, 1 using a max priority queue. – Insert the five elements into a max priority queue. – Do five remove max operations placing removed elements into the sorted array from right to left.

After Inserting Into Max Priority Queue

After First Remove Max Operation

Min Tree Definition Each tree node has a value. Value in any node is the minimum value in the subtree for which that node is the root. Equivalently, no descendent has a smaller value.

Priority Queues10 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue ADT – insert(k, x) inserts an entry with key k and value x – removeMin() removes and returns the entry with smallest key Additional methods – min() returns, but does not remove, an entry with smallest key – size(), isEmpty() Applications: – Standby flyers – Auctions – Stock market

Min Tree Example

Min Heap Definition complete binary tree min tree

A Heap Is Efficiently Represented As An Array

Interface IBT public interface IBT { public boolean isEmpty(); public boolean isFull(); public void sisipData(int dt); public int hapusData(); }

MaxHeapTree public class BinaryMaxHeap implements IBT{ private static int maxSize = 100; private int counter; private int []data; public BinaryMaxHeap(){} public boolean isFull(){return counter == maxSize;} public boolean isEmpty(){return counter == 0;} public void sisipData(int value) {//} private void siftUp(int id) { //} public int hapusData() { //} private void siftDown(int id) { // } public void cetak(){ // } }

BinaryMaxHeap public BinaryMaxHeap(){ counter = 0; data = new int[maxSize]; }

isFull() dan isEmpty() public boolean isFull(){ return counter == maxSize; } public boolean isEmpty(){ return counter == 0; }

sisipData(int value) public void sisipData(int value) { if (!isFull()){ data[++counter] = value; siftUp(counter); }

private void siftUp(int id) private void siftUp(int id) { int idInduk, tmp; if (id != 0) { idInduk = id/2; if (idInduk != 0){ if (data[idInduk] < data[id]) { tmp = data[idInduk]; data[idInduk] = data[id]; data[id] = tmp; siftUp(idInduk); }

hapusData() public int hapusData() { int itemHapus = -999; if (!isEmpty()){ itemHapus = data[1]; data[1] = data[counter--]; if (counter > 1) siftDown(1); } return itemHapus; }

private void siftDown(int id) private void siftDown(int id) { int idAnakKiri, idAnakKanan, idMax, tmp; idAnakKiri = id*2; idAnakKanan = id*2 + 1; if (idAnakKanan > counter) { if (idAnakKiri > counter) return; else idMax = idAnakKiri; } else { if (data[idAnakKiri] >= data[idAnakKanan]) idMax = idAnakKiri; else idMax = idAnakKanan; } //swap data if (data[id] < data[idMax]) { tmp = data[idMax]; data[idMax] = data[id]; data[id] = tmp; siftDown(idMax); }

public void cetak() public void cetak(){ for (int i=1; i<counter+1; i++){ System.out.print(data[i]+" "); }

public static void main(String[] args) { BinaryMaxHeap bmax = new BinaryMaxHeap(); bmax.sisipData(9);bmax.sisipData(3);bmax.sisipData(10); bmax.sisipData(42);bmax.sisipData(9);bmax.sisipData(70); bmax.sisipData(23);bmax.sisipData(11);bmax.sisipData(7); bmax.sisipData(8);bmax.sisipData(13);bmax.sisipData(900); bmax.cetak(); System.out.println(); while (!bmax.isEmpty()){ System.out.print(bmax.hapusData()+" "); }

Priority Queues25 Total Order Relations (§ 7.1.1) Keys in a priority queue can be arbitrary objects on which an order is defined Two distinct entries in a priority queue can have the same key Mathematical concept of total order relation  – Reflexive property: x  x – Antisymmetric property: x  y  y  x  x = y – Transitive property: x  y  y  z  x  z

Similar presentations