堆的实现(C语言)

  如果有一个关键码的集合K = {k0,k1, k2,…,kn-1},把它的所有元素按完全二叉树的顺序存储方式存储在一个一维数组中,并满足:Ki <= K2i+1 且 Ki<= K2i+2 (Ki >= K2i+1 且 Ki >= K2i+2) i = 0,1,2…,则称为小堆(或大堆)。将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小堆或小根堆。
性质

堆中某个节点的值总是不大于或不小于其父节点的值;
堆总是一棵完全二叉树
父节点=(子节点 - 1) / 2
子节点=2 * 父节点 +1(或者 + 2)

1.向下调整算法
左右子树必须是一个堆

int a[] = {27,15,19,18,28,34,65,49,25,37};

在这里插入图片描述

2.堆得插入
向上调整算法
插入一个80:
在这里插入图片描述

3.堆的删除
删除就是删除堆顶元素

在这里插入图片描述
4.堆排序
在这里插入图片描述

实现
Heap.h

#pragma once;

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>

typedef int HPDataType;
typedef struct HPNode
{
	HPDataType* _a;
	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);
void HeapSort(Heap* hp);
void HeapPrint(Heap* hp);
void HeapTest();

Heap.c

#include"Heap.h"

//交换函数
void Swap(int * pa, int * pb)
{
	int tmp = *pa;
	*pa = *pb;
	*pb = tmp;
}

//向下调整算法,它的左右子树是堆
void ADJustDown(Heap* hp, int root, int n)
{
	assert(hp);
	int parent = root;
	int child = 2 * parent + 1;
	while (child < n)
	{
		//找出子节点中大的那一个结点
		if (hp->_a[child] < hp->_a[child + 1])
		{
			child++;
		}
		//如果孩子节点结点大于父节点,交换
		if (hp->_a[parent] < hp->_a[child])
			Swap(&hp->_a[parent], &hp->_a[child]);
		parent = child;
		child = 2 * parent + 1;
	}
}

void HeapInit(Heap* hp, HPDataType* a, int n)
{
	assert(hp);
	hp->_a = (HPDataType*)malloc(sizeof(HPDataType)* n);
	memcpy(hp->_a, a, sizeof(HPDataType) * n);
	hp->_size = n;
	hp->_capacity = n;
	//最后一个节点的下表是n-1,父节点=(子节点-1)/ 2
	for (int i = (n - 2) / 2; i >= 0; i--)
	{
		ADJustDown(hp, i, n);
	}
}

void HeapDestory(Heap* hp)
{
	assert(hp);
	free(hp->_a);
	hp->_size = 0;
	hp->_capacity = 0;
}

//向上调整算法
void ADJustUp(Heap* hp, int i)
{
	assert(hp);
	int child = i;
	int parent = (child - 1) / 2;
	while (child > 0)
	{
		if (hp->_a[child] > hp->_a[parent])
			Swap(&hp->_a[child], &hp->_a[parent]);
		child = parent;
		parent = (child - 1) / 2;
	}
}

void HeapPush(Heap* hp, HPDataType x)
{
	assert(hp);
	//判断空间是否够存
	if (hp->_size == hp->_capacity)
	{
		int newCapacity = (hp->_capacity == 0) ? 5 : hp->_capacity * 2;
		hp->_a = (HPDataType*)realloc(hp->_a, sizeof(HPDataType)* newCapacity);
		assert(hp->_a);
		hp->_capacity = newCapacity;
	}
	//先插入最后,再向上调整
	hp->_a[hp->_size++] = x;
	ADJustUp(hp, hp->_size - 1);
}

//先把最后一个节点和根节点交换,删除最后一个结点,之后用向下调整算法
void HeapPop(Heap* hp)
{
	assert(hp);
	Swap(&hp->_a[0], &hp->_a[hp->_size - 1]);
	hp->_size--;
	ADJustDown(hp, 0, hp->_size - 1);
}

HPDataType HeapTop(Heap* hp)
{
	assert(hp);
	return hp->_a[0];
}

int HeapSize(Heap* hp)
{
	assert(hp);
	return hp->_size - 1;
}

int HeapEmpty(Heap* hp)
{
	assert(hp);
	return hp->_size == 0 ? 0 : 1;
}

//把根节点和最后一个结点交换,不用管最后一个结点,向下调整算法,
//找出次大的,与倒数第二个结点交换,以此类推
void HeapSort(Heap* hp)
{
	assert(hp);
	for (int i = (hp->_size - 2) / 2; i > 0; i--)
	{
		ADJustDown(hp, i, hp->_size);
	}
	int end = hp->_size - 1;
	while (end > 0)
	{
		Swap(&hp->_a[0], &hp->_a[end]);
		end--;
		ADJustDown(hp, 0, end);
	}
}

void HeapPrint(Heap* hp)
{
	int i = 0;
	for (i = 0; i < hp->_size; i++)
		printf("%d ", hp->_a[i]);
	printf("\n");
}

//测试
void HeapTest()
{
	int a[] = { 1, 5, 3, 8, 7, 6 };
	int size = sizeof(a) / sizeof(a[0]);
	Heap hp;
	HeapInit(&hp, a, size);
	HeapPrint(&hp);
	HeapPush(&hp, 7);
	HeapPrint(&hp);
	HeapPop(&hp);
	HeapPop(&hp);

	HeapPrint(&hp);
	HeapSort(&hp);
	HeapPrint(&hp);

	HeapDestory(&hp);
}

main.c

#include"Heap.h"

int main()
{
	HeapTest();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值