数据结构:大顶堆、小顶堆

前言:

       堆是其中一种非常重要且实用的数据结构。堆可以用于实现优先队列,进行堆排序,以及解决各种与查找和排序相关的问题。本文将深入探讨两种常见的堆结构:大顶堆和小顶堆,并通过 C++ 语言展示如何实现和使用它们。

1. 什么是大顶堆和小顶堆?

前提:

        堆是一种完全二叉树。完全二叉树的定义:所有节点从上往下,从左往右的依次排列,不能有空位置,是为完全二叉树

        下面是完全二叉树和不完全二叉树的示意图:

在这里插入图片描述

大顶堆:

        根节点(堆顶元素)是所有节点中的最大值(父节点都大于左右子节点)。大顶堆常用于实现优先队列,且可用于构建堆排序算法。

小顶堆:

        小顶堆中的根节点是所有节点中的最小值(父节点都小于左右子节点)。小顶堆常用于问题如:查找流中的前 K 个最小元素。

示意图:
在这里插入图片描述

2. 大小顶堆的底层实现

        一般来说,定义二叉树都是定义一个节点类,节点类中包含本节点的数据元素和两个节点指针,分别指向左右子节点,通过这两个节点指针将整个树节点连起来。

代码:

template <typename Type>
class BstNode
{
public:
	BstNode():leftChild(nullptr), rightChild(nullptr){};
	BstNode(Type _content) :leftChild(nullptr), rightChild(nullptr)
	{
		content = _content;
	};
private:
	Type content;
	BstNode<Type> *leftChild;
	BstNode<Type> *rightChild;
};

         实际上,堆一般用数组表示,如下图。究其原因,我认为的是通过节点类的方式占用的内存空间多,多加了两个指针。并且当我们做插入操作向上渗透时,需要找到节点的父节点(文章后面会详细描述),这样实际的节点应该再加一个标识父节点的指针。这样实际是多了3个指针。

图示: 

3. 代码实现小顶堆

3.1 定义小顶堆类

template<typename T>
class MinHeap
{
public:
	MinHeap(int _maxsize = 10);
	~MinHeap();

	void Push(T _data);//插入数据
	void Pop();//删除堆顶数据
	T& Top();//获取堆顶数据

	void ShowHeap();//打印堆
private:
	T *heap;
	int MaxSize;//堆数组的最大大小
	int currentSize;//堆数组当前大小
};

3.2 构造函数

template<typename T>
MinHeap<T>::MinHeap(int _maxsize)
:MaxSize(_maxsize)
{
	try{
		if (MaxSize < 1)
			throw "Error Size,< 1";
		heap = new T[MaxSize];
		currentSize = 0;
	}
	catch (const char* cp){

		//1.提示用户错误信息,提示用户重新输入
		int size = 0;
		while (size < 1){
			std::cout << cp << std::endl;
			std::cout << "please Enter size again" << std::endl;
			std::cin >> size;
		}

		//2.初始化MinHeap类的数据成员
		MaxSize = size;
		heap = new T[MaxSize];
		currentSize = 0;
	}
}

        解释:这里我做了异常处理。当堆数组的大小MaxSize<1时,抛出异常,并提示输入大小,一直到输入的大小>=1。

3.3 插入

插入方法:
  1. 首先将需插入的数据放在堆的最后一个位置
  2. 然后依次和父节点比较,比父节点小就和父节点交换,再向上比较;比父节点大就停止比较。

比如我按20->30->15->10->9->8->12依次插入,插入示意图如下:

在这里插入图片描述

