Midterm 2 Solutions

10 points example let a be the array drawn as a

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: r of keys stored in array A and another integer j, and outputs true (false) if the keys the subtree rooted at node j forms a min-heap. 10 points Example: Let A be the array drawn as a complete tree shown below (with currentSize = 10). If testHeap is called with j = 2, the output should be true, with j = 1, the output should be false. Hint: recursive solution is easier than an iterative one. Solution: checkHeap(int A, int currentSize, int j) { // currentSize is the size of the heap if (2*j > currentSize) return true; if (A[j] > A[2*j]) return false; if (!checkHeap(A, currentSize, 2*j)) return false; if (currentSize <= 2*j + 1) { if (A[j] > A[2*j+1]) return false; else return che...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online