Download presentation
Presentation is loading. Please wait.
Published byLacey Wilcoxson Modified over 9 years ago
1
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
2 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 A BC D E FG HIJ ABCDEFGHIJ 0123456789101112 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
3 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Priority Queue Definition Operations Binary Heap Operations Properties Representation Heap Sort Outline
4
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
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
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
7 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 1 2 23 1 2 3 2 1 3 2 2 1 2 2 3 13 2116 6 31 1968 652632 Which Of These Are Binary Heaps?
8
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) / 2 01 43 3 32 65 5840424 0143332655840424 01234567891011121314 Heap Representation
9
9 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Find Minimum How to access the minimum element? Simple! Return root. Constant time operation 13 2116 26 31 1968 652632
10
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
11 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 4042 01 43 5 38 65 584 2 0143538655840424 01234567891011121314 231 Insertion Insert 2 (Percolate Up)
12
12 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 4042 01 43 5 28 65 584 3 0143538655840424 01234567891011121314 321 Insertion Insert 2 (Percolate Up)
13
13 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insert 14 Insertion 14 13 2116 24 31 1968 652632
14
14 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insert 14 Insertion 31 13 2116 24 14 1968 652632
15
15 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insert 14 Insertion 31 13 1416 24 21 1968 652632
16
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
17 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 0143332655840424 0 12345678910111213 01 43 3 32 65 5840424 14 Delete Minimum Percolate Down
18
18 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 0143332655840442 0 12345678910111213 4 01 43 3 32 65 584042 14 Delete Minimum
19
19 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 3143432655840042 0 12345678910111213 0 31 43 4 32 65 584042 14 Delete Minimum: Completed
20
20 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 13 1416 19 21 1968 65263231 Delete Min (Alternative)
21
21 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 1416 19 21 1968 652632 31 Delete Min (Alternative) Percolate Down
22
22 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Min (Alternative) 14 16 19 21 1968 652632 31
23
23 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 14 1916 21 1968 652632 31 Delete Min (Alternative)
24
24 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Min (Alternative) 14 1916 26 21 1968 6532 31
25
25 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Min (Alternative) 14 1916 26 21 1968 653132 Instead of lots of swapping, simply store value at the beginning, move “hole” around, and plug value in at the end.
26
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
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
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
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
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
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
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! 20 6117 12 5537 45 2564 63 8373 92 4721
33
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! 20 6117 12 5537 45 2564 63 8373 92 4721 n
34
34 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(6) 20 6117 12 5537 45 2564 63 8373 92 4721
35
35 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(5) 20 6117 12 5537 45 2564 63 8373 92 4721 25 45
36
36 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(4) 20 6117 12 5537 25 4564 63 8373 92 4721
37
37 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(3) 20 6117 12 5537 25 4564 63 8373 92 4721 20 17
38
38 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(2) 17 6120 12 5537 25 4564 63 8373 92 4721
39
39 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(1) 17 6120 12 5537 25 4564 63 8373 92 4721 12 37 47
40
40 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(0) 17 6120 37 5547 25 4564 63 8373 92 1221 17 20 92 12
41
41 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 97 5359 26 41 5831 162136 53592641583116213697 012345678910111213 Max Heap Heaps can also store maximum item (instead of min).
42
42 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 59 5358 26 41 3631 1621 53582641363116219759 012345678910111213 97 Heap after the first deleteMax
43
43 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 58 5336 26 41 2131 16 53362641213116599758 012345678910111213 9759 Heap after second deleteMax
44
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
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.