【C语言】堆的实现

堆的基本概念

堆在逻辑上是完全二叉树,那什么又是完全二叉树呢?

完全二叉树简单来说就是前n-1层每个节点都有两个儿子,最后一层叶子紧挨着排列。

堆在物理结构上适合用数组存储。

让我们先来学习树->二叉树的基本知识(可看文章:http://t.csdnimg.cn/8GUQH),来更好学习堆

堆分为大(根)堆和小(根)堆

大堆:任何一个父亲节点都大于他的两个儿子

小堆:任何一个父亲节点都小于他的两个儿子

补充(必须要掌握)

在数组中,孩子找父亲,父亲找孩子数组下标的表示

左孩子找父亲:parent(的下标)=chlid(的下标)*2+1;

右孩子找父亲:parent(的下标)=chlid(的下标)*2+2;

孩子找父亲:child=(parent-1)/2;

堆的定义

typedef int HPDataType;
typedef struct Heap
{
	HPDataType* a;//数组
	int size;
	int capacity;
}HP;

在堆中有两个很重要的算法:向上调整算法和向下调整算法

向上调整算法

假设我们要向一个小堆中插入一个数据,如果插入的数据比他的父亲还小,那我们就需要交换他们的位置,交换了之后,该数据成为替代原本的父亲,再比较他与他现在的父亲的关系,如果还是比他父亲小则,二者交换......直到插入的数据大于他的父亲。我们要实现一个向上调整算法

void Adjustup(HPDataType*a,int child)
{
	int parent = (child - 1) / 2;
	while (child>0)
	{
		
		if (a[child] < a[parent])
		{
			Swap(&a[child], &a[parent]);
			child = parent;
			parent = (parent - 1) / 2;//找原本父亲的父亲下标
		}
		else
		{
			break;
		}
	}
}

为了更好的实现复用我们此处第一个参数传的是一个数组指针,而不是堆指针,插入是在数组最后一个数据的后面进行插入,所以我们要从下往上调整。

向下调整算法

该算法一般应用于,删除树顶数据。(以小堆为例)

第一步是要将树顶数据,与树末端数据进行交换,此时树末端数据成为新的树顶,size--删除原来的树顶

第二步再调用向下调整算法,此时树顶数据需要与他的儿子们比较,若是大于儿子,则要向下调整,调整到符号小堆结构为止

void AdjustDown(HPDataType*a, int n,int parent)
{
	int child = parent * 2 + 1;
	while (child<n)
	{
		if (child + 1 < n && a[child + 1] < a[child])
		{
			child++;
		}
		if (a[child] < a[parent])
		{
			Swap(&a[child], &a[parent]);
			parent = child;
			child = child * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

初始化

方式一

void HPInit(HP* hp)
{
	assert(hp);
	hp->a = NULL;
	hp->size = hp->capacity = 0;
}

方式二

void HPArrayInit(HP* hp, HPDataType* a, int n)
{
	assert(hp);
	hp->a = (HPDataType*)malloc(sizeof(HPDataType) * n);
	if (hp->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	memcpy(hp->a, a, n * sizeof(HPDataType));
	hp->size = hp->capacity = n;
	//向上调整,建堆时间复杂度O(N*logN)
	//for (int i = 1; i < hp->size; i++)
	//{
	//	Adjustup(hp->a, i);
	//}
	//向下调整,建堆时间复杂度O(N)
	for (int i = (hp->size - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(hp->a, hp->size, i);
	}
}

向上调整建堆与向下调整建堆时间复杂度的分析

向上

向下

销毁

void HPDestroy(HP* hp)
{
	assert(hp);
	hp->size = hp->capacity = 0;
	free(hp->a);
	hp->a = NULL;
}

堆的插入

//交换函数
void Swap(HPDataType* px, HPDataType* py)
{
	HPDataType tmp;
	tmp = *px;
	*px = *py;
	*py = tmp;
}

// 堆的插入
void HPPush(HP* hp, HPDataType x)
{
	assert(hp);
	//扩容
	if (hp->size == hp->capacity)
	{
		int newcapacity = hp->capacity == 0 ? 4 : 2 * hp->capacity;
		HPDataType* tmp = (HPDataType*)realloc(hp->a, sizeof(HPDataType)*newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		hp->a = tmp;
		hp->capacity = newcapacity;
	}
	hp->a[hp->size++] = x;
	Adjustup(hp->a, hp->size - 1);
}

堆的删除

// 堆的删除
void HPPop(HP* hp)
{
	assert(hp);
	assert(hp->size > 0);
	Swap(&hp->a[0], &hp->a[hp->size - 1]);
	
	hp->size--;
	AdjustDown(hp->a, hp->size, 0);
}

取堆顶的数据

HPDataType HPTop(HP* hp)
{
	assert(hp);
	return hp->a[0];
}

判空

int HPEmpty(HP* hp)
{
	assert(hp);
	return hp->size == 0;
}

堆的个数

int HPSize(HP* hp)
{
	assert(hp);
	return hp->size;
}

总代码

头文件

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
#include<string.h>


typedef int HPDataType;
typedef struct Heap
{
	HPDataType* a;
	int size;
	int capacity;
}HP;
//初始化
void HPInit(HP* hp);
void HPArrayInit(HP* hp, HPDataType* a, int n);
//销毁
void HPDestroy(HP* hp);
// 堆的插入
void HPPush(HP* hp, HPDataType x);
// 堆的删除
void HPPop(HP* hp);
// 取堆顶的数据
HPDataType HPTop(HP* hp);
// 堆的数据个数
int HPSize(HP* hp);
// 堆的判空
int HPEmpty(HP* hp);
//向上调整算法
void Adjustup(HPDataType* a, int child);
//向下调整算法
void AdjustDown(HPDataType* a, int n, int parent);


函数实现

#include"Heap.h"
//初始化
void HPInit(HP* hp)
{
	assert(hp);
	hp->a = NULL;
	hp->size = hp->capacity = 0;
}
void HPArrayInit(HP* hp, HPDataType* a, int n)
{
	assert(hp);
	hp->a = (HPDataType*)malloc(sizeof(HPDataType) * n);
	if (hp->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	memcpy(hp->a, a, n * sizeof(HPDataType));
	hp->size = hp->capacity = n;
	//向上调整,建堆时间复杂度O(N*logN)
	//for (int i = 1; i < hp->size; i++)
	//{
	//	Adjustup(hp->a, i);
	//}
	//向下调整,建堆时间复杂度O(N)
	for (int i = (hp->size - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(hp->a, hp->size, i);
	}
}
//销毁
void HPDestroy(HP* hp)
{
	assert(hp);
	hp->size = hp->capacity = 0;
	free(hp->a);
	hp->a = NULL;
}
//交换函数
void Swap(HPDataType* px, HPDataType* py)
{
	HPDataType tmp;
	tmp = *px;
	*px = *py;
	*py = tmp;
}
//向上调整算法
void Adjustup(HPDataType*a,int child)
{
	int parent = (child - 1) / 2;
	while (child>0)
	{
		
		if (a[child] < a[parent])
		{
			Swap(&a[child], &a[parent]);
			child = parent;
			parent = (parent - 1) / 2;//找原本父亲的父亲下标
		}
		else
		{
			break;
		}
	}
}
// 堆的插入
void HPPush(HP* hp, HPDataType x)
{
	assert(hp);
	//扩容
	if (hp->size == hp->capacity)
	{
		int newcapacity = hp->capacity == 0 ? 4 : 2 * hp->capacity;
		HPDataType* tmp = (HPDataType*)realloc(hp->a, sizeof(HPDataType)*newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		hp->a = tmp;
		hp->capacity = newcapacity;
	}
	hp->a[hp->size++] = x;
	Adjustup(hp->a, hp->size - 1);
}
//向下调整算法
void AdjustDown(HPDataType*a, int n,int parent)
{
	int child = parent * 2 + 1;
	while (child<n)
	{
		if (child + 1 < n && a[child + 1] < a[child])
		{
			child++;
		}
		if (a[child] < a[parent])
		{
			Swap(&a[child], &a[parent]);
			parent = child;
			child = child * 2 + 1;
		}
		else
		{
			break;
		}
	}
}
// 堆的删除
void HPPop(HP* hp)
{
	assert(hp);
	assert(hp->size > 0);
	Swap(&hp->a[0], &hp->a[hp->size - 1]);
	
	hp->size--;
	AdjustDown(hp->a, hp->size, 0);
}
// 取堆顶的数据
HPDataType HPTop(HP* hp)
{
	assert(hp);
	return hp->a[0];
}
// 堆的数据个数
int HPSize(HP* hp)
{
	assert(hp);
	return hp->size;
}
// 堆的判空
int HPEmpty(HP* hp)
{
	assert(hp);
	return hp->size == 0;
}

测试

#include"Heap.h"
int main()
{
	int a[] = { 60,70,65,50,32,100 };

	HP hp;
	//HPInit(&hp);
	HPArrayInit(&hp, a, 6);
	/*for (int i = 0; i < sizeof(a) / sizeof(int); i++)
	{
		HPPush(&hp, a[i]);
	}*/

	//printf("%d\n", HPTop(&hp));
	//HPPop(&hp);
	//printf("%d\n", HPTop(&hp));
	while (!HPEmpty(&hp))
	{
		printf("%d\n", HPTop(&hp));
		HPPop(&hp);
	}

	HPDestroy(&hp);

	return 0;

}

欢迎各位大佬一起学习交流~

  • 56
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 25
    评论
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值