手撕二叉树--堆的接口实现(附源码+图解)

堆的接口实现(附源码+图解)



前言

本文主要介绍二叉树中堆的增删查改等接口实现,结尾附总源码!


一、定义结构体

在这里插入图片描述

代码如下(示例):

typedef int HPDataType;
typedef struct Heap
{
    
    
	HPDataType* a; //定义数组二叉树
	int size;      //二叉树结点个数
	int capacity;  //二叉树容量(考虑增容)
}HP;

二、接口实现(附图解+源码)

在这里插入图片描述
这里一共11个接口,我会我都会一 一为大家讲解(图解+源码


1.初始化堆

由于二叉树中堆的存储结构是数组,所以初始化和销毁两个接口和顺序表等数据结构一样这里不做过多介绍!

代码如下(示例):

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

2.销毁堆

代码如下(示例):

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

3.尾插数据

在这里插入图片描述


代码如下(示例):

void HeapPush(HP* hp, HPDataType x)
{
    
    
	assert(hp);
	if (hp->size == hp->capacity)
	{
    
    
		size_t newCapacity = hp->capacity == 0 ? 4 : hp->capacity * 2;
		HPDataType* tmp = realloc(hp->a, sizeof(HPDataType) * newCapacity);
		if (tmp == NULL)
		{
    
    
			printf("realloc fail\n");
			exit(-1);
		}
		hp->a = tmp;
		hp->capacity = newCapacity;
	}
	hp->a[hp->size] = x;
	hp->size++;
}

这里要注意:无论是大堆还是小堆都要满足 父亲结点 > or < 孩子结点(二叉树性质),这里我们采用向上下调整!push数据时只改变这条路径上的数据!
在这里插入图片描述


(1)向上调整

假设我们在插入几个数据之后,如下图!向上调整只会改变一条路径!!!
在这里插入图片描述

向上调整可以转变为 父亲结点一定大于孩子结点


在这里插入图片描述


在这里插入图片描述


代码如下(示例):

void AdjustUp(int* a, int child)
{
    
    
	assert(a);
	int parent = (child - 1) / 2;
	//while (parent >= 0)
	while (child > 0)
	{
    
    
		if (a[child] > a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);

			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
    
    
			break;
		}
	}
}

(2)交换函数

在进行函数传参的时候,形式参数是实际参数的一份临时拷贝,所以要传指针!

代码如下(示例):

void Swap(HPDataType * px, HPDataType * py)
{
    
    
	HPDataType tmp = *px;
	*px = *py;
	*py = tmp;
}

(3)向下调整


在这里插入图片描述

注意参数这里: 表示 从 parent结点 到 n结点 之间所有结点的算法,最高点是最小值
在这里插入图片描述

在这里插入图片描述


向下调整需要保证,top(最高结点)为最小值!
在这里插入图片描述


在这里插入图片描述


4.打印数据

由于二叉树存储结构是数组,打印的时候直接遍历即可!

代码如下(示例):

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

5.判断是否为空

代码如下(示例):

bool HeapEmpty(HP* hp)
{
    
    
	assert(hp);
	return hp->size == 0;
}

6.节点个数

代码如下(示例):

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

7.堆顶数据

不要忘记对数组进行判空操作,否则会出现野指针问题!

代码如下(示例):

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

8.删除堆顶的数据

这里要注意:在我们删除完数据之后要保护堆的性质结构(Adjustdown)。
在这里插入图片描述


在这里插入图片描述


三、源代码展示

(1)Heap.h(接口函数的声明)

代码如下(示例):

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int HPDataType;
typedef struct Heap
{
    
    
	HPDataType* a;
	int size;
	int capacity;
}HP;
void AdjustUp(int* a, int child);//向上调整
void AdjustDown(int* a, int n, int parent);//向下调整
void Swap(HPDataType* px, HPDataType* py);//交换
void HeapInit(HP* hp);//初始化堆
void HeapDestroy(HP* hp);//销毁堆
void HeapPush(HP* hp, HPDataType x);//尾插数据
void HeapPop(HP* hp);//删除堆顶的数据
HPDataType HeapTop(HP* hp);//取堆顶的数据
void HeapPrint(HP* hp);//打印堆(数组)
bool HeapEmpty(HP* hp);//判断堆是否为空
int HeapSize(HP* hp);//堆结点个数

(2)Heap.c(接口函数的实现)

代码如下(示例):

#include "Heap.h"
void Swap(HPDataType * px, HPDataType * py)
{
    
    
	HPDataType tmp = *px;
	*px = *py;
	*py = tmp;
}
void HeapInit(HP* hp)
{
    
    
	assert(hp);
	hp->a = NULL;
	hp->size = hp->capacity = 0;
}
void HeapDestroy(HP* hp)
{
    
    
	assert(hp);
	free(hp->a);
	hp->capacity = hp->size = 0;
}
void AdjustUp(int* a, int child)
{
    
    
	assert(a);
	int parent = (child - 1) / 2;
	//while (parent >= 0)
	while (child > 0)
	{
    
    
		if (a[child] > a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);

			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
    
    
			break;
		}
	}
}
void HeapPrint(HP* hp)
{
    
    
	for (int i = 0; i < hp->size; ++i)
	{
    
    
		printf("%d ", hp->a[i]);
	}
	printf("\n");
}
void HeapPush(HP* hp, HPDataType x)
{
    
    
	assert(hp);
	if (hp->size == hp->capacity)
	{
    
    
		size_t newCapacity = hp->capacity == 0 ? 4 : hp->capacity * 2;
		HPDataType* tmp = realloc(hp->a, sizeof(HPDataType) * newCapacity);
		if (tmp == NULL)
		{
    
    
			printf("realloc fail\n");
			exit(-1);
		}
		hp->a = tmp;
		hp->capacity = newCapacity;
	}
	hp->a[hp->size] = x;
	hp->size++;
	AdjustUp(hp->a, hp->size - 1);
}
bool HeapEmpty(HP* hp)
{
    
    
	assert(hp);
	return hp->size == 0;
}
int HeapSize(HP* hp)
{
    
    
	assert(hp);
	return hp->size;
}
HPDataType HeapTop(HP* hp)
{
    
    
	assert(hp);
	assert(!HeapEmpty(hp));
	return hp->a[0];
}
void AdjustDown(int* 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 = parent * 2 + 1;
		}
		else
		{
    
    
			break;
		}
	}
}
// 删除堆顶的数据
void HeapPop(HP* hp)
{
    
    
	assert(hp);
	assert(!HeapEmpty(hp));
	Swap(&hp->a[0], &hp->a[hp->size - 1]);
	hp->size--;
	AdjustDown(hp->a, hp->size, 0);
}

(3)test.c(测试+主函数)

代码如下(示例):

#include"Heap.h"
void TestHeap()
{
    
    
	int a[] = {
    
     70, 56, 30, 25, 15, 10, 75 };
	HP hp;
	HeapInit(&hp);
	for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
	{
    
    
		HeapPush(&hp, a[i]);
	}
	HeapPrint(&hp);

	HeapPop(&hp);
	HeapPrint(&hp);

	HeapPop(&hp);
	HeapPrint(&hp);

	HeapPop(&hp);
	HeapPrint(&hp);

	HeapPop(&hp);
	HeapPrint(&hp);

	HeapDestroy(&hp);
}

总结

以上就是今天要讲的内容,本文介绍二叉树中堆的接口实现(附图解和源码)!
如果我的博客对你有所帮助记得三连支持一下,感谢大家的支持!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/2201_75587702/article/details/129786039