Presentation is loading. Please wait.

Presentation is loading. Please wait.

Priority Queues1 Priority Queues (Antrian Berprioritas)

Similar presentations


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

1 Priority Queues1 Priority Queues (Antrian Berprioritas)

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

3 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)

4 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)

5 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)

6 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.

7 After Inserting Into Max Priority Queue

8 After First Remove Max Operation

9 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.

10 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

11 Min Tree Example

12 Min Heap Definition complete binary tree min tree

13

14 A Heap Is Efficiently Represented As An Array

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

16 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(){ // } }

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

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

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

20 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); }

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

22 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); }

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

24 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()+" "); }

25 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


Download ppt "Priority Queues1 Priority Queues (Antrian Berprioritas)"

Similar presentations


Ads by Google