堆,是一颗完全二叉树,且任一结点大于等于其左右孩子结点。
代码如下,详见注释:
输入:
10
85 55 82 57 68 92 99 98 66 56
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn = 110;
int heap[maxn] = {
0};
int n;
void downAdjust(int low, int high){
int i = low, j = 2*i;
while(j<=high){
//结点i存在孩子
if(j+1<=high&&heap[j+1]>heap[j]){
//右孩子大于父亲结点i,且右孩子大于左孩子