代码入下:
template<typename T>
void MinHeap<T>::Push(T _data)
{
	if (currentSize == MaxSize){
		//堆满了,扩容
	}
	//将数据放入最后一个位置
	//注意:currentSize和数组索引值差1:
	//比如currentSize = 3,对应的数组索引为2
	//所以,当currentSize=3时,即堆数组有3个数据时,新插入的数据应在数组索引3上
	heap[currentSize] = _data;

	int sonIndex = currentSize;//最后一个位置的数组索引
	int fatherIndex = (currentSize - 1)/ 2;//最后一个位置的父节点的数组索引

	while (sonIndex > 0){
		if (heap[sonIndex] < heap[fatherIndex]){
			std::swap(heap[sonIndex], heap[fatherIndex]);//交换父子节点元素
			
			//继续向上
			sonIndex = fatherIndex;
			fatherIndex = (fatherIndex - 1) / 2;
		}
		else break;
	}

	currentSize++;//堆数组当前大小加1
}

        currentSize表示的是当前堆数组有多少个元素。如下图,比如currentSize = 3,表明当前有3个数据。再push一个数据时,在数组的存储的位置刚好是索引3。

示意图:

        如下图,节点7和节点4的父节点都是节点2。节点7的索引为1,节点4索引为2,节点2的索引为0,所以计算7 4 节点的父节点公式为==(索引-1)/2==,其他节点也一样。这里你不从数组索引角度,从树的层序角度也是一样,只不过公式要变为:层序号/2。

扩大堆数组容量
        在上述代码中,当currentSize == MaxSize时,表示堆数组已经满了,此时我们需要扩大数组的大小,这和循环队列和顺序栈是一样的原理。
