基于vector实现带有键值的最小(大)堆

最小(大)堆作为一个二叉树结构,广泛应用于最值维护问题。其时间复杂度相对于最小值查找来说,能够从O(n)降低到O(logn)。这里,我们介绍基于vector和C++标准库的带键值最小堆实现。

首先需要引入相关的头文件:

# include <vector>
# include <functional>
# include <algorithm>

如果是不带键值的最小堆,比较容易实现。

vector<int> minList;
make_heap(minList.begin(), minList.end(), greater<int>());

//minlist变成最小堆

//堆更新,将vector的最有一个值,加入到已经建堆的vector里
minlist.push_back(9);
push_heap(minlist.begin(), minlist.end(), great<int>());

//堆弹出,将堆的根节点弹出到vector的最后,并弹出
pop_heap(minlist.begin(), minlist.end(), great<int>());
minlist.pop_back();

在实际应用中,数据往往是以键值的形式进行处理的。除了进行处理比较值外,还有对应的键。我们希望将键值做为一对信息进行处理。这里,就要引入Pair<>数据类型。

vector<pair<double, int>> minKeyHeap;
minKeyHeap.push_back(make_pair(distance_i, index_i));
make_heap(minKeyHeap.begin(), minKeyHeap.end(), greater<pair<double, char>>());

类似于sort,比较方法也能够被重写,参照如下格式。

// TEMPLATE STRUCT greater
template<class _Ty>
struct greater : public binary_function<_Ty, _Ty, bool>
{	// functor for operator>
	bool operator()(const _Ty& _Left, const _Ty& _Right) const
	{	// apply operator> to operands
		return (_Left > _Right);
	}
};
 
// TEMPLATE STRUCT less
template<class _Ty> 
struct less : public binary_function<_Ty, _Ty, bool>
{	// functor for operator<
	bool operator()(const _Ty& _Left, const _Ty& _Right) const
	{	// apply operator< to operands
		return (_Left < _Right);
	}
};

这样我们就得到了对于键值数据的最小堆。这里给一个代码示例:(我们修改了最小堆的比较运算符,使得变成基于char比较的最大堆)

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
# include <functional>
# include <algorithm>
using namespace std;

// TEMPLATE STRUCT greater
template<class _Ty>
struct greaterNew: public binary_function<pair<double, char>, pair<double, char>, bool>
{	// functor for operator>
	bool operator()(pair<double, char> p1, pair<double, char> p2) const
	{	// apply operator> to operands		
		return (p1.second < p2.second);
	}
};


void main() {

	pair<double, char> p1;
	p1.first = 0.12;
	p1.second = 'd';

	pair<double, char> p2;
	p2.first = 0.22;
	p2.second = 'c';

	pair<double, char> p3;
	p3.first = 0.11;
	p3.second = 'b';

	vector<pair<double, char>> minKeyHeap;
	minKeyHeap.push_back(p1);
	minKeyHeap.push_back(p2);
	minKeyHeap.push_back(p3);

	cout << "original vector"<<endl;
	for (int i = 0; i < minKeyHeap.size(); i++) {

		cout << minKeyHeap[i].first <<","<< minKeyHeap[i].second << endl;
		
	}

	cout << "default min-heap"<<endl;
	make_heap(minKeyHeap.begin(), minKeyHeap.end(), greater<pair<double, char>>());
	
	for (int i = 0; i < minKeyHeap.size(); i++) {

		cout << minKeyHeap[i].first << "," << minKeyHeap[i].second << endl;

	}

	cout << "design min-heap"<<endl;
	make_heap(minKeyHeap.begin(), minKeyHeap.end(), greaterNew<pair<double, char>>());
	
	for (int i = 0; i < minKeyHeap.size(); i++) {

		cout << minKeyHeap[i].first << "," << minKeyHeap[i].second << endl;

	}

}

输出:

original vector
0.12, d
0.22, c
0.11, b
default min-heap
0.11, b
0.22, c
0.12, d
design min-heap
0.12, d
0.22, c
0.11, b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序猿老甘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值