程序填空题:Build MinHeap
The function is to turn an array `A[]` of `N` elements into a min-heap.
```c++
#define leftchild(i) ( 2*(i)+1 )
void BuildMinHeap( ElementType A[], int N )
{ int i, j, child;
ElementType Tmp;
for ( i = (N-1)/2; i >= 0; i-- ) {
j = i;
for ( Tmp = A[j]; leftchild(j) < N; j = child ) {
child = leftchild(j);
if (@@[child!=N-1 && A[child+1] child ++;
if (@@[Tmp > A[child]](4)) A[j] = A[child];
else break;
}
@@[A[j] = Tmp](4);
}
}
```
**Thanks to DOU Yan from Yanshan University for the correction!**
答案:
第1空:child!=N-1 && A[child+1]
第2空:Tmp > A[child]
第3空:A[j] = Tmp
```c++
#define leftchild(i) ( 2*(i)+1 )
void BuildMinHeap( ElementType A[], int N )
{ int i, j, child;
ElementType Tmp;
for ( i = (N-1)/2; i >= 0; i-- ) {
j = i;
for ( Tmp = A[j]; leftchild(j) < N; j = child ) {
child = leftchild(j);
if (@@[child!=N-1 && A[child+1] child ++;
if (@@[Tmp > A[child]](4)) A[j] = A[child];
else break;
}
@@[A[j] = Tmp](4);
}
}
```
**Thanks to DOU Yan from Yanshan University for the correction!**
答案:
第1空:child!=N-1 && A[child+1]
第2空:Tmp > A[child]
第3空:A[j] = Tmp