头文件
Heap.h
#pragma once
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include<string.h>
typedef int Datatype;
typedef int(*Compare)(Datatype left, Datatype right); //函数指针
typedef struct Heap
{
Datatype *_hp;
int _capacity;
int _size;
Compare _com;
}Heap;
void swap(Datatype *left, Datatype *right); //交换函数
void AdjustDown(Heap *pHp, int parrent); //向下调整
void AdjustUp(Heap* pHp, int child); //向上调整
void Create(Heap *pHp, Datatype array[], int size, Compare com); //创建堆
void InitHeap(Heap* pHp, Compare com); //初始化堆
void Insert(Heap* pHp, Datatype data); //插入元素
int EmptyHeap(Heap *php); //判断是否为空堆
void Remove(Heap* pHp); //删除堆中元素
void CheckCapacity(Heap *php); //增容
int SizeHeap(Heap* pHp); //堆大小
Datatype HeapTop(Heap *php); //堆顶元素
int Less(Datatype left, Datatype right);
int Greater(Datatype left, Datatype right);
源文件
Heap.c
#include "Heap.h"
void swap(Datatype *left, Datatype *right)
{
int tmp = *left;
*left = *right;
*right = tmp;
}
void InitHeap(Heap* pHp, Compare com)
{
pHp->_hp = (Datatype*)malloc(sizeof(Datatype) * 3);
if (NULL == pHp)
return;
pHp->_capacity = 3;
pHp->_size = 0;
pHp->_com = com;
}
void AdjustDown(Heap *pHp, int parrent)//小堆
{
if (NULL == pHp)
{
return;
}
int child = parrent * 2 + 1;
while (child < pHp->_size)
{
if ((child + 1 < pHp->_size) && (pHp->_hp[child] > pHp->_hp[child + 1]))
{
child = child + 1;
}
if (pHp->_hp[child] < pHp->_hp[parrent])
{
swap(&pHp->_hp[child], &pHp->_hp[parrent]);
parrent = child;
child = parrent * 2 + 1;
}
else
return;
}
}
void AdjustUp(Heap* pHp, int child)//大堆
{
if (NULL == pHp)
{
return;
}
int parrent = (child - 1) >> 2;
if (pHp->_hp[child] > pHp->_hp[parrent])
{
swap(&pHp->_hp[child], &pHp->_hp[parrent]);
child = parrent;
parrent = (child - 1) >> 1;
}
else
return;
}
void Create(Heap *pHp, Datatype array[], int size, Compare com)
{
int i = 0;
int root = 0;
pHp->_hp = (Datatype*)malloc(sizeof(Datatype)*(size + 3));
assert(pHp);
pHp->_capacity = size + 3;
for (i = 0; i < size; ++i)
{
pHp->_hp[i] = array[i];
pHp->_size++;
}
pHp->_size = size;
pHp->_com = com;
root = (size - 2) >> 1;
for (; root >= 0; root--)
{
AdjustDown(pHp, root);
}
}
void CheckCapacity(Heap *php)//lll
{
if (php->_size >= php->_capacity)
{
int pnewcapacity = php->_capacity * 2;
Datatype *pnew = (Datatype *)malloc(sizeof((Datatype *)pnewcapacity));
assert(pnew);
memcpy(pnew, php->_hp, php->_size * sizeof(Datatype));
free(php->_hp);
php->_hp = pnew;
php->_capacity = pnewcapacity;
}
}
void Insert(Heap* pHp, Datatype data)
{
if (NULL == pHp)
{
return;
}
CheckCapacity(pHp);
pHp->_hp[++pHp->_size] = data;
if (pHp->_size > 1)
{
AdjustUp(pHp, pHp->_size - 1);
}
}
int EmptyHeap(Heap *php)
{
assert(php);
return php->_size == 0;
}
void Remove(Heap* pHp)
{
if (NULL == pHp)
return;
if (EmptyHeap(pHp))
return;
swap(&pHp->_hp[0], &pHp->_hp[pHp->_size - 1]);
pHp->_size--;
AdjustDown(pHp, 0);
}
int SizeHeap(Heap* pHp)
{
return pHp->_size;
}
Datatype HeapTop(Heap *php)
{
assert(php);
if (EmptyHeap(&php))
{
return -1;
}
return php->_hp[0];
}
int Less(Datatype left, Datatype right)
{
return left < right;
}
int Greater(Datatype left, Datatype right)
{
return left > right;
}