关于堆的操作

本文介绍了一种使用C++实现的堆操作方法,包括调整堆顺序、插入和删除元素等基本操作,并通过实例演示了最大堆和最小堆的创建过程及堆排序算法的应用。

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

/*
堆操作
来源:Data Structures with C++ Using STL
*/

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <functional>
using namespace std;

//调整堆的顺序
template <typename T, typename Compare>
void adjustHeap(vector<T>& v, int first, int last, Compare comp)
{
	int currentPos, childPos;
	T target;

	currentPos = first;
	target = v[first];

	childPos = 2 * currentPos + 1;

	while (childPos <= last - 1)
	{
		//判断左右子节点的大小
		if ((childPos + 1 <= last - 1) && comp(v[childPos + 1], v[childPos]))
			childPos = childPos + 1;


		if (comp(v[childPos], target))
		{
			v[currentPos] = v[childPos];

			currentPos = childPos;
			childPos = 2 * currentPos + 1;

		}
		else
			break;
	}
	v[currentPos] = target;
}

//堆的插入操作
template <typename T, typename Compare>
void pushHeap(vector<T> &v, int last, Compare comp)
{
	int currentPos, parentPos;
	T target;

	currentPos = last - 1;
	parentPos = (currentPos - 1) / 2;
	target = v[last - 1];

	//遍历父结点的路径
	while (currentPos != 0)
	{
		if (comp(target, v[parentPos]))
		{
			v[currentPos] = v[parentPos];
			currentPos = parentPos;
			parentPos = (currentPos - 1) / 2;
		}
		else
			break;
	}
	v[currentPos] = target;

}
//从堆中删除数据
template <typename T, typename Compare>
void popHeap(vector<T>& v, int last, Compare comp)
{
	T temp;
	temp = v[0];						//交换堆顶元素和最后一个元素
	v[0] = v[last - 1];
	v[last - 1] = temp;

	adjustHeap(v, 0, last - 1, comp);	//调整0-(last-1)之间的次序

}

//向量堆化
template <typename T, typename Compare>
void makeHeap(vector<T>& v, Compare comp)
{
	int heapPos, lastPos;
	lastPos = v.size();
	heapPos = (lastPos - 2) / 2;
	while (heapPos >= 0)
	{
		adjustHeap(v, heapPos, lastPos, comp);
		heapPos--;
	}
}

//堆排序
template <typename T, typename Compare>
void heapSort(vector<T>& v, Compare comp)
{
	makeHeap(v, comp);

	int i, n = v.size();
	for (i = n; i > 1; i--)
	{
		popHeap(v, i, comp);
	}
}

void main()
{
	int arr[] = { 5, 9, 2, 7, 1, 3, 8 };
	int i, arrSize = sizeof(arr) / sizeof(int);
	vector<int> vA, vB;

	for (i = 0; i < arrSize; i++)
	{
		vA.push_back(arr[i]);
		pushHeap(vA, vA.size(), greater<int>());
		vB.push_back(arr[i]);
		pushHeap(vB, vB.size(), less<int>());
	}

	cout << "Maximum heap:";
	while (!vA.empty())
	{
		popHeap(vA, vA.size(), greater<int>());
		cout << vA.back() << " ";
		vA.pop_back();
	}
	cout << endl;
	cout << "Minimum heap:";
	while (!vB.empty())
	{
		popHeap(vB, vB.size(), less<int>());
		cout << vB.back() << " ";
		vB.pop_back();
	}
	cout << endl;

	//堆排序
	vector<int> v = { 5, 9, 2, 7, 1, 3, 8 };
	heapSort(v, greater<int>());
	for (i = 0; i < v.size(); i++)
		cout << v[i] << " ";
	cout << endl;
	heapSort(v, less<int>());
	for (i = 0; i < v.size(); i++)
		cout << v[i] << " ";
	cout << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值