最小优先队列 解决TopK问题

#include <vector>
#include <algorithm>
#include <limits>
#include <iostream>
using namespace std;

 优先队列
#define  MAX_HEAP    300
class PriorityHeap
{
public:
	PriorityHeap();
	~PriorityHeap();
	
	bool  empty();
	int   size();
	int   top();
	void  push(int nValue);
	int   pop();
private:
	int				GetParentIndex(int nIndex);
	const int&		GetParentValue(int nIndex);
	int             GetLeftIndex(int nIndex);
	int             GetRightIndex(int nIndex);
	void            AdjustHeap(int nIndex);               // 调整根节点为nIndex的子树  前提是left(nIndex) right(nIndex)都是大顶堆

	int  heap[MAX_HEAP];
	int  heapSize;
};

PriorityHeap::PriorityHeap()
{
	memset(heap, 0, sizeof(heap));
	heapSize = 0;
}

PriorityHeap::~PriorityHeap()
{

}

bool PriorityHeap::empty()
{
	return heapSize > 0 ? false : true;
}

const int& PriorityHeap::GetParentValue(int nIndex)
{
	if (nIndex < 0 || nIndex >= heapSize)
	{
		return 0;
	}

	int nParentIndex = GetParentIndex(nIndex);
	return heap[nParentIndex];
}

int PriorityHeap::GetParentIndex(int nIndex)
{
	//return nIndex/2 - 1 > 0 ? nIndex/2 - 1 : 0;
	return (nIndex+1)/2 - 1;
}

int PriorityHeap::GetLeftIndex(int nIndex)
{
	return 2*nIndex + 1;
}

int PriorityHeap::GetRightIndex(int nIndex)
{
	return 2*nIndex + 2;
}

void PriorityHeap::push(int nValue)
{
	heapSize = heapSize + 1;
	int heapIndex = heapSize -1;

	heap[heapIndex] = nValue;
	int nParentIndex = GetParentIndex(heapIndex);
	while(heapIndex > 0 && heap[nParentIndex] > heap[heapIndex])
	{
		int nTemp = heap[heapIndex];
		heap[heapIndex] = heap[nParentIndex];
		heap[nParentIndex] = nTemp;

		heapIndex = nParentIndex;
		nParentIndex = GetParentIndex(heapIndex);
	}
}

int PriorityHeap::top()
{
	return heap[0];
}

int PriorityHeap::size()
{
	return heapSize;
}

int PriorityHeap::pop()
{
	int nRoot = heap[0];
	heapSize = heapSize - 1;
	heap[0] = heap[heapSize];
	AdjustHeap(0);
	return nRoot;
}

void PriorityHeap::AdjustHeap(int nIndex)
{
	int largestIndex = nIndex;
	int leftIndex = GetLeftIndex(nIndex);
	int rightIndex = GetRightIndex(nIndex);

	if (leftIndex < heapSize && heap[leftIndex] < heap[largestIndex])
	{
		largestIndex = leftIndex;
	}

	if (rightIndex < heapSize && heap[rightIndex] < heap[largestIndex])
	{
		largestIndex = rightIndex;
	}

	if (largestIndex != nIndex)
	{
		int nTemp = heap[largestIndex];
		heap[largestIndex] = heap[nIndex];
		heap[nIndex] = nTemp;
		AdjustHeap(largestIndex);
	}
}

void FindTopK()
{
	int nValue;
	int k;
	PriorityHeap heap;

	cin >> k;
	while (cin >> nValue)
	{	
		if (heap.size() < k)
		{
			heap.push(nValue);
		}
		else if(nValue > heap.top())
		{
			heap.pop();
			heap.push(nValue);
		}
	}
	while(!heap.empty())
	{
		cout << heap.pop() << " " << endl;
	}
	system("pause");
}

int main()
{
	PriorityHeap  Heap;
	int nValue;
	while (cin >> nValue)
	{
		Heap.push(nValue);
	}

	while(!Heap.empty())
	{
		cout << Heap.pop() << " " << endl;
	}

	FindTopK();
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值