この Priority Queue の実装では、再帰を使用して、メソッドのパラメーターとして受け取る配列がIsMaxHeap
最大ヒープかどうかを判断する必要があります。
これが機能するか、または問題なく機能する可能性があるすべてのケースを確実に評価したいと思います。
static boolean isMaxHeap(int[] H, int idx) {
if(2*idx == (H.length -1) && H[2 * idx] <= H[idx])
return true;
if(2*idx > (H.length -1))
return true;
if ((2*idx+1) == (H.length -1) && H[2 * idx] <= H[idx] && H[2 * idx + 1] <= H[idx])
return true;
if((2*idx+1) > (H.length -1))
return true;
if (H[2 * idx] <= H[idx] && H[2 * idx + 1] <= H[idx])
return isMaxHeap(H, (idx + 1));
else
return false;
}
私たちを手伝ってくれますか?