c版数据结构--堆的基本操作

本文深入讲解堆数据结构的实现原理,包括大根堆和小根堆的概念,以及堆的创建、插入、删除、调整和排序等基本操作。通过具体代码示例,详细解释了堆的向上调整和向下调整算法,帮助读者理解堆的内部运作机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

堆:逻辑上是一种特殊的完全二叉树,存储方式上是一个线性表。其基本操作有:堆的创建、堆的向下调整算法、堆的插入、堆的删除、堆的排序等,代码如下:
1、首先自定义堆的库函数

#ifndef _HEAP_H_
#define _HEAP_H_

typedef int HPDataType;
typedef struct Heap 
{ 
	HPDataType* data;    
	int size;    
	int capacity; 
}Heap;

void HeapInit(Heap* hp, HPDataType* a, int n);
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);

#endif

2、实现各种基本操作

#include "heap.h"//本程序所建堆为大根堆
#include <stdlib.h>
#include <stdio.h>
void adjustDown(Heap* hp,int n)
{
	int cur = n;
	while (cur * 2 + 1 < hp->size)//没有左孩子就跳出,说明其为叶子结点
	{
		if (cur * 2 + 2 >= hp->size)//没有右孩子
		{
			n = cur * 2 + 1;
		}
		else
		{
			if (hp->data[cur * 2 + 1] > hp->data[cur * 2 + 2])//当左孩子大于右孩子
			{
				n = cur * 2 + 1;
			}
			else
			{
				n = cur * 2 + 2;
			}
			if (hp->data[cur] < hp->data[n])//比较cur和n所对应的值的大小
				//若cur的值小于data,则进行如下交换
			{
				int tmp = hp->data[cur];
				hp->data[cur] = hp->data[n];
				hp->data[n] = tmp;
				cur = n;//重新将新n的值付给cur以进行下一次的循环
			}
			else
			{
				break;
			}
		}
	}
}
void HeapInit(Heap* hp, HPDataType* a, int n)
//size和capacity的区别:size是真实占用空间的大小
//capacity表示发生在realloc前能允许的最大元素数
//void* calloc (size_t num, size_t size);
//函数的功能是为 num 个大小为 size 的元素开辟一块空间,
//并且把空间的每个字节初始化为0。

{
	hp->capacity = n * 2;
	hp->size = n;
	hp->data = (HPDataType*)calloc(hp->capacity,sizeof(HPDataType));
	int i;
	for (i = 0; i < n; i++)//给堆的数组赋值
	{
		hp->data[i] = a[i];
	}
	for (i = n / 2 - 1; i >= 0; i--)//找到最后一个非叶子结点进行向下调整
	{
		adjustDown(hp ,i);
	}
}
void HeapDestory(Heap* hp)//堆的销毁
{
	if (hp->data)
	{
		free(hp->data);
	}
	hp->data = NULL;
	hp->size = hp->capacity = 0;
}
void HeapPush(Heap* hp, HPDataType x)//堆的插入
{
	if (hp->size == hp->capacity)//当堆的大小等于容量的大小,就要扩容
	{
		hp->capacity *= 2;
		hp->data = (HPDataType*)realloc(hp->data, hp->capacity * sizeof(HPDataType));
			//用relloc对其进行扩容
			//reclloc的用法:void* realloc (void* ptr, size_t size);
			//把ptr所指向的空间扩大或缩小到size个字节
		int cur = hp->size;
		hp->data[hp->size] = x;
		hp->size++;
		while (cur > 0)//向上调整算法
		{
			if (hp->data[cur] > hp->data[(cur - 1) / 2])//hp->data[(cur - 1) /2 为cur
				//的父结点的下标
			{
				int tmp = hp->data[cur];
				hp->data[cur] = hp->data[(cur - 1) / 2];
				hp->data[(cur - 1) / 2] = tmp;
				cur = (cur - 1) / 2;
			}
			else
			{
				break;
			}
		}
	}
}
void HeapPop(Heap* hp)//堆的数据删除,把堆顶数据和最后一个数据交换
//再把堆顶数据利用向下调整算法进行调整
{
	if (hp->size == 0)
	{ 
		return;
	}
	hp->size--;
	int tmp = hp->data[0];
	hp->data[0] = hp->data[hp->size];
	hp->data[hp->size] = tmp;
	adjustDown(hp, 0);
}
HPDataType HeapTop(Heap* hp)//获取堆顶数据
{
	if (hp->size == 0)//如果该堆的数据个数为0返回二叉树数据类型的0。
	{
		return (HPDataType)0;
	}
	return hp->data[0];
}
int HeapSize(Heap* hp)
{
	return hp->size;
}
int HeapEmpty(Heap* hp)//空为0、非空为1
{
	return hp->size == 0 ? 0 : 1;//如果size=0 返回0否则返回1
}
void HeapPrintS(Heap* hp)//打印函数
{
	int i;
	for (i = 0; i < hp->size; i++)
	{
		printf("%d", hp->data[i]);
	}
}
void HeapPrint(Heap * hp)//按二叉树的方式进行打印
{
	int i;
	int rn = 0;
	int bin = 2;
	for (i = 0; i < hp->size; i++)
	{
		printf("%d", hp->data[i]);
		if (i == rn)
		{
			putchar('\n');
			rn  += bin;
			bin *= 2;
		}
	}
}
void HeapSort(Heap* hp)//堆排序
{
	int tmp = hp->size;
	while (hp->size > 1)
	{
		HeapPop(hp);
	}
	hp->size = tmp;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值