Previous sortinggg algorithms Insertion Sort 2 O(n ) time Heapsort Merge Sort Based off slides by: David Matuszek http://www.cis.upenn.edu/~matuszek/cit594-2008/ O(n) space PtdbMttBPresented by: Matt Boggus 2 Heap data structure Balanced binary trees Binary tree Recall: The dthfdepth of a no de iitditis its distance f rom th e root The depth of a tree is the depth of the deepest node Balanced A binary tree of depth n is balanced if all the nodes at depths 0 through n-2 have two children Left-justified n-2 (Max) Heap property: no node has a value greater n-1 than the value in its parent n Balanced Balanced Not balanced 3 4 Left-jjyustified binary trees Buildinggp up to heap sort How to build a heap A balanced binary tree of depth n is left- justified if: n it has 2 nodes at depth n (the tree is “full”)or ), or k How to maintain a heap it has 2 nodes at depth k, for all k < n, and all the leaves at dept h n are as fa r le ft as poss i ble How to use a heap to sort data Left-justified Not left-justified 5 6 The heappp prop erty siftUp A node has the heap property if the value in the Given a node that does not have the heap property, you can nodide is as large as or larger t han th e va lues iiin its give it the heap property by exchanging its value with the children value of the larger child 12 12 12 12 14 8 3 8 12 8 14 8 14 8 12 Blue node has Blue node has Blue node does not Blue node does not Blue node has heap property heap property have heap property have heap property heap property All leaf nodes automatically have the heap property This is sometimes called sifting up AbiA binary tree is a heap if all nodiiththdes in it have the heap property 7 8 Constructinggp a heap I Constructinggp a heap II A tree consisting of a single node is automatically Each time we add a node, we may destroy the heap a heap property of its parent node To fix this, we sift up We construct a heap by adding nodes one at a time: But each time we sift up , the value of the topmost node Add the node just to the right of the rightmost node in in the sift may increase, and this may destroy the heap the deepest level property of its parent node If the deepest level is full , start a new level We repeat the sifting up process, moving up in the tree, Examples: until either Add a new Add a new Whdhld’tdtbdWe reach nodes whose values don’t need to be swapped node here node here (because the parent is still larger than both children), or We reach the root 9 10 Constructinggp a heap III Other children are not affected 8 8 10 10 12 12 14 10 8 8 5 10 5 14 5 12 5 123 8 14 8 10 8 10 10 10 12 The node containing 8 is not affected because its parent gets larger, not 8 5 12 5 10 5 smaller The node containing 5 is not affected because its parent gets larger, not smaller 12 8 8 4 The node containing 8 is still not affected because, although its parent got smaller, its parent is still greater than it was originally 11 12 A samppple heap Removingg() the root (animated) Here’s a sample binary tree after it has been heapified Notice that the largest number is now in the root Suppose we discard the root: 25 11 22 17 22 17 19 22 14 15 19 22 14 15 18 14 21 3 9 11 18 14 21 3 9 11 Notice that heapified does not mean sorted How can we fix the binary tree so it is once again balanced Heapifying does not change the shape of the binary tree; and left-justified? this binary tree is balanced and left-justified because it Solution: remove the rightmost leaf at the deepest level and started out that way use it for the new root 13 14 The reHeap method I The reHeap method II Our tree is balanced and left-justified, but no longer a heap Now the left child of the root (still the number 11) lacks However, only the root lacks the heap property the heap property 11 22 22 17 11 17 19 22 14 15 19 22 14 15 18 14 21 3 9 18 14 21 3 9 We can siftUp() the root We can siftUp() this node After doing this , one and only one of its children may have After doing this , one and only one of its children may have lost the heap property lost the heap property 15 16 The reHeap method III The reHeap method IV Now the right child of the left child of the root (still the Our tree is once again a heap, because every node in it has number 11) lacks the heap property: the heap property 22 22 22 17 22 17 19 11 14 15 19 21 14 15 18 14 21 3 9 18 14 11 3 9 We can siftUp() this node Once again, the largest (or a largest) value is in the root After doing this , one and only one of its children may have We can repeat this process until the tree becomes empty lost the heap property —but it doesn’t, because it’s a leaf This produces a sequence of values in order largest to smallest 17 18 Sorting Keyyp prop erties What do heaps have to do with sorting an array? H’hHere’s the neat part: Determining location of root and “last node” take Because the binary tree is balanced and left justified, it can be constant time reppyresented as an array Danger Will Robinson: This representation works well only with balanced, left-justified binary trees All our operations on binary trees can be represented as operations on arrays Remove n elements, re-heap each time To sort: heapify the array; while the array isn’t empty { remove and replace the root; reheap the new root node; } 19 20 Analysis Analysis To reheap the root node, we have to follow one path fhfrom the root to a lfd(dihbfleaf node (and we might stop before Construct the heap O(n log n) we reach a leaf) The bi nary tree i s perf ectl y b al anced Therefore, this path is O(log n) long Remove and re-heap O(n log n) AdAnd we on ly do O(1) operations at each nod e Therefore, reheaping takes O(log n) times Since we reheap inside a while loop that we do n times, Total time O(n log n) + O(n log n) the total time for the while loop is n*O(log n), or O(n log n) 21 22 The End PriorityyQ Queue Continue to priority queues? Queue – only access element in front Queue elements sorted by order of importance Imppplement as a heap where nodes store p pyriority values 23 24 Extract Max Increase Key Remove root Change node value Swap with last node Re-heapify Re-heappyify 25 26 Insert Add new node, priority is minimum possible value The End Increase priority 27.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages7 Page
-
File Size-