基于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

Guess you like

Origin blog.csdn.net/aliexken/article/details/109327527