優先キュー [C++]

優先キュー

プライオリティキュー(priority_queue)もキューの一種であり、priority_queueのインターフェースはqueueと同じです。したがって、両方の使用構文は同じです。優先キュー (優先キュー) の基本的な実装原則を直接見てみましょう。

デフォルトでは、priority_queue は大きなヒープです。

priority_queue が使用するもの

//用vector作为底层容器,内部构造大堆结构。
priority_queue<int, vector<int>, less<int>> q1;
//用vector作为底层容器,内部构造小堆结构。
priority_queue<int, vector<int>, greater<int>> q2;
//不指定底层容器和内部需要构造的堆结构。
priority_queue<int> q3;
#include <iostream>
#include <functional>
#include <queue>
using namespace std;
int main()
{
    
    
	priority_queue<int> q;
	q.push(3);
	q.push(6);
	q.push(0);
	q.push(2);
	q.push(9);
	q.push(8);
	q.push(1);
	while (!q.empty())
	{
    
    
		cout << q.top() << " ";
		q.pop();
	}
	cout << endl; //9 8 6 3 2 1 0
	return 0;
}

priority_queue のアナログ実装

priority_queue の最下層は実際にはヒープであるため、priority_queue をシミュレートする前に、下方調整アルゴリズムと上方調整アルゴリズムを理解する必要があります。(例として使用する次の 2 つのアルゴリズム)

アルゴリズムを上に調整

上方調整アルゴリズムの前提:
小さなヒープに調整したい場合、ルート ノードの左右のサブツリーは両方とも小さなヒープでなければなりません。
 大きなヒープに合わせて調整したい場合は、ルート ノードの左右のサブツリーが両方とも大きなヒープである必要があります。

ヒープにデータを挿入するには、上方調整アルゴリズムを使用して、
まず要素をヒープの最後、つまり最後の子の後に挿入し、挿入されたノードの位置から親ノードと比較する必要があります(例として大きなヒープを取り上げます)、ターゲット ノードの値が親ノードの値より大きい場合、ターゲット ノードとその親ノードの位置が交換され、元のターゲット ノードの親ノードがが新たなターゲットノードとみなされ、ルートノードが終了するまで調整が続けられます。

この図では、小さなヒープが例として取り上げられています。
ここに画像の説明を挿入

下方調整アルゴリズム

下方調整アルゴリズムの前提:
小さなヒープに調整したい場合、ルート ノードの左右のサブツリーは両方とも小さなヒープでなければなりません。
 大きなヒープに調整したい場合は、ルート ノードの左右のサブツリーが両方とも大きなヒープである必要があります。ルート ノードから始めて、

左右の子の値が大きいノードを選択し、そのノードの値を比較します。値が大きい方のノードの値を親ノードの値と置き換え、値が大きい方のノードの値が親ノードの値より小さい場合、2 つの位置を交換し、値が大きい方の子ノードをノードとして使用します。親ノードを下方向に調整し続け、葉ノードの端まで調整します。

この図では、小さなヒープが例として取り上げられています。
ここに画像の説明を挿入

押す

	void push(const T & x)
		{
    
    
			_con.push_back(x);
			AdjustUp(_con.size() - 1);
		}

ポップ

	void pop()
		{
    
    
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			AdjustDown(0);
		}

const T& top()
		{
    
    
			return _con[0];
		}

サイズ

bool size()
		{
    
    
			return _con.size();
		}	

空の

bool empty()
		{
    
    
			return _con.empty();
		}

ファンクタ

using namespace std;
//仿函数 /函数对象
template <class T>
class Less
{
    
    
public:
	bool operator() (const T& x, const T& y)
	{
    
    
		return x < y;
	}
};

int main()
{
    
    
	Less<int>  lessfunc;
	bool result = lessfunc(6, 2);//仿函数
	//bool result = lessfunc.operator()(6, 2);
	cout <<boolalpha <<result << endl;
	return 0;
}

完全なコード

#pragma once 
#include<vector>
#include<functional>
using namespace std;

//仿函数 /函数对象
template <class T>
class Less
{
    
    
public:
	bool operator() (const T& x, const T& y)
	{
    
    
		return x < y;
	}
};
template <class T>
class Greater
{
    
    
public:
	bool operator() (const T& x, const T& y)
	{
    
    
		return x > y;
	}
};