循环队列:带你一步步用C++实现循环队列
顺序栈:用C++实现顺序栈(以数组为底层数据结构,可自动扩容。
代码如下:

template<typename T>
void MinHeap<T>::ChangeSize()
{
	int size = MaxSize * 2;//堆数组容量扩大1倍

	T* tmp = new T[size];//1.申请一块原来2倍大小的空间。
	std::copy(heap, heap + MaxSize, tmp);//2.将栈中的数据赋值到新的内存空间
	delete[] heap;//3.删除老的空间

	heap = tmp;//4.heap指向新地址
	MaxSize = size;//5.改变MaxSize
}

        注意:由于C++编译器是不检查数组越界的,所以,即使程序员不自己扩容,使用越界的索引程序也不会报错,只是它会继续向下占用一块内存。程序员必须注意数组越界的问题。

3.4 删除

删除步骤:
  1. 首先把堆顶元素删除
  2. 接着把堆的最后一个数据放在堆顶
  3. 最后把堆顶数据向下渗透,不断的和两个子节点比较,若父节点不比两个子节点的任意一个小,取两个子节点中小的和父节点交换,一直这样下去,直到父节点比左右子节点都小。
示意图:

在这里插入图片描述

代码如下:
template<typename T>
void MinHeap<T>::Pop()
{
	if (currentSize == 0){
		std::cout << "Error!The heap is empty,Invalid operation." << std::endl;
		return;
	}

	heap[0] = heap[--currentSize];//删除堆顶,并将最后一个数据放在堆顶

	int fatherIndex = 0;//父节点索引为堆顶
	int leftSonIndex = fatherIndex * 2 + 1;//获得左子节点索引
	int RightSonIndex = leftSonIndex + 1;//获得右子节点索引

	//确认循环条件
	while (leftSonIndex < currentSize ){
		/**1.找左右子节点中最小值的数组索引***/
		int minIndex;
		//如果RightSonIndex < currentSize,表明fatherIndex有右子节点
		if (RightSonIndex < currentSize){
			minIndex = heap[leftSonIndex] > heap[RightSonIndex] ?  RightSonIndex: leftSonIndex;
		}
		else{
			minIndex = leftSonIndex;
		}

		/**2.父节点大就交换,否则退出循环***/
		if (heap[fatherIndex] > heap[minIndex])
			std::swap(heap[fatherIndex], heap[minIndex]);
		else break;

		fatherIndex = minIndex;
		leftSonIndex = fatherIndex * 2 + 1;
		RightSonIndex = leftSonIndex + 1;
	}
}

对于while循环条件说明:
        使用左子节点leftSonIndex和currentSize比较,不要使用右子节点RightSonIndex,因为右子节点RightSonIndex可能没有。当然,你也可以使用父节点fatherIndex和currentSize比较:fatherIndex<currentSize/2。
验证循环的真确性,只需考虑以下三种情况,大家可自行验证。


析构函数:
        析构只需将申请的内存释放即可。

template<typename T>
MinHeap<T>::~MinHeap()
{
	delete []heap; 
}

4. 代码实现大顶堆

        原理和小顶堆一样,直接给出代码

#ifndef _MAX_HEAP_H
#define _MAX_HEAP_H

template<typename T>
class MaxHeap
{
public:
	MaxHeap(int _maxsize = 10);
	~MaxHeap();

	void Push(T _data);//插入数据
	void Pop();//删除堆顶数据
	T& Top();//获取堆顶数据

	void ShowHeap();
private:
	T *heap;
	int MaxSize;//堆数组的最大大小
	int currentSize;//堆数组当前大小

	void ChangeSize();//扩大堆数组的大小
};

template<typename T>
MaxHeap<T>::MaxHeap(int _maxsize)
:MaxSize(_maxsize)
{
	try{
		if (MaxSize < 1)
			throw "Error Size,< 1";
		heap = new T[MaxSize];
		currentSize = 0;
	}
	catch (const char* cp){

		//1.提示用户错误信息,提示用户重新输入
		int size = 0;
		while (size < 1){
			std::cout << cp << std::endl;
			std::cout << "please Enter size again" << std::endl;
			std::cin >> size;
		}

		//2.初始化MaxHeap类的数据成员
		MaxSize = size;
		heap = new T[MaxSize];
		currentSize = 0;
	}
}
template<typename T>
MaxHeap<T>::~MaxHeap()
{
	delete[]heap;
}
template<typename T>
void MaxHeap<T>::Push(T _data)
{
	if (currentSize == MaxSize){
		//堆满了,扩容
		ChangeSize();
	}
	//将数据放入最后一个位置
	//注意:currentSize和数组索引值差1:
	//比如currentSize = 3,对应的数组索引为2
	//所以,当currentSize=3时,即堆数组有3个数据时,新插入的数据应在数组索引3上
	heap[currentSize] = _data;

	int sonIndex = currentSize;//最后一个位置的数组索引
	int fatherIndex = (currentSize - 1) / 2;//最后一个位置的父节点的数组索引

	while (sonIndex > 0){
		if (heap[sonIndex] > heap[fatherIndex]){
			std::swap(heap[sonIndex], heap[fatherIndex]);

			//继续向上
			sonIndex = fatherIndex;
			fatherIndex = (fatherIndex - 1) / 2;
		}
		else break;
	}

	currentSize++;
}
template<typename T>
void MaxHeap<T>::Pop()
{
	if (currentSize == 0){
		std::cout << "Error!The heap is empty,Invalid operation." << std::endl;
		return;
	}

	heap[0] = heap[--currentSize];//删除堆顶,并将最后一个数据放在堆顶

	int fatherIndex = 0;//父节点索引为堆顶
	int leftSonIndex = fatherIndex * 2 + 1;//获得左子节点索引
	int RightSonIndex = leftSonIndex + 1;//获得右子节点索引

	//确认循环条件
	while (leftSonIndex < currentSize){
		/**1.找左右子节点中最小值的数组索引***/
		int maxIndex;
		//如果RightSonIndex < currentSize,表明fatherIndex有右子节点
		if (RightSonIndex < currentSize){
			maxIndex = heap[leftSonIndex] < heap[RightSonIndex] ? RightSonIndex : leftSonIndex;
		}
		else{
			maxIndex = leftSonIndex;
		}

		/**2.父节点小就交换,否则退出循环***/
		if (heap[fatherIndex] < heap[maxIndex])
			std::swap(heap[fatherIndex], heap[maxIndex]);
		else break;

		fatherIndex = maxIndex;
		leftSonIndex = fatherIndex * 2 + 1;
		RightSonIndex = leftSonIndex + 1;
	}
}
template<typename T>
T& MaxHeap<T>::Top()
{
	if (currentSize == 0)
		throw "Error.The heap is empty,there is not data.";
	return heap[0];
}

template<typename T>
void MaxHeap<T>::ChangeSize()
{
	int size = MaxSize * 2;//堆数组容量扩大1倍

	T* tmp = new T[size];//1.申请一块原来2倍大小的空间。
	std::copy(heap, heap + MaxSize, tmp);//2.将栈中的数据赋值到新的内存空间
	delete[] heap;//3.删除老的空间

	heap = tmp;//4.heap指向新地址
	MaxSize = size;//5.改变MaxSize
}

template<typename T>
void MaxHeap<T>::ShowHeap()
{
	int size = 1;
	for (int i = 0; i < currentSize;){
		std::cout << "[ ";
		for (int j = 0; j < size && i < currentSize; j++){
			std::cout << heap[i++] << " ";
		}
		std::cout << "]" << std::endl;
		size *= 2;
	}
}

#endif

5. 测试

        下面只测试小顶堆,大顶堆可自行测试。

MinHeap.h

#ifndef _MIN_HEAP_H
#define _MIN_HEAP_H

template<typename T>
class MinHeap
{
public:
	MinHeap(int _maxsize = 10);
	~MinHeap();

	void Push(T _data);//插入数据
	void Pop();//删除堆顶数据
	T& Top();//获取堆顶数据

	void ShowHeap();
private:
	T *heap;
	int MaxSize;//堆数组的最大大小
	int currentSize;//堆数组当前大小

	void ChangeSize();//扩大堆数组的大小
};

template<typename T>
MinHeap<T>::MinHeap(int _maxsize)
:MaxSize(_maxsize)
{
	try{
		if (MaxSize < 1)
			throw "Error Size,< 1";
		heap = new T[MaxSize];
		currentSize = 0;
	}
	catch (const char* cp){

		//1.提示用户错误信息,提示用户重新输入
		int size = 0;
		while (size < 1){
			std::cout << cp << std::endl;
			std::cout << "please Enter size again" << std::endl;
			std::cin >> size;
		}

		//2.初始化MinHeap类的数据成员
		MaxSize = size;
		heap = new T[MaxSize];
		currentSize = 0;
	}
} 
template<typename T>
MinHeap<T>::~MinHeap()
{
	delete []heap; 
}
template<typename T>
void MinHeap<T>::Push(T _data)
{
	if (currentSize == MaxSize){
		//堆满了,扩容
		ChangeSize();
	}
	//将数据放入最后一个位置
	//注意:currentSize和数组索引值差1:
	//比如currentSize = 3,对应的数组索引为2
	//所以,当currentSize=3时,即堆数组有3个数据时,新插入的数据应在数组索引3上
	heap[currentSize] = _data;

	int sonIndex = currentSize;//最后一个位置的数组索引
	int fatherIndex = (currentSize - 1)/ 2;//最后一个位置的父节点的数组索引
	 
	while (sonIndex > 0){
		if (heap[sonIndex] < heap[fatherIndex]){
			std::swap(heap[sonIndex], heap[fatherIndex]);
			
			//继续向上
			sonIndex = fatherIndex;
			fatherIndex = (fatherIndex - 1) / 2;
		}
		else break;
	}

	currentSize++;
}
template<typename T>
void MinHeap<T>::Pop()
{
	if (currentSize == 0){
		std::cout << "Error!The heap is empty,Invalid operation." << std::endl;
		return;
	}

	heap[0] = heap[--currentSize];//删除堆顶,并将最后一个数据放在堆顶

	int fatherIndex = 0;//父节点索引为堆顶
	int leftSonIndex = fatherIndex * 2 + 1;//获得左子节点索引
	int RightSonIndex = leftSonIndex + 1;//获得右子节点索引

	//确认循环条件
	while (leftSonIndex < currentSize ){
		/**1.找左右子节点中最小值的数组索引***/
		int minIndex;
		//如果RightSonIndex < currentSize,表明fatherIndex有右子节点
		if (RightSonIndex < currentSize){
			minIndex = heap[leftSonIndex] > heap[RightSonIndex] ?  RightSonIndex: leftSonIndex;
		}
		else{
			minIndex = leftSonIndex;
		}

		/**2.父节点大就交换,否则退出循环***/
		if (heap[fatherIndex] > heap[minIndex])
			std::swap(heap[fatherIndex], heap[minIndex]);
		else break;

		fatherIndex = minIndex;
		leftSonIndex = fatherIndex * 2 + 1;
		RightSonIndex = leftSonIndex + 1;
	}
}
template<typename T>
T& MinHeap<T>::Top()
{
		if (currentSize == 0)
			throw "Error.The heap is empty,there is not data.";
		return heap[0];
}

template<typename T>
void MinHeap<T>::ChangeSize()
{
	int size = MaxSize * 2;//堆数组容量扩大1倍

	T* tmp = new T[size];//1.申请一块原来2倍大小的空间。
	std::copy(heap, heap + MaxSize, tmp);//2.将栈中的数据赋值到新的内存空间
	delete[] heap;//3.删除老的空间

	heap = tmp;//4.heap指向新地址
	MaxSize = size;//5.改变MaxSize
}

template<typename T>
void MinHeap<T>::ShowHeap()
{
	int size = 1;
	for (int i = 0; i < currentSize;){
		std::cout << "[ ";
		for (int j = 0; j < size && i < currentSize; j++){
			std::cout <<heap[i++] <<" ";
		}
		std::cout << "]"<<std::endl;
		size *= 2;
	}
}

#endif

Main.cpp

#include <iostream>
#include "MinHeap.h"
#include "MaxHeap.h"

using namespace std;

int main()
{
	MinHeap<int> a(4);
	
	/*************插入***********/
	a.Push(20);

	a.ShowHeap();
	cout << endl;

	a.Push(30);

	a.ShowHeap(); 
	cout << endl;

	a.Push(15);

	a.ShowHeap();
	cout << endl;

	a.Push(10);

	a.ShowHeap();
	cout << endl;

	a.Push(9);

	a.ShowHeap();
	cout << endl;

	a.Push(8);

	a.ShowHeap();
	cout << endl;

	a.Push(12);

	a.ShowHeap();
	cout << endl;


	/*************删除***********/
	cout << "**********删除************" << endl;
	cout << a.Top() << endl;
	a.Pop();
	a.ShowHeap();
	cout << endl;

	cout << a.Top() << endl;
	a.Pop();
	a.ShowHeap();
	cout << endl;

	cout << a.Top() << endl;
	a.Pop();
	a.ShowHeap();
	cout << endl;

	cout << a.Top() << endl;
	a.Pop();
	a.ShowHeap();
	cout << endl;

	cout << a.Top() << endl;
	a.Pop();
	a.ShowHeap();
	cout << endl;
	
	cout << a.Top() << endl;
	a.Pop();
	a.ShowHeap();
	cout << endl;

	cout << a.Top() << endl;
	a.Pop();
	a.ShowHeap();
	cout << endl;

	a.Pop();

	system("pause");
	return 0;
}

结果展示

 

总结

        大顶堆和小顶堆都是堆数据结构的变种,它们分别能够在 O(1) 时间内访问最大和最小元素。在 C++ 中,可以方便地使用 std::priority_queue 创建并操作这两种堆。无论是大顶堆还是小顶堆,插入元素和删除堆顶元素的时间复杂度都是 O(log n),其中 n 为堆中的元素数量。

  • 10
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SecureCode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值