Download presentation

Presentation is loading. Please wait.

Published byLacey Wilcoxson Modified about 1 year 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 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 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) / 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

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 Insertion Insert 2 (Percolate Up)

12
12 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Insertion Insert 2 (Percolate Up)

13
13 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insert 14 Insertion

14
14 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insert 14 Insertion

15
15 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insert 14 Insertion

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 Delete Minimum Percolate Down

18
18 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Minimum

19
19 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Minimum: Completed

20
20 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Min (Alternative)

21
21 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Min (Alternative) Percolate Down

22
22 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Min (Alternative)

23
23 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Min (Alternative)

24
24 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Min (Alternative)

25
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
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

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!

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! n

34
34 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(6)

35
35 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(5)

36
36 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(4)

37
37 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(3)

38
38 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(2)

39
39 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(1)

40
40 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(0)

41
41 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Max Heap Heaps can also store maximum item (instead of min).

42
42 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Heap after the first deleteMax

43
43 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 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

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google