中国大学MOOC-陈越、何钦铭-数据结构-2018秋 05-树7 堆中的路径 (25 分)
程序员文章站
2022-07-15 12:22:37
...
#include <bits/stdc++.h>
#define MAX 1001
#define MINH -10001
int Heap[MAX], size;
void Creat();
void Insert(int value);
using namespace std;
int main()
{
int N, M, tmp, j;
cin >> N >> M;
Creat();
for(int i = 0; i < N; i++){
cin >> tmp;
Insert(tmp);
}
for(int i = 0; i < M; i++){
cin >> j;
cout << Heap[j];
while(j > 1){
j /= 2;
cout << " " << Heap[j];
}
cout << endl;
}
return 0;
}
void Creat(){
Heap[0] = MINH;
size = 0;
}
void Insert(int value)
{
int i;
for(i = ++size; Heap[i/2] > value; i/= 2){
Heap[i] = Heap[i/2];
}
Heap[i] = value;
}