【数据结构 C语言版】第七篇 堆_typedef int hpdatatype; typedef struct heap { int

4.堆的插入

【思路】:

堆尾插入x,然后向上调整。

【代码】:

void HeapPush(Heap\* hp, HPDataType x)
{
	assert(hp);
    //如果空间不够就扩容
	if (hp->capacity == hp->size)
	{
		int newCapaciy = hp->capacity == 0 ? 4 : hp->capacity \* 2;
		HPDataType\* tmp = (HPDataType\*)realloc(hp->a, sizeof(HPDataType) \* newCapaciy);
		if (tmp == NULL)
		{
			perror("malloc fail");
			exit(-1);
		}
		hp->a = tmp;
		hp->capacity = newCapaciy;
	}
    
    //这边才是插入代码
	hp->a[hp->size++] = x;
	AdjustUp(hp->a, hp->size - 1);
}

5.堆顶的删除

【思路】:

将堆顶与最后一个元素交换。随后换到堆顶的元素向下调整,size要-1。

【代码】:

void HeapPop(Heap\* hp)
{
	assert(hp);
	assert(!HeapEmpty(hp));
	swap(&hp->a[0], &hp->a[hp->size - 1]);
	hp->size--;
	AdjustDown( hp->a,hp->size, 0);
}

6.向上调整

【思路】:

因为本文建的是小堆,所以如果孩子小于双亲,那么就交换(小堆:parent>child)。

如果parent>child一直存在,那么就一直执行下去,直至child > 0;

起始位置:child的位置。

最后:走到a[child]>a[parent]的时候,或者child<=0

image-20221023202659341

image-20221023203906377

【代码】:

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 = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}

7.向下调整

【思路】:

如果Child<parent就互换。

起始位置:parent

终止位置:走到a[child]>a[parent]的时候,或者Child>=n

image-20221023204202170

image-20221023204328169

【代码】:

//向下调整
void AdjustDown(HPDataType\* a, int n, int parent)
{
    //可以默认最小的孩子(左右孩子中)是左孩子
    //如果右孩子更小,就更新minChild。
	int minChild = parent \* 2 + 1;
	while (minChild < n)
	{
		if (minChild + 1 < n && a[minChild + 1] < a[minChild])
		{
			minChild++;
		}

		if (a[minChild] < a[parent])
		{
			swap(&a[minChild], &a[parent]);
			parent = minChild;
			minChild = parent \* 2 + 1;
		}
		else
		{
			break;
		}
	}
}

5. 源代码

heap.h

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
//这里的代码是建小堆。
typedef int HPDataType;
typedef struct Heap
{
	HPDataType\* a;
	int size;
	int capacity;
}Heap;

void HeapInit(Heap\* hp);
// 堆的销毁
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 AdjustUp(HPDataType\* a, int child);
//向下调整
void AdjustDown(HPDataType\* a, int n, int parent);
//交换
void swap(HPDataType\* child, HPDataType\* parent);
void HeapInit(Heap\* hp);
void HeapPrint(Heap\* hp);

heap.c

#include "Heap.h"

void HeapInit(Heap\* hp)
{
	assert(hp);
	hp->a = NULL;
	hp->size = hp->capacity = 0;
}
// 堆的销毁
void HeapDestory(Heap\* hp)
{
	assert(hp);
	free(hp->a);
	hp->a = NULL;
	hp->capacity = hp->size = 0;
}
// 堆的插入
void HeapPush(Heap\* hp, HPDataType x)
{
	assert(hp);
	if (hp->capacity == hp->size)
	{
		int newCapaciy = hp->capacity == 0 ? 4 : hp->capacity \* 2;
		HPDataType\* tmp = (HPDataType\*)realloc(hp->a, sizeof(HPDataType) \* newCapaciy);
		if (tmp == NULL)
		{
			perror("malloc fail");
			exit(-1);
		}
		hp->a = tmp;
		hp->capacity = newCapaciy;
	}
	hp->a[hp->size++] = x;
	AdjustUp(hp->a, hp->size - 1);
}
// 堆的删除
void HeapPop(Heap\* hp)
{
	assert(hp);
	assert(!HeapEmpty(hp));
	swap(&hp->a[0], &hp->a[hp->size - 1]);
	hp->size--;
	AdjustDown( hp->a,hp->size, 0);
}
// 取堆顶的数据
HPDataType HeapTop(Heap\* hp)
{
	assert(hp);
	assert(!HeapEmpty(hp));
	return hp->a[0];
}
// 堆的数据个数
int HeapSize(Heap\* hp)
{
	assert(hp);
	return hp->size;
}
// 堆的判空
int HeapEmpty(Heap\* hp)
{
	assert(hp);
	return hp->size == 0;
}

//向上调整
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 = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}

//向下调整
void AdjustDown(HPDataType\* a, int n, int parent)
{
	int minChild = parent \* 2 + 1;
	while (minChild < n)
	{
		if (minChild + 1 < n && a[minChild + 1] < a[minChild])
		{
			minChild++;
		}

		if (a[minChild] < a[parent])
		{
			swap(&a[minChild], &a[parent]);
			parent = minChild;
			minChild = parent \* 2 + 1;
		}
		else
		{
			break;
		}
	}
}

void swap(HPDataType\* child, HPDataType\* parent)
{
	HPDataType tmp = \*child;
	\*child = \*parent;
	\*parent = tmp;
}
void HeapPrint(Heap\* hp)
{
	assert(hp);
	assert(!HeapEmpty(hp));
	int i = 0;
	for (i = 0; i < hp->size; i++)
	{
		printf("%d ", hp->a[i]);
	}
	printf("\n");
}



![img](https://img-blog.csdnimg.cn/img_convert/503234050be0925241e2b4601cff9b67.png)
![img](https://img-blog.csdnimg.cn/img_convert/1a5b507e8b0746d694081a368a9ec46c.png)
![img](https://img-blog.csdnimg.cn/img_convert/a87c1d413fcb009659039301e2b582c7.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**

printf("\n");
}



[外链图片转存中...(img-kbc0z7H2-1714736387137)]
[外链图片转存中...(img-fTqvC75h-1714736387137)]
[外链图片转存中...(img-EURUFsgR-1714736387137)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值