HeapsortWhy study Heapsort?What is a “heap”?Balanced binary treesLeft-justified binary treesPlan of attackThe heap propertysiftUpConstructing a heap IConstructing a heap IIConstructing a heap IIIOther children are not affectedA sample heapRemoving the root (animated)The reHeap method IThe reHeap method IIThe reHeap method IIIThe reHeap method IVSortingMapping into an arrayRemoving and replacing the rootReheap and repeatAnalysis IAnalysis IIAnalysis IIIAnalysis IVThe EndHeapsort2Why study Heapsort?It is a well-known, traditional sorting algorithm you will be expected to knowHeapsort is always O(n log n)Quicksort is usually O(n log n) but in the worst case slows to O(n2)Quicksort is generally faster, but Heapsort is better in time-critical applicationsHeapsort is a really cool algorithm!3What is a “heap”?Definitions of heap:1. A large area of memory from which the programmer can allocate blocks as needed, and deallocate them (or allow them to be garbage collected) when no longer needed2. A balanced, left-justified binary tree in which no node has a value greater than the value in its parentThese two definitions have little in commonHeapsort uses the second definition4Balanced binary treesRecall:The depth of a node is its distance from the rootThe depth of a tree is the depth of the deepest nodeA binary tree of depth n is balanced if all the nodes at depths 0 through n-2 have two childrenBalanced Balanced Not balancedn-2n-1n5Left-justified binary treesA balanced binary tree of depth n is left-justified if:it has 2n nodes at depth n (the tree is “full”), orit has 2k nodes at depth k, for all k < n, and all the leaves at depth n are as far left as possibleLeft-justified Not left-justified6Plan of attackFirst, we will learn how to turn a binary tree into a heapNext, we will learn how to turn a binary tree back into a heap after it has been changed in a certain wayFinally (this is the cool part) we will see how to use these ideas to sort an array7The heap propertyA node has the heap property if the value in the node is as large as or larger than the values in its childrenAll leaf nodes automatically have the heap propertyA binary tree is a heap if all nodes in it have the heap property128 3Blue node has heap property128 12Blue node has heap property128 14Blue node does not have heap property8siftUpGiven a node that does not have the heap property, you can give it the heap property by exchanging its value with the value of the larger childThis is sometimes called sifting upNotice that the child may have lost the heap property148 12Blue node has heap property128 14Blue node does not have heap property9Constructing a heap IA tree consisting of a single node is automatically a heapWe construct a heap by adding nodes one at a time:Add the node just to the right of the rightmost node in the deepest levelIf the deepest level is full, start a new levelExamples:Add a new node hereAdd a new node here10Constructing a heap IIEach time we add a node, we may destroy the heap property of its parent nodeTo fix this, we sift upBut each time we sift up, the value of the topmost node in the sift may increase, and this may destroy the heap property of its parent nodeWe repeat the sifting up process, moving up in the tree, until eitherWe reach nodes whose values don’t need to be swapped (because the parent is still larger than both children), orWe reach the root11Constructing a heap III8 810108108 5108 5121012 581210 581 2 3412Other children are not affectedThe node containing 8 is not affected because its parent gets larger, not smallerThe node containing 5 is not affected because its parent gets larger, not smallerThe node containing 8 is still not affected because, although its parent got smaller, its parent is still greater than it was originally1210 58 141214 58 101412 58 1013A sample heapHere’s a sample binary tree after it has been heapifiedNotice that heapified does not mean sortedHeapifying does not change the shape of the binary tree; this binary tree is balanced and left-justified because it started out that way19141822321141191525172214Removing the root (animated)Notice that the largest number is now in the rootSuppose we discard the root:How can we fix the binary tree so it is once again balanced and left-justified?Solution: remove the rightmost leaf at the deepest level and use it for the new root19141822321141191517221115The reHeap method IOur tree is balanced and left-justified, but no longer a heapHowever, only the root lacks the heap propertyWe can siftUp() the rootAfter doing this, one and only one of its children may have lost the heap property191418223211491517221116The reHeap method IINow the left child of the root (still the number 11) lacks the heap propertyWe can siftUp() this nodeAfter doing this, one and only one of its children may have lost the heap property191418223211491517112217The reHeap method IIINow the right child of the left child of the root (still the number 11) lacks the heap property:We can siftUp() this nodeAfter doing this, one and only one of its children may have lost the heap property —but it doesn’t, because it’s a leaf191418113211491517222218The reHeap method IVOur tree is once again a heap, because every node in it has the heap propertyOnce again, the largest (or a largest) value is in the rootWe can repeat this process until the tree becomes emptyThis produces a sequence of values in order largest to smallest191418213111491517222219SortingWhat do heaps have to do with sorting an array?Here’s the neat part:Because the binary tree is balanced and left justified, it can be represented as an arrayDanger Will Robinson: This representation works well only with balanced, left-justified binary treesAll our operations on binary trees can be represented as operations on arraysTo sort: heapify the array; while the array isn’t empty { remove and replace the root; reheap the new root node;}20Mapping into an arrayNotice:The left child of index i is at index 2*i+1The right child of index i is at index 2*i+2Example: the children of node 3 (19) are 7 (18) and 8 (14)19141822321141191525172225 22 17 19 22 14 15 18 14 21 3 9 11 0 1 2 3 4 5 6 7 8 9 10 11 1221Removing and replacing the rootThe
View Full Document