2
void ReheapDown(int* heap, int top, int swpIndx, int numElements) {
int leftChild = 2 * top + 1;
int rightChild = 2 * top + 2;
int minChild;
if (leftChild < numElements) {
// find subscript of smallest child
if (rightChild >= swpIndx || heap[leftChild] < heap[rightChild])
minChild = leftChild;
else
minChild = rightChild;
// if data at top is greater than smallest
// child then swap and continue
if (heap[top] > heap[minChild]) {
swap(heap[top], heap[minChild]);
ReheapDown(heap, minChild, swpIndx, numElements);
}
}
這是一個簡單的堆。 ReheapDown部分用於刪除堆中的項目。 swpIndx
做什麼? (我需要知道做一個家庭作業,我應該寫這個函數來刪除堆中的某個鍵。)簡單的堆程序 - 這個變量做什麼
它只在函數中使用過一次,試圖弄清楚'if'在哪裏使用... –