了解了堆的一些基本概念之后,让我们来学习一下堆的一些 基本功能的实现方法
此处用 小根堆进行演示
用选择和努力去惊艳时光
1.头文件和堆结构体
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int HPDataType;
typedef struct Heap
{
HPDataType* a; //可动态变化的存储堆数据的数组
int size; //堆中的有效数据个数
int capacity; //堆的存储容量
}Heap;
2.函数声明
//堆的构建
void HeapCreate(HPDataType* a, int n);
//堆的初始化
void HeapInit(Heap* hp, HPDataType* a, int n);
//检查是否需要扩容
void CheckCapacity(Heap* hp);
//交换结点的值
void Swap(HPDataType* x, HPDataType* y);
//向上调整
void AdjustUp(HPDataType* a, int child);
//向下调整
void AdjustDown(HPDataType* a, int size, int parent);
//堆的打印
void HeapPrint(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 HeapDestory(Heap* hp);
3.堆的创建
创建堆可以用向上调整和向下调整两种方式
向上调整就是从第二个节点开始,依次向上进行建堆
向下调整的前提是左右子树都是堆,如果左右子树还不是堆,在向下调整时,只能保证每个父子结点是成堆的,无法保证整体是堆
向上建堆的时间复杂度为O(N*logN)
向下建堆的时间复杂度为O(N)
//堆的构建
void HeapCreate(HPDataType* a, int n)
{
assert(a);
向上调整
//for (int i = 1; i < n; i++)
//{
// AdjustUp(a, i);
//}
//向下调整(前提是左右子树都是堆,所以从下向上调整)
for (int i = (n - 1 - 1) / 2; i >= 0; i--)
{
AdjustDown(a, n, i);
}
}
4.堆的初始化
//堆的初始化
void HeapInit(Heap* hp, HPDataType* a, int n)
{
assert(hp && a);
hp->a = (HPDataType*)malloc(sizeof(HPDataType) * n);
if (hp->a == NULL)
{
perror("Malloc fail!\n");
}
hp->capacity = hp->size = n;
for (int i = 0; i < n; i++)
{
hp->a[i] = a[i];
}
}
5.检查是否需要扩容
//检查是否需要扩容
void CheckCapacity(Heap* hp)
{
assert(hp);
if (hp->size == hp->capacity)
{
hp->capacity += 4;
hp->a = (HPDataType*)realloc(hp->a, sizeof(HPDataType) * hp->capacity);
if (hp->a == NULL)
{
perror("Realloc fail!\n");
}
}
}
6.交换结点的值
//交换结点的值
void Swap(HPDataType* x, HPDataType* y)
{
HPDataType tmp = *x;
*x = *y;
*y = tmp;
}
7.向上调整
//向上调整
void AdjustUp(HPDataType* a, int child)
{
assert(a);
int parent = (child - 1) / 2;
while (child > 0)
{
if (a[child] < a[parent])
{
//交换父子结点的值
Swap(&a[child], &a[parent]);
//继续向上检查
child = parent;
parent = (child - 1) / 2;
}
else break;
}
}
8.向下调整
//向下调整
void AdjustDown(HPDataType* a, int size, int parent)
{
assert(a);
//先默认左孩子是小的
int child = parent * 2 + 1;
while (child < size)
{
if ((child + 1) < size && a[child + 1] < a[child])
child++;//较小的改为右孩子
//将较小的子节点与父节点交换
if (a[child] < a[parent])
{
Swap(&a[child], &a[parent]);
//继续向下检查
parent = child;
child = parent * 2 + 1;
}
else break;
}
}
9.堆的打印
//堆的打印
void HeapPrint(Heap* hp)
{
assert(hp);
for (int i = 0; i < hp->size; i++)
{
printf("%d ", hp->a[i]);
}
printf("\n");
}
10.堆的插入
//堆的插入
void HeapPush(Heap* hp, HPDataType x)
{
assert(hp);
CheckCapacity(hp);
hp->a[hp->size] = x;
hp->size++;
AdjustUp(hp->a, hp->size - 1);
}
11.堆的删除
删除堆是删除堆顶的数据,将堆顶的数据根最后一个数据交换,然后删除数组最后一个数据,再进行向下调整算法
//堆的删除
void HeapPop(Heap* hp)
{
assert(hp);
hp->a[0] = hp->a[hp->size - 1];
hp->size--;
AdjustDown(hp->a, hp->size, 0);
}
12.取堆顶的数据
//取堆顶的数据
HPDataType HeapTop(Heap* hp)
{
assert(hp);
return hp->a[0];
}
13.堆的数据个数
//堆的数据个数
int HeapSize(Heap* hp)
{
assert(hp);
return hp->size;
}
14.堆的判空
//堆的判空
int HeapEmpty(Heap* hp)
{
if (hp->size == 0) return 1;
else return 0;
}
15.堆的销毁
//堆的销毁
void HeapDestory(Heap* hp)
{
assert(hp);
free(hp->a);
hp->a = NULL;
hp->capacity = hp->size = 0;
}
好了,这就是树和堆的基本概念和性质,有问题的地方欢迎评论区留言,如果感觉有帮助,还请三连支持!!!