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

程序填空题:大顶堆的指定删除

Luz4年前 (2021-05-10)题库1507
下列代码的功能是从大顶堆`H`中删除指定位置`p`上的元素,然后继续调整为大顶堆。

```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]

发表评论

访客

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