lecture22-aug3 - 8/3/2009 Lecture 22 Assignment 4 Q&A...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
8/3/2009 1 Lecture 22 • Assignment 4 – Q&A • Topics of the day – Heaps – Heapsort –Sets – Bitsets – Conclusions about sorting algorithms Heap Example 70 tree 60 40 30 12 8 And use the Numbers as Indexes into A Vector [or ArrayList] to Store the Heap Nodes 70 0 12 tree [ 0 ] [ 1 ] [2 70 60 tree.nodes 60 1 40 3 30 4 2 8 5 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 12 40 30 8 Let i = index In a vector implementation children of i th node are at element[2*i+1] and element[2*i+2]; Parent of the i th node is at element [(i-1)/2] Heap Operations ± Insertion(value) : place the new value at the back of the heap and percolate it up the tree to its proper position ( aka push ) . ± Deletion() : exchange root value with the back of the heap and then percolate the new root down the tree which now has one less element aka pop tree,which now has one less element ( ) - Insert and delete running time: O(log 2 n) ± (re)Heapify: apply the percolate-down operation to the interior nodes, from the last interior node in the tree up to the root - running time: O(n) ( aka make ) ± Heapsort : heapifies a whole vector and deletes repeatedly from the heap, putting each deleted value into its final sorted position. O(n log 2 n) Heap Methods These generic methods in JCF assume that the heap is represented by a sequence (e.g. say v is a Vector, ArrayList, etc.) If start and end are indicies: 1. v.makeHeap (start, end, [compareMethodName] ) constructs a heap from a sequence 2. v.pushHeap (T value) - pushes an element onto the end of the heap and then rebuilds it 3. v.popHeap ( ) - exchanges the first and last elements and then rebuilds the rest of the heap (minus the last element). Returns the old first. 4. v.sortHeap ( ) - sorts a heap into descending order Insert (push) 50 into a Heap 63 38 8 25 10 40 30 v[0] v[1] v[2] v[4 [5 [3 [6 63 38 8 25 10 40 30 v[0] v[1] v[2] Vector <Integer> v = new Vector<Integer> (); 18 3 5 v[9] v[4] v[8] v[7] v[5] v[3] v[6] 18 3 5 v[9] v[4] v[8] v[7] v[5] v[3] v[6] (a) (b) 50 v[10] Before After Clearly 50 is in the wrong place and so we must percolate it up the tree Percolate-up compares the new element to its parent and swaps values if necessary - this continues until no swap is needed
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8/3/2009 2 Percolate Up the tree in pushHeap() 63 30 v[0] . . . 63 . . . . . . 18 25 v[9] v[4] 50 v[10] . . . Step 1 Compare 50 and 25 (Exchange v[10] and v[4]) 18 25 30 v[9] v[4] v[10] . . . 18 25 30 v[1] v[9] v[4] v[10] . . . Step 2 Compare 50 and 30 (Exchange v[4] and v[1]) Step 3 Compare 50 and 63 (50 in correct location) Delete (pop) from a Heap 63 40 30 v[0] v[1] v[2] 18 40 30 v[0] v[1] v[2] 18 3 5 38 8 25 10 v[9] v[4] v[8] v[7] v[5] v[3] v[6] Before a deletion After exchanging the root and last element in the heap 63 3 5 38 8 25 10 v[9] v[4] v[8] v[7] v[5] v[3] v[6] Percolate-down: compares the new element to its children and swaps values with the largest if necessary - this continues until no swap is needed Percolate Down for popHeap() ... 40 v[0] Step 1: Exchange 18 and 40 18 38 8 v[2] v[5] v[6] . . .
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

lecture22-aug3 - 8/3/2009 Lecture 22 Assignment 4 Q&amp;A...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online