namespace cxq
{
    
    
	template<class T, class Container = vector<T>, class Compare = Less<T> >
	class priority_queue
	{
    
    
	private:
		void AdjustDown(int parent)//从根节点开始
		{
    
    
			Compare com;//仿函数
			int child = parent * 2 + 1;
			while (child < _con.size())
			{
    
    
				//假设默认左孩子大于右孩子
			   // if (child + 1 < _con.size() && _con[child + 1] > _con[child])//右孩子要存在,防止越界
				if (child + 1 < _con.size() && com(_con[child + 1], _con[child]))
				{
    
    
					child++;
				}
				//if (_con[child] > _con[parent])
				if (com(_con[parent], _con[child]))
				{
    
    
					swap(_con[child], _con[parent]);
					int  parent = child;
					child = parent * 2 + 1;
				}
				else
				{
    
    
					break;
				}
			}

		}
		void AdjustUp(int child)
		{
    
    
			Compare com;//仿函数
			int parent = (child - 1) / 2;
			//大堆

			while (child > 0)
			{
    
    
				//if (_con[child] > _con[parent])
				if (com(_con[parent], _con[child]))
				{
    
    
					swap(_con[child], _con[parent]);
					int child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
    
    
					break;
				}
			}
		}
	public:
		//默认构造
		priority_queue()
		{
    
    }
		template<class InputIterator>
		//构造函数
		priority_queue(InputIterator first, InputIterator last)
		{
    
    
			while (first != last)
			{
    
    
				_con.push_back(*first);
				first++;
			}
			//建堆
			//最后一个非叶子节点
			for (size_t i = (_con.size() - 1 - 1) / 2; i >= 0; i++)
			{
    
    
				AdjustDown(i);
			}
		}
		void pop()
		{
    
    
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			AdjustDown(0);
		}
		void push(const T& x)
		{
    
    
			_con.push_back(x);
			AdjustUp(_con.size() - 1);
		}
		const T& top()
		{
    
    
			return _con[0];
		}
		bool empty()
		{
    
    
			return _con.empty();
		}
		bool size()
		{
    
    
			return _con.size();
		}
	private:
		Container _con;

	};
	void test_priority_queue1()
	{
    
    
		//默认是大堆--less
		//priority_queue<int> pq;
		priority_queue<int, vector<int>, Greater<int> > pq;//小堆
		pq.push(3);
		pq.push(5);
		pq.push(1);
		pq.push(4);
		while (!pq.empty())
		{
    
    
			cout << pq.top() << " ";
			pq.pop();
		}
		cout << endl;
	}
	class Date
	{
    
    
	public:
		Date(int year = 1900, int month = 1, int day = 1)
			: _year(year)
			, _month(month)
			, _day(day)
		{
    
    }

		bool operator<(const Date& d)const
		{
    
    
			return (_year < d._year) ||
				(_year == d._year && _month < d._month) ||
				(_year == d._year && _month == d._month && _day < d._day);
		}

		bool operator>(const Date& d)const
		{
    
    
			return (_year > d._year) ||
				(_year == d._year && _month > d._month) ||
				(_year == d._year && _month == d._month && _day > d._day);
		}

		friend ostream& operator<<(ostream& _cout, const Date& d);//声明
	private:
		int _year;
		int _month;
		int _day;
	};
	
	ostream& operator<<(ostream& _cout, const Date& d)
	{
    
    
		_cout << d._year << "-" << d._month << "-" << d._day;
		return _cout;
	}
	struct  LessPDate
	{
    
    
		//仿函数
		bool operator() ( const Date * p1 , const Date* p2)
		{
    
    
			return *p1 < *p2;
		}
	};
	void test_priority_queue2()
	{
    
    
		//priority_queue<Date> pq;
		//pq.push(Date(2023, 7, 20));
		//pq.push(Date(2023, 6, 20));
		//pq.push(Date(2023, 8, 20));
		//while (!pq.empty())
		//{
    
    
		//	cout << pq.top() << " ";
		//	pq.pop();
		//}
		//cout << endl;
		priority_queue<Date*, vector<Date*>, LessPDate> pq;
		pq.push(new Date(2023, 7, 20));
		pq.push(new Date(2023, 6, 20));
		pq.push(new Date(2023, 8, 20));
		while (!pq.empty())
		{
    
    
			cout << *pq.top() << " ";
			pq.pop();
		}
		cout << endl;
	}
	
}

おすすめ

転載: blog.csdn.net/qq_73478334/article/details/132332960