Binary Heaps (Chapters 20.3 – 20.5) Leftist Heaps Binary Heaps Are Arrays!
Total Page:16
File Type:pdf, Size:1020Kb
Binary heaps (chapters 20.3 – 20.5) Leftist heaps Binary heaps are arrays! A binary heap is really implemented using an array! 8 Possible because 18 29 of completeness 20 28 39 66 property 37 26 76 32 74 89 0 1 2 3 4 5 6 7 8 9 10 11 12 8 18 29 20 28 39 66 37 26 76 32 74 89 Child positions The left child of node i ...the right child is at index 2i + 1 8 is at index 2i + 2 in the array... 18 29 20 28 39 66 37 26 76 32 74 89 0 1 2 3 4 5 6 7 8 9 10 11 12 8 18 29 20 28 39 66 37 26 76 32 74 89 L R P . a . C r C e h h n i i l t l d d Child positions The left child of node i ...the right child is at index 2i + 1 8 is at index 2i + 2 in the array... 18 29 20 28 39 66 37 26 76 32 74 89 0 1 2 3 4 5 6 7 8 9 10 11 12 8 18 29 20 28 39 66 37 26 76 32 74 89 R L P . a . C r C e h h n i i l t l d d Child positions The left child of node i ...the right child is at index 2i + 1 8 is at index 2i + 2 in the array... 18 29 20 28 39 66 37 26 76 32 74 89 0 1 2 3 4 5 6 7 8 9 10 11 12 8 18 29 20 28 39 66 37 26 76 32 74 89 L R P . a . C r C e h h n i i l t l d d Parent position The parent of node i 8 is at index (i-1)/2 18 29 20 28 39 66 37 26 76 32 74 89 0 1 2 3 4 5 6 7 8 9 10 11 12 8 18 29 20 28 39 66 37 26 76 32 74 89 P C a h r i e l d n t Reminder: inserting into a binary heap To insert an element into a binary heap: ● Add the new element at the end of the heap ● Sift the element up: while the element is less than its parent, swap it with its parent e can do exactly the same thing for a binary heap represented as an array! Inserting into a binary heap Step !: add the new element to the end of the array, set child to its index 6 18 29 20 28 39 66 C 37 26 76 32 74 89 8 h i l d 0 1 2 3 4 5 6 7 8 9 10 11 12 13 6 18 29 20 28 39 66 37 26 76 32 74 89 8 Inserting into a binary heap Step ": compute parent = (child-1)/2 6 18 29 20 28 39 66 37 26 76 32 74 89 8 P a C r h e i n l d t 0 1 2 3 4 5 6 7 8 9 10 11 12 13 6 18 29 20 28 39 66 37 26 76 32 74 89 8 Inserting into a binary heap Step #: if array[parent] $ array[child], swap them 6 18 29 20 28 39 8 37 26 76 32 74 89 66 P a C r h e i n l d t 0 1 2 3 4 5 6 7 8 9 10 11 12 13 6 18 29 20 28 39 8 37 26 76 32 74 89 66 Inserting into a binary heap Step %: set child = parent, parent = (child – 1) / 2, and repeat 6 18 29 20 28 39 8 P C a 37 26 76 32 74 89 66 r h e i l n d t 0 1 2 3 4 5 6 7 8 9 10 11 12 13 6 18 29 20 28 39 8 37 26 76 32 74 89 66 Inserting into a binary heap Step %: set child = parent, parent = (child – 1) / 2, and repeat 6 18 8 20 28 39 29 P C a 37 26 76 32 74 89 66 r h e i l n d t 0 1 2 3 4 5 6 7 8 9 10 11 12 13 6 18 8 20 28 39 29 37 26 76 32 74 89 66 Binary heaps as arrays &inary heaps are “morally( trees ● This is how we )iew them when we design the heap algorithms &ut we implement the tree as an array ● The actual implementation translates these tree concepts to use arrays In the rest of the lecture, we will only show the heaps as trees ● &ut you should ha)e the 'array )iew( in your head when looking at these trees Building a heap ,ne more operation, build heap ● Takes an arbitrary array and makes it into a heap ● *n-place: moves the elements around to ma+e the heap property hold *dea: ● .eap property: each element must be less than its children ● *f a node breaks this property, we can fix it by sifting down ● So simply looping through the array and sifting down each element in turn ought to fix the in)ariant ● &ut when we sift an element down, its children must already ha)e the heap property 0otherwise the sifting doesn1t work2 ● To ensure this, loop through the array in reverse Building a heap 3o through elements in reverse order, sifting each down 25 13 28 37 32 29 6 20 26 18 28 31 18 Building a heap 4ea)es never need sifting down! 25 13 28 37 32 29 6 20 26 18 28 31 18 Building a heap "5 is greater than 16 so needs swapping 25 13 28 37 32 18 6 20 26 18 28 31 29 Building a heap #" is greater than 16 so needs swapping 25 13 28 37 18 18 6 20 26 32 28 31 29 Building a heap #7 is greater than 28 so needs swapping 25 13 28 20 18 18 6 37 26 32 28 31 29 Building a heap "6 is greater than 6 so needs swapping 25 13 6 20 18 18 28 37 26 32 28 31 29 Build heap complexity :ou would expect ,0n log n2 complexity: ● n 'sift down( operations ● each sift down has ,0log n) complexity Actually, it1s O0n2! See boo+ 28.#. ● 0Rough reason: sifting down is most expensi)e for elements near the root of the tree, but the )ast majority of elements are near the lea)es2 Heapsort To sort a list using a heap: ● start with an empty heap ● add all the list elements in turn ● repeatedly /nd and remo)e the smallest element from the heap, and add it to the result list 0this is a kind of selection sort2 .owe)er, this algorithm is not in-place. .eapsort uses the same idea, but without allocating any extra memory. Heapsort, in-place e are going to repeatedly remo)e the largest )alue from the array and put it in the right place ● using a so-called max heap, a heap where you can /nd and delete the maximum element instead of the minimum e'll divide the array into two parts ● The /rst part will be a heap ● The rest will contain the )alues we')e remo)ed (This division is the same idea we used for in-place selection and insertion sort) Heapsort, in-place =irst turn the array into a heap Then just repeatedly delete the minimum element! Remember the deletion algorithm: ● Swap the maximum 0first) element with the last element of the heap ● Reduce the size of the heap by ! 0deletes the /rst element, breaks the in)ariant) ● Sift the first element down 0repairs the in)ariant) The swap actually puts the maximum element in the right place in the array Trace of heapsort =irst build a heap 0not shown2 89 76 74 37 32 39 66 20 26 18 28 29 6 Trace of heapsort Step !: swap maximum and last element; decrease si>e of heap by ! 89 76 74 37 32 39 66 20 26 18 28 29 6 Trace of heapsort Step ": sift /rst element down 6 76 74 37 32 39 66 &lue – sorted 20 26 18 28 29 89 part of array, not part of heap Trace of heapsort Step ": sift first element down 76 6 74 37 32 39 66 20 26 18 28 29 89 Trace of heapsort Step ": sift /rst element down 76 37 74 6 32 39 66 20 26 18 28 29 89 Trace of heapsort Step ": sift /rst element down 76 37 74 26 32 39 66 20 6 18 28 29 89 Trace of heapsort e now ha)e the biggest element at the end of the array, and a heap that1s one element smaller! 76 37 74 26 32 39 66 20 6 18 28 29 89 Trace of heapsort Step !: swap maximum and last element; decrease si>e of heap by ! 76 37 74 26 32 39 66 20 6 18 28 29 89 Trace of heapsort Step ": sift /rst element down 29 37 74 26 32 39 66 20 6 18 28 76 89 Trace of heapsort Step ": sift /rst element down 74 37 29 26 32 39 66 20 6 18 28 76 89 Trace of heapsort Step ": sift /rst element down 74 37 66 26 32 39 29 20 6 18 28 76 89 Trace of heapsort 74 37 66 26 32 39 29 20 6 18 28 76 89 Trace of heapsort Step !: swap maximum and last element; decrease si>e of heap by ! 74 37 66 26 32 39 29 20 6 18 28 76 89 Trace of heapsort Step ": sift /rst element down 28 37 66 26 32 39 29 20 6 18 74 76 89 Trace of heapsort Step ": sift /rst element down 66 37 28 26 32 39 29 20 6 18 74 76 89 Trace of heapsort Step ": sift /rst element down 66 37 39 26 32 28 29 20 6 18 74 76 89 Trace of heapsort 66 37 39 26 32 28 29 20 6 18 74 76 89 Complexity o heapsort &uilding the heap ta+es O0n2 time e delete the maximum element n times, each deletion ta+ing ,0log n2 time .ence the total complexity is O0n log n2 !arning ,ur formulas for finding children and parents in the array assume 0-based arrays The book, for some reason, uses 1-based arrays (and later switches to 8-based arrays)! *n a heap implemented using a 1-based array: ● the left child of index i is index 2i ● the right child is index 2i+1 ● the parent is index i/2 &e careful when doing the lab! Summary of binary heaps &inary heaps: ,0log n2 insert, O0!2 find minimum, O0log n2 delete minimum ● A complete binary tree with the heap property, represented as an array .eapsort: build a max heap, repeatedly remo)e last element and place at end of array ● Aan be done in-place, ,(n log n) *n fact, heaps were originally in)ented for heapsort! #e tist heaps $erging t%o heaps Another operation we might want to do is merge two heaps ● &uild a new heap with the contents of both heaps ● e.g., merging a heap containing 1, 2, 8, 5, !8 and a heap containing 3, 4, 5, 6, 7 gives a heap containing 1, 2, #, %, B, 6, 7, 6, 5, !8 =or our earlier naï)e priority Dueues: ● An unsorted array: concatenate the arrays ● A sorted array: merge the arrays (as in mergesort) =or binary heaps: ● Ta+es ,(n) time because you need to at least copy the contents of one heap to the other Can't combine two arrays in less than ,0n2 time! $erging tree-based heaps 3o bac+ to our idea of a binary tree with the heap property: 8 18 29 20 28 39 66 37 32 74 89 *f we can merge two of these trees, we can implement insertion and delete minimum! 0 e'll see how to implement merge later) Insertion To insert a single element: ● build a heap containing just that one element ● merge it into the existing heap! E.g., inserting 1" A tree with <ust one node 8 18 29 + 12 20 28 39 66 37 32 74 89 &elete minimum To delete the minimum element: ● ta+e the left and right branches of the tree ● these contain e)ery element except the smallest ● merge them! E.g., deleting 8 from the previous heap 18 29 20 28 + 39 66 37 32 74 89 Heaps based on merging *f we can ta+e trees with the heap property, and implement merging with O0log n2 complexity, we get a priority queue with: ● ,0!2 /nd minimum ● ,0log n) insertion 0by merging2 ● ,0log n) delete minimum 0by merging2 ● plus this useful merge operation itself There are lots of heaps based on this idea: ● s+ew heaps, Fibonacci heaps, binomial heaps e will study one: leftist heaps 'ai(e merging !.