IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Binary Heap
2 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 A BC D E FG HIJ ABCDEFGHIJ Review Complete binary tree: the tree is completely filled, with possible exception of the bottom level, which is filled from left to right. Array Implementation:
3 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Priority Queue Definition Operations Binary Heap Operations Properties Representation Heap Sort Outline
4 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 A Priority Queue A special kind of queue that features slightly different rules: Enqueue operation doesn’t always add new items to the rear of the queue. Instead, it insert each new item into the queue as determined by its priority. Remember Assignment 1: Passport Processing? Essentially, PQ is an Abstract Data Type where access is restricted to the minimum item only.
5 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Binary Heap Tree-based data structure where access is restricted to the minimum item only. Basic operations: Find the minimum item in constant time Insert a new item in logarithmic worst-case time Delete the minimum item in logarithmic worst-case time
6 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Structural Property Data is stored in a complete binary tree The tree is balanced, so all operations are guaranteed O(log n) worst case Can be implemented as array or list Ordering Property Heap Order: Parent Child Properties P X P X
7 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Which Of These Are Binary Heaps?
8 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Root at location 0 Left child of i is at location 2i + 1 Right child of i is at location 2i + 2 = 2(i + 1) Parent of i is at location (i - 1) / Heap Representation
9 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Find Minimum How to access the minimum element? Simple! Return root. Constant time operation
10 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insertion Create a “hole” in the next available location. Where? Bottom level, rightmost leaf. Move it up towards root to maintain heap order This is called “percolating up”
11 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Insertion Insert 2 (Percolate Up)
12 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Insertion Insert 2 (Percolate Up)
13 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insert 14 Insertion
14 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insert 14 Insertion
15 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insert 14 Insertion
16 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Minimum Only the root can be deleted. Why? Access is restricted to minimum item! Replace “hole” in the root with former last item. “Percolate down” towards leaf to maintain heap order Compare with smallest child, recurse.
17 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Minimum Percolate Down
18 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Minimum
19 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Minimum: Completed
20 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Min (Alternative)
21 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Min (Alternative) Percolate Down
22 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Min (Alternative)
23 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Min (Alternative)
24 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Min (Alternative)
25 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Min (Alternative) Instead of lots of swapping, simply store value at the beginning, move “hole” around, and plug value in at the end.
26 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Heap Operation sequence: Insert: 40, 20, 5, 55, 76, 31, 3 Delete Min Insert: 10, 22 Delete Min
27 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Heap Class Definition & Constructor public class MyHeap implements PriorityQueue { // Array implementation private int[] data; int currentSize; // Constructor public MyHeap(int maxSize) { data = new int[maxSize]; currentSize = 0; }
28 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Heap Indexing Methods // Returns index of an element’s parent private int parentOf(int i) {return (i - 1) / 2;} // Returns index of an element’s left child private int leftChildOf(int i) {return 2 * i + 1;} // Returns index of an element’s right child private int rightChildOf(int i) {return 2 * i + 2;} // Returns minimum value (i.e. heap root), a.k.a. findMin public int peek() {return data[0];}
29 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Removal & Insertion public int poll() // a.k.a. remove, dequeue { int minVal = peek(); data[0] = data[--currentSize]; if(currentSize > 1) percolateDown(0); return minVal; } public void add(int value) // a.k.a. offer { data[currentSize++]=value; percolateUp(currentSize-1); } (Actually, still need to check if polling empty array, adding to full array…) Implement?
30 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Percolate Up protected void percolateUp (int leaf) { int parent = parentOf(leaf); int value = data[leaf]; while(leaf>0 && value<data[parent]) { data[leaf]=data[parent]; leaf = parent; parent = parentOf(leaf); } data[leaf]=value; }
31 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Percolate Down private void percolateDown (int hole) { int value = data[hole]; while(leftChildOf(hole) < size()) { int child = leftChildOf(hole); if(rightChildOf(hole) < size() && data[rightChildOf(hole)] < data[child]) child = rightChildOf(hole); if(data[child] < value) { data[hole] = data[child]; hole = child; } data[hole] = value; }
32 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify The fixHeap operation takes a complete tree that does not have heap order and reinstates it. N insertions could be done in O(n log n) But we can make a fixHeap that takes O(n) time!
33 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 fixHeap Algorithm Inefficient version: fixHeap left subtree fixHeap right subtree percolateDown root However, we don’t need the recursive calls! Why? If we call percolateDown in reverse level order from the bottom (non- leaves) upwards, by the time we call percolateDown on node n, it’s children are already guaranteed to be heaps! n
34 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(6)
35 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(5)
36 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(4)
37 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(3)
38 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(2)
39 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(1)
40 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(0)
41 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Max Heap Heaps can also store maximum item (instead of min).
42 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Heap after the first deleteMax
43 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Heap after second deleteMax
44 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Heap Sort 1. Create a heap tree 2. Remove the root elements from the heap one at a time, recomposing the heap
45 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Priority queue can be implemented using binary heap Properties of binary heap structural property: complete binary tree ordering property: Parent Child Operations of binary heap insertion: O(log n) find min: O(1) delete min: O(log n) heapify: O(n) Binary heap can be used for sorting Summary