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

程序填空题:筛选法调整堆

Luz4年前 (2021-05-10)题库1056
筛选法调整堆。

```c++
#include
#define MAXSIZE 1000
using namespace std;

typedef struct
{
int key;
char *otherinfo;
}ElemType;

typedef struct
{
ElemType *r;
int length;
}SqList;

void HeapAdjust(SqList &L,int s,int m)
{
ElemType rc;
int j;
rc=L.r[s];
for(j=2*s;j<=m;@@[j*=2](2))
{
if(@@[j if(@@[rc.key>=L.r[j].key](2)) break;
L.r[s]=L.r[j];
s=j;
}
L.r[s]=@@[rc](1);
}

void Create_Sq(SqList &L)
{
int i,n;
cin>>n; //输入的值不大于 MAXSIZE,确保L.r[2-n]已经是堆
for(i=1;i<=n;i++)
{
cin>>L.r[i].key;
L.length++;
}
}
void show(SqList L)
{
int i;
for(i=1;i<=L.length;i++)
if(i==1)
cout< else
cout<<" "<}

int main()
{
SqList L;
L.r=new ElemType[MAXSIZE+1];
L.length=0;
Create_Sq(L);
HeapAdjust(L,1,L.length);//L.r[2-L.length]已经是堆,将L.r[1..L.length]调整为以r[1]为根的大根堆
show(L);
return 0;
}
```
### 输入样例:
第一行输入一个数n(输入的值不大于 MAXSIZE)。
第二行依次输入n个数,并确保第2-n个数已经是堆

```in
9
34 78 53 45 65 09 31 17 23
```

### 输出样例:
输出以第1个数为根的大根堆。
```out
78 65 53 45 34 9 31 17 23
```





答案:
第1空:j*=2

第2空:j
第3空:rc.key>=L.r[j].key

第4空:rc

发表评论

访客

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