堆分为大堆和小堆,在这里实现的为大堆
形如下图的即为大堆(所有的父节点大于其两个子节点),反之则为小堆
//头文件
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
typedef int HPDataType;
typedef struct Heap {
HPDataType* _a;
int _size; //堆的元素个数
int _capacity; //堆的大小
}Heap;
//初始化
void HeapInit(Heap* hp, HPDataType* a, int n);
//销毁
void HeapDestory(Heap* hp);
//入堆
void HeapPush(Heap* hp, HPDataType x);
//出堆
void HeapPop(Heap* hp);
//返回堆顶元素
HPDataType HeapTop(Heap* hp);
//返回堆的元素个数
int HeapSize(Heap* hp);
//判断堆是否为空
int HeapEmpty(Heap* hp);
//向下调整
void ShiftDown(int* a, int n, int root);
//向上调整
void ShiftUp(int* a, int n, int child);
// 堆排序
void HeapSort(int* a, int n);
//测试函数
void TestHeap();
//输出函数
void Heap_print(Heap* hp);
//c文件
#include"堆.h"
void HeapInit(Heap* hp, HPDataType* a, int n) {
assert(hp);
hp->_a= (int*)malloc(n*sizeof(int));
int i = 0;
for (i = 0; i < n; i++) {
hp->_a[i] = a[i];
}
for (i = (n - 2) / 2; i >= 0; i--) {
ShiftDown(hp->_a, n, i);
}
hp->_size = n;
hp->_capacity = n;
}
void HeapDestory(Heap* hp) {
assert(hp);
free(hp->_a);
hp->_capacity = 0;
hp->_size = 0;
}
//入堆,入堆尾之后做向上调整恢复堆的结构
void HeapPush(Heap* hp, HPDataType x) {
assert(hp);
if (hp->_capacity == hp->_size) {
int newc = hp->_capacity == 0 ? 10 : 2 * hp->_capacity;
hp->_a = (int*)realloc(hp->_a, newc * sizeof(int));
}
hp->_a[hp->_size] = x;
hp->_size++;
ShiftUp(hp->_a, hp->_size, hp->_size - 1);
}
//出堆,先将堆顶元素和堆尾元素交换位置,然后出堆尾元素,
//之后做向下调整恢复堆的结构
void HeapPop(Heap* hp) {
assert(hp);
if (hp == NULL || hp->_size == 0) {
return;
}
if (hp->_size == 1) {
hp->_size--;
return;
}
int tmp = hp->_a[0];
hp->_a[0] = hp->_a[hp->_size - 1];
hp->_a[hp->_size - 1] = tmp;
hp->_size--;
for (int i = (hp->_size - 2) / 2; i >= 0; i--) {
ShiftDown(hp->_a, hp->_size, i);
}
}
HPDataType HeapTop(Heap* hp) {
assert(hp);
if (hp->_size == 0) {
printf("堆为空");
return NULL;
}
return hp->_a[0];
}
int HeapSize(Heap* hp) {
assert(hp);
return hp->_size;
}
void ShiftDown(int* a, int n, int root) {
int parent =root;
int child = 2 * parent + 1;
while (child < n) {
//比较两孩子结点的大小,取到较大的孩子
if (child + 1 < n && a[child] < a[child + 1]) {
child++;
}
//再比较较大的孩子和父节点的大小
//若大于父节点,交换,并且更新父子结点
if (a[child] >a[parent]) {
int tmp = a[parent];
a[parent] = a[child];
a[child] = tmp;
parent = child;
child = 2 * parent + 1;
}else {
break;
}
}
}
void ShiftUp(int* a, int n, int child) {
assert(a);
int parent = (child - 1) / 2;
while (child > 0){
if (a[child] > a[parent]){
int tmp = a[parent];
a[parent] = a[child];
a[child] = tmp;
child = parent;
parent = (child - 1) / 2;
}else{
break;
}
}
}
int HeapEmpty(Heap* hp) {
assert(hp);
if (hp->_size == 0) {
return 1;
}
return 0;
}
// 堆排序
//1向下调整
//2最大元素放到最后
//3向下调整
//4更新end
void HeapSort(int* a, int n){
for (int i = (n - 2) / 2; i >= 0; i--) {
ShiftDown(a, n, i);
}
int end = n - 1;
while (end > 0) {
int tmp = a[0];
a[0] = a[end];
a[end] = tmp;
ShiftDown(a, end,0);
end--;
}
}
void Heap_print(Heap* hp) {
assert(hp);
if (hp->_size == 0) {
return;
}
for (int i = 0; i < hp->_size; i++) {
printf("%d ", hp->_a[i]);
}
printf("\n");
}
void TestHeap() {
Heap hp;
int a[] = { 2,5,6,1,9,13,7 };
HeapInit(&hp, a, sizeof(a) / sizeof(int));
Heap_print(&hp);
HeapPush(&hp, 18);
HeapPush(&hp, 12);
HeapPush(&hp, 0);
Heap_print(&hp);
HeapPop(&hp);
HeapPop(&hp);
Heap_print(&hp);
HeapSort((&hp)->_a, (&hp)->_size);
Heap_print(&hp);
}
int main() {
TestHeap();
return 0;
}