#include<stdio.h>
#include<stdlib.h>
#define MaxSize 9
#define T int
typedef struct minheap {
int Size;
T Elements[MaxSize];
}MinHeap;
void AdjustDown(T heap[], int r, int n);
void CreateHeap(MinHeap* hp);
void main() {
int i;
MinHeap* hp = (MinHeap*)malloc(sizeof(MinHeap));
hp->Size = 8;
//T array[MaxSize]= { 0, 61,28,81,43,36,47,83,5 };
hp->Elements[0] = 0;
hp->Elements[1] = 61;
hp->Elements[2] = 28;
hp->Elements[3] = 81;
hp->Elements[4] = 43;
hp->Elements[5] = 36;
hp->Elements[6] = 47;
hp->Elements[7] = 83;
hp->Elements[8] = 5;
CreateHeap(hp);
for (i = 1; i < MaxSize; i ++) {
printf("%d ", hp->Elements[i]);
}
//hp->Elements = array;
}
void CreateHeap(MinHeap* hp) {
int i, n = hp->Size;
for (i = n / 2; i > 0; i--) {
AdjustDown(hp->Elements, i, n);
}
}
void AdjustDown(T heap[], int r, int n) {
int child = 2 * r;
T temp = heap[r];
while(child<=n)
{
if (child<n && heap[child]>heap[child + 1]){
child++;
}
if (temp <= heap[child]) {
break;
}
heap[child / 2] = heap[child];
child *= 2;
}
heap[child / 2] = temp;
}
最小堆--树---完全二叉树---堆---最小堆
Guess you like
Origin blog.csdn.net/m0_47472749/article/details/121728665
Recommended
Ranking