
Priority queues, binary heaps, and heapsort (6.9, 21.1 – 21.5) Priority queue A priority queue is an ADT supporting three operations ● insert: add an element ● findMin: return the smallest element ● deleteMin: remo#e the smallest element A!!ows you to maintain a set of e!e"ents whi!e always knowing the sma!!est one 'ava: PriorityQueue<E> (!ass de%ining add, element and remove )"ple"enting a priority queue Several possible ways ● An unsorted array+ ● A sorted array+ ● A binary sear(h tree+ ● A ba!anced binary search tree+ A nicer way: a binary heap The heap property A tree satisfies the heap property if the #alue of each node is !ess than (or equal to) the value of its chi!dren: 8 -oot node is the s"a!lest – (an do find.in 18 29 in O(1) ti"e 20 28 39 66 37 32 74 89 ,hat (an we say about the root node+ Binary heap A binary heap is a complete binary tree that satisfies the heap property 8 18 29 20 28 39 66 37 26 76 32 74 89 Note: it is not a binary search tree! Complete "eans that a!l !evels except the botto" one are fu!!, and the botto" !evel is %i!!ed fro" !eft to right (see diagra") Binary heap invariant The binary heap invariant ● The tree must be complete ● It must have the heap property (each node is less than or equal to its chi!dren) Re"ember, al! our operations must preserve this invariant (Why this invariant+ See later!) Adding an e!ement to a binary heap Step 1: insert the e!ement at the ne3t empty position in the tree 8 18 29 20 28 39 66 37 26 76 32 74 89 12 This might break the heap invariant! )n this case, 12 is less than 66, its parent. Adding an e!ement to a binary heap Step 2: if the ne$ e!ement is !ess than its parent, swap it with its parent 8 18 29 20 28 39 12 37 26 76 32 74 89 66 The invariant is stil! broken, since 12 is !ess than 29, its new parent Adding an e!ement to a binary heap Repeat step 2 until the new e!e"ent is greater than or equal to its parent. 8 18 12 20 28 39 29 37 26 76 32 74 89 66 Now 12 is in its right place, and the invariant is restored. (Think about $hy this a!gorithm restores the invariant.) Why this works At every step, the heap property a!"ost holds except that the new element "ight be !ess than its parent After s$apping the element and its parent, still on!y the new element can be in the wrong p!ace (why?) 8 18 29 20 28 39 12 37 26 76 32 74 89 66 -e"oving the "inimu" ele"ent Step 1: replace the root e!e"ent with the last element in the heap 66 18 12 20 28 39 29 37 26 76 32 74 89 The invariant is broken, be(ause 66 is greater than its children -e"oving the "inimu" ele"ent Step 2: if the moved e!e"ent is greater than its children, swap it with its least child 12 18 66 20 28 39 29 37 26 76 32 74 89 (Why not its greatest (hi!d?) -e"oving the "inimu" ele"ent Step 3: repeat until the moved e!ement is !ess than or equal to its chi!dren 12 18 29 20 28 39 66 37 26 76 32 74 89 Sifting Two useful operations in i"plementing a heap Sift up: if an e!e"ent might be less than its parent, i.e. “too low6 (used in insert) ● -epeated!y swap the e!ement with its parent Sift down: if an element might be greater than its chi!dren, i.e. 5too high” (used in deleteMin) ● -epeated!y swap the e!ement with its least chi!d *u"mary so far Binary heap: complete binary tree where e#ery node is less than or equa! to its (hildren .ini"u" is root node Insert: add to end of tree and sift up ((a!!ed percolate up in book) Delete "inimu" s$ap with fina! e!ement and sift down ((a!led percolate down in book) Binary heaps are arrays! A binary heap is real!y imple"ented using an array: 8 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 7hild positions The le%t child of node i ...the right chi!d is at inde3 2i + 1 8 is at inde3 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 P R L a . r C C e h h n i i t l l d d 7hild positions The le%t child of node i ...the right chi!d is at inde3 2i + 1 8 is at inde3 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 P R L a . r C C e h h n i i t l l d d 7hild positions The le%t child of node i ...the right chi!d is at inde3 2i + 1 8 is at inde3 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 P L R a . r C C e h h n i i t l l d d 7hild positions The le%t child of node i ...the right chi!d is at inde3 2i + 1 8 is at inde3 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 P R L a . r C C e h h n i i t l l d d 7hild positions The le%t child of node i ...the right chi!d is at inde3 2i + 1 8 is at inde3 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 P R L a . r C C e h h n i i t l l d d Parent position The parent of node i 8 is at inde3 (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 ,hy the binary heap invariant The binary heap invariant: the tree is complete and satisfies the heap property ● Because of the heap property, we (an find the minimum element in O(1) ti"e ● Because the tree is complete, we can represent a heap o% n items with an array o% si8e n Warning ,e are using 9-based arrays, the ob#ious choice in 'ava The book, for some reason, uses 1-based arrays (and !ater switches to 0-based arrays)2 )n a heap imp!emented using a 1-based array ● the le%t chi!d o% index i is index 2i ● the right chi!d is index 2i+1 ● the parent is inde3 i/2 0e care%u! when doing the !ab2 -e"inder: inserting into a binary heap To insert an e!e"ent into a binary heap: ● Add the new element at the end o% the heap ● Si%t the element up: whi!e the element is less than its parent, swap it with its parent ,e (an just as we!! imple"ent this using the array representation! Just need to use index (al(ulations instead of fo!!owing !eft<right/parent !inks. )nserting into a binary heap Step 1: add the new e!ement to the end of the array, set child to its inde3 6 18 29 20 28 39 66 37 26 76 32 74 89 8 C 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 )nserting into a binary heap Step 2: 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 )nserting into a binary heap Step 3: if array[parent] > array[child], swap the" 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 )nserting into a binary heap Step 4: set child = parent, parent = (child – 1) / , and repeat 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 )nserting into a binary heap Step 4: set child = parent, parent = (child – 1) / , and repeat 6 18 8 20 28 39 29 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 8 20 28 39 29 37 26 76 32 74 89 66 The insertion a!gorith" )nsert # at the end of the array, let child be its index and parent = (child-1)/ ,hile child > $ and array[parent] > array[child] ● *$ap array[parent] and array[child] ● *et child = parent and parent = (child-1)/ Does many swap operations! .oves the new e!ement many times in the heap.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages104 Page
-
File Size-