使用vector实现优先队列

本文详细介绍了优先队列的实现方式,并通过实例展示了其在不同场景中的应用。

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

#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

template<class T>
class priority_queue {
public:
	int size() {
		return hvec.size();
	}
	bool empty() {
		return hvec.empty();
	}
	//向上重新建堆 
	void push(const T& nval) {
		hvec.push_back(nval);
		int child = hvec.size()-1;
		int parent = (child-1)/2;
		while(parent >= 0 && hvec[parent] < hvec[child]) {
			swap(hvec[parent], hvec[child]);
			child = parent;
			parent = (child-1)/2;
		}	
	}
	//向下重新建堆,优先级最高的先出列 
	void pop() {
		assert(!hvec.empty());
		swap(hvec[0], hvec[size()-1]);
		hvec.pop_back();
		int root_index = 0;
		int l_index = 2 * root_index +1;
		int r_index = 2 * root_index +2;
		while(l_index < hvec.size()) {
			if(r_index < hvec.size()) {			//有右孩子 
				T tmp = max(hvec[root_index], hvec[l_index], hvec[r_index]);
				if(tmp == hvec[root_index])
					break;
				else if(tmp == hvec[l_index]) {
					swap(hvec[l_index], hvec[root_index]);
					root_index = l_index;	
				}
				else {
					swap(hvec[r_index], hvec[root_index]);
					root_index = r_index;
				}
			}
			//无右孩子 
			else {
				if(hvec[root_index] >= hvec[l_index])
					break;
				else {
					swap(hvec[l_index], hvec[root_index]);
					root_index = l_index;	
				}
			}
			l_index = 2 * root_index +1;
	        r_index = 2 * root_index +2;	
		}
	}
	T top() {
		assert(!hvec.empty());
		return hvec[0];
	} 
private:
	vector<T> hvec;
	void swap(T &a, T &b) {
		T tmp;
		tmp = a;
		a = b;
		b = tmp;
	}
	T max(T &a, T &b, T &c)	{
		T tmp = (a > b) ? a : b;
		return (tmp > c) ? tmp : c;
	}
}; 

int main() {
	priority_queue<int> pq;
	pq.push(21);
	cout << pq.top() << endl;
	pq.push(35);
	cout << pq.top() << endl;
	pq.push(4);
	cout << pq.top() << endl;
	pq.push(22);
	cout << pq.top() << endl;
	pq.push(5);
	cout << pq.top() << endl;
	pq.push(19);
	cout << pq.top() << endl;
	pq.push(27);
	cout << pq.top() << endl;
	pq.push(45);
	cout << pq.top() << endl;
	pq.push(23);
	cout << pq.top() << endl;
	pq.push(42);
	cout << pq.top() << endl;
	while(!pq.empty()) {
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl; 
	return 0;
}

优先队列是一种特殊的队列,可以根据元素的优先级自动排序,最常见的实现方式是使用堆(heap)。以下是一个使用实现优先队列的示例代码: ```c++ #include <iostream> #include <vector> using namespace std; class PriorityQueue { public: PriorityQueue() {} void push(int val) { heap.push_back(val); int idx = heap.size() - 1; while (idx > 0 && heap[idx] > heap[(idx - 1) / 2]) { swap(heap[idx], heap[(idx - 1) / 2]); idx = (idx - 1) / 2; } } void pop() { int lastIdx = heap.size() - 1; swap(heap[0], heap[lastIdx]); heap.pop_back(); int idx = 0; while (2 * idx + 1 < heap.size()) { int maxChildIdx = 2 * idx + 1; if (maxChildIdx + 1 < heap.size() && heap[maxChildIdx + 1] > heap[maxChildIdx]) { maxChildIdx++; } if (heap[idx] < heap[maxChildIdx]) { swap(heap[idx], heap[maxChildIdx]); idx = maxChildIdx; } else { break; } } } int top() { return heap[0]; } bool empty() { return heap.empty(); } private: vector<int> heap; }; ``` 在这个实现中,我们使用一个动态数组来存储队列中的元素,然后通过堆的方式来维护元素的优先级顺序。具体来说,我们使用一个大根堆来实现优先队列,每次插入元素时,我们将其插入到堆的末尾,然后不断将其与其父节点比较,如果比父节点优先级高,则交换位置,直到找到合适的位置。删除元素时,我们将堆顶元素与最后一个元素交换位置,然后弹出最后一个元素,最后不断将堆顶元素与其子节点比较,如果比子节点优先级低,则交换位置,直到找到合适的位置。获取队列首元素时,直接返回堆顶元素即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值