Midterm 2 Solutions

Hereisanotherpossiblewaytoremoveakeyatpositionjmovethe

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ckHeap(A, currentSize, 2*j+1); } else return true; } 8) Recall that we presented an algorithm to remove a key at position j (which is also described in the text.) Here is another possible way to remove a key at position j: move the key at A[currentSize] to A[j] and call percolateDown (j). Will this work? If so, prove it. Otherwise, give an example for which it fails. 8 points Solution: Consider a heap of size 6 containing 1 10 2 12 15 3. Let j = 4. It is easy to see that in this case the above algorithm fails. 9) Write a procedure to delete the second smallest key from a min heap. Your procedure should perform only a constant number of additional operations besides calling INSERT or DELETEMIN. (It should return the second smallest key.) What is the complexity of your procedure? Express your answer using O notation in terms of N = the number of nodes in the heap. 8 points Solution: deleteSecondMin( ) { int temp1 = deleteMin(); int temp2 = deleteMin(); insert(temp1); return temp2; } Time complexity = 3 x O(log n) = O(log n)...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online