Chapter 6 Heaps
Total Page:16
File Type:pdf, Size:1020Kb
Introduction some systems applications require that items be processed Chapter 6 in specialized ways printing Heaps may not be best to place on a queue some jobs may be more ________________ small 1-page jobs should be printed before a 100-page job operating system _________________ processes run only for a slice of time queue: FIFO some jobs may take too long other jobs are more important and should not wait 2 Heap Model Heap Implementation specialized queue required heaps can be implemented with heap (priority queue) , with insertions at head provides at least insert _______________ deleteMin deleteMin: finds, returns, and removes min (or max) ordered linked list (worse due to number of ) other operations common insert used in other applications deleteMin external sorting binary search tree algorithms insert discrete event simulation deleteMin since only min is deleted, tree will be _______________ overkill since other included operations not required 3 4 Binary Heap Binary Heap binary heap common structure property simply termed heap completely filled two properties except level, which is filled from left to right ________________ complete binary tree has between and nodes heap _____________ height: operations can one of the properties can be represented with an array (no necessary) operation must continue until heap properties have been array position restored, which is typically simple left child in right child in parent in operations simple maximum heap size must be ______________________ 5 6 Binary Heap Binary Heap heap-order property allows operations to be performed quickly want to be able to find minimum quickly smallest element at ___________ any should also be a heap any node should be than all its descendants 7 8 Heap Operations Binary Heap two main heap operations example: insert 14 insert deleteMin insert need to maintain structure property create in next available location (at bottom of tree) place new value there if possible otherwise, slide into hole and bubble up hole continue until new value can be inserted termed strategy 9 10 Heap Operations Heap Operations insert (cont.) insert (cont.) could have used repeated swaps, but a swap requires could require as much as _____________ assignments on average, percolation terminates ___________ if element percolated up levels on average 2.607 comparisons are required swap method: assignments non-swap method: assignments if new element is smaller than all others in heap, hole will percolate to the __________ hole will be at index 1 and we will break out of the loop if extra check for 1 in loop, adds unnecessary time could place a copy of new value in position ____ 11 12 Heap Operations Binary Heap deleteMin example: deleteMin (13) finding minimum easy minimum more difficult hole is created at root element in complete binary tree must move if last element can be placed in hole, done child into hole hole slides down one level repeat until last element can be placed in hole worst case: average case: 13 14 Binary Heap Heap Operations example: deleteMin (13) (cont.) other heap operations finding minimum ___________ finding maximum not possible without scan through entire heap maximum in one of the leaves could use separate data structure, such as _____________ 15 16 Heap Operations Heap Operations other heap operations (cont.) other heap operations (cont.) decreaseKey buildHeap lowers value at position by given amount place items into the tree in any ____________ percolate up maintains property example: change process for more run time use percolateDown( ) from node increaseKey increases value at position by given amount percolate down example: drop process priority if taking ______________ remove decreaseKey to root, then deleteMin example: process is by user 17 18 Binary Heap Binary Heap example: start with random placement example: (cont.) start with percolateDown(7) each dashed line represents 2 comparisons 19 20 Heap Applications Heap Applications selection problem selection problem (cont.) from a list of elements, find th largest if heap is used original algorithm build heap of elements list and index th element perform deleteMin operations with simple sort, element extracted is th smallest element alternative algorithm if , read ____ elements into array and sort them another algorithm using heap smallest is in th position as in previous algorithm, but put elements in heap other elements processed one by one, placing them into is in th position the array other elements processed one by one, placing them into running time the array if , running time 21 22 Heap Applications -Heaps event simulation -heap bank where customers arrive and wait until one of tellers same as heap, but all nodes have children is available tends to be more than binary heap customer and based running time reduced to on probability distribution function deleteMin more expensive since more _______________ compute statistics on the length of time a customer must required wait, or the length of the line useful when heap is too large to fit entirely into main need to consider event that will occur in the least amount memory of time 4-heaps may outperform 2-heaps (binary heaps) heap can be used to order events 23 24 -Heaps Leftist Heaps example: -heap with one weakness of heaps so far is two heaps is difficult three data structures that can help heaps heaps binomial queues 25 26 Leftist Heaps Leftist Heaps leftist heaps leftist heaps (cont.) can be difficult to design a data structure for ____________ null path length (npl): length of shortest path from current that uses an array, but runs efficiently node to a node without children linked data structure therefore required npl of a node with zero or one child is 0 leftist heap npl of a null pointer is -1 structure and ordering properties of binary heaps npl of each node is 1 more than the minimum of the null difference is that heap is not ______________________ path lengths of its ________________ very unbalanced is desired leftist heap property npl of the left child is at least as large as that of the right child biases tree to left subtree 27 28 Leftist Heaps Leftist Heaps example: leftist heap leftist heaps operations all work should be done on the right path, which is guaranteed to be short and may destroy the leftist heap property not difficult to fix 29 30 Leftist Heaps Leftist Heaps leftist heaps operations (cont.) leftist heaps operations (cont.) merging merging example insertion special case of merging a 1-node heap with a larger heap if either of the two heaps is , return the other heap otherwise, compare __________ merge heap with the larger root with the right subheap of the heap with the smaller root 31 32 Leftist Heaps Leftist Heaps leftist heaps operations (cont.) leftist heaps operations (cont.) merging example merging example result is not leftist: left npl = 1, right npl = 2 33 34 Leftist Heaps Skew Heaps leftist heaps operations (cont.) self-adjusting version of leftist heap merging example relationship of skew heap to leftist heap is analogous to splay fix by swapping children trees and trees skew heaps binary trees with heap order but no structural constraint no information kept about null path length right path can be arbitrarily _________ worst case of all operations: for operations, total worst case: , or __________________ 35 36 Skew Heaps Skew Heaps skew heaps (cont.) skew heaps example fundamental operation is merging merge two skew heaps after merging, for leftists heaps, check both children for tree with larger root will merge onto tree with smaller root structure and swap children if needed in skew heaps, swap children except nodes on right paths do not swap children no extra required to maintain path lengths no tests required to determine when to swap children 37 38 Skew Heaps Skew Heaps skew heaps example (cont.) skew heaps example (cont.) recursively merge with the subheap of rooted at 8 make this heap the new left child of and the old left child of becomes the new right child heap happens to be ______________ 39 40 Binomial Queues Binomial Queues binomial queues binomial queues example keep a collection of heap-ordered trees, known as a ____________ at most one binomial tree of every ____________ heap order imposed on each binomial tree can represent any priority queue example: a priority queue of size 13 can be represented by the forest or 1101 worst case of all operations: 41 42 Binomial Queues Binomial Queues binomial queue of size 6 example binomial queue operations minimum element found by scanning of all trees found in can keep ongoing information to reduce to merging two queues merge takes 43 44 Binomial Queues Binomial Queues merge example merge example (cont.) now we have three binomial trees of height 3; keep one and merge the other two merge of two trees 45 46 Binomial Queues Binomial Queues merge values 1 7 example merge values 1 7 example (cont.) insert 1 insert 5 insert 2 insert 6 insert 3 insert 4 insert 7 47 48 Binomial Queues Binomial Queues deleteMin example deleteMin example (cont.) after merge separate tree with minimum root from rest of tree remaining trees after removing min 49 50.