#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//假设小堆
typedef int HDataType;
typedef struct heap{
HDataType* _data;
int _size;
int _capapcity;
} heap;
void heapInit(heap* hp){
if (hp == NULL)
return;
//空堆
hp->_data = NULL;
hp->_size = hp->_capapcity = 0;
}
//向下调整
void shiftDown(int* arr, int n, int cur){
//找到孩子的位置
//左孩子
int child = 2 * cur + 1;
while (child < n){
//从左孩子中找最小的
if (child + 1 < n&&arr[child + 1] < arr[child])
++child;
//和当前数据比较
//1.调整:孩子<当前
if (arr[child] < arr[cur]){
int tmp = arr[child];
arr[child] = arr[cur];
arr[cur] = tmp;
//更新位置,继续调整
cur = child;
child = 2 * cur + 1;
}
else
//2.不调整:孩子>=当前
break;
}
}
//插入元素
void shiftUp(int* arr, int n, int cur){
//和父节点比较
int parent = (cur - 1) / 2;
while (cur>0){
if (arr[cur] < arr[parent]){
int tmp = arr[cur];
arr[cur] = arr[parent];
arr[parent] = tmp;
//更新位置
cur = parent;
parent = (cur - 1) / 2;
}
else
break;
}
}
void Swap(int* a, int* b){
int tmp =*a;
*a = *b;
*b = tmp;
}
void checkCapacity(heap* hp){
if (hp->_size = hp->_capapcity){
int newC = hp->_capapcity == 0 ? 1 : 2 * _capapcity;
hp->_data = (HDataType*)realloc(hp->_data, sizeof(HDataType)* newC);
hp->_capapcity = newC;
}
}
void heapPush(heap* hp, HDataType val){
if (hp == NULL)
return;
checkCapacity(hp);
//尾插
hp->_data[hp->_size++] = val;
//向上调整
shiftUp(hp->_data, hp->_size, hp->_size - 1);
}
void heapPop(heap* hp){
if (hp->_size > 0){
//交换
Swap(&hp->_data[0], &hp->_data[hp->_size - 1]);
//尾删
hp->_size--;
//从堆顶位置向下调整
shiftDown(hp->_data, hp->_size, 0);
}
}
//void test(){
// int arr[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
// int n = sizeof(arr) / sizeof(arr[0]);
// //建堆:从最后的一个非叶子节点开始:向下调整
// for (int i = (n - 2) / 2; i >= 0; --i){
// shiftDown(arr, n, i);
// }
//}
void test(){
int arr[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
heap hp;
heapInit(&hp);
for (int i = 0; i < n; ++i){
heapPush(&hp, arr[i]);
}
for (int i = 0; i < n; ++i){
heapPop(&hp);
}
}
int main(){
test();
return 0;
}