-->
当前位置:首页 > 题库 > 正文内容

程序填空题:MinHeap Deletion

Luz4年前 (2021-05-10)题库626
Please fill in the blanks in the program which deletes a given element at position `p` from a min-heap `H`.

```c++
Deletion ( PriorityQueue H, int p ) /* delete the element H->Elements[p] */
{
ElementType temp;
int child;

temp = H-> Elements[ H->Size-- ];
if ( temp < H->Elements[p] ) {
while ( (p != 1) && (temp < H->Elements[p/2]) ) {
@@[H->Elements[p] = H->Elements[p/2]](3);
p /= 2;
}
}
else {
while( (child = 2*p) <= H->Size) {
if ( child != H->Size && @@[H->Elements[child+1] < H->Elements[child]](3) )
child ++;
if ( @@[temp > H->Elements[child]](3) ) {
H->Elements[p] = H->Elements[child];
p = child;
}
else
break;
}
}
H->Elements[p] = temp;
}
```





答案:
第1空:H->Elements[p] = H->Elements[p/2]

第2空:H->Elements[child+1] < H->Elements[child]

第3空:temp > H->Elements[child]

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。