最大堆实现的优先队列

//声明文件

class CPriorityQueueByMaxHeap{
private:
	int* pArr;
	int nHeapSize;
private:
	void MaxHeapify(int root);
public:
	CPriorityQueueByMaxHeap();
	CPriorityQueueByMaxHeap(int* arr, int len);
	~CPriorityQueueByMaxHeap();

	void AllElements();
	int DeQueue();
	void IncreaseKey(int i, int key);
	void EnQueue(int key);
};

//实现文件

CPriorityQueueByMaxHeap::CPriorityQueueByMaxHeap()
{
	pArr = new int[14]; pArr[0] = 13;
	for (int i = 1; i < 14; i++)		pArr[i] = 0;
	nHeapSize = 0;
}
CPriorityQueueByMaxHeap::CPriorityQueueByMaxHeap(int* arr, int len)
{
	pArr = NULL; nHeapSize = 0;
	if (arr != NULL && len != 0){
		pArr = new int[len + 1 + 3];
		pArr[0] = len + 3;
		for (int i = 0; i < len; i++)		pArr[i + 1] = arr[i];
		nHeapSize = len;
		for (int i = nHeapSize / 2; i >= 1; i--)		MaxHeapify(i);
	}
}
CPriorityQueueByMaxHeap::~CPriorityQueueByMaxHeap()
{ 
	if (pArr != NULL)	delete pArr; 
}
//////////////////////////////////////////////////////////////////////////////////
void CPriorityQueueByMaxHeap::IncreaseKey(int i, int key)
{
	if (key < pArr[i]){
		cout << "new key is smaller than current key !" << endl;
		return;
	}
	pArr[i] = key;
	while (i > 1 && pArr[i / 2] < pArr[i]){
		int tmp = pArr[i / 2]; pArr[i / 2] = pArr[i]; pArr[i] = tmp;
		i = i / 2;
	}
}
void CPriorityQueueByMaxHeap::MaxHeapify(int root)
{
	int left = root * 2;
	int right = root * 2 + 1;
	int largest = root;
	if (left <= nHeapSize && pArr[left] > pArr[root])		largest = left;
	if (right <= nHeapSize && pArr[right] > pArr[largest])	largest = right;
	if (largest != root){//root比left和right的值都小,甚至比叶子结点的值还小
		int tmp = pArr[root]; pArr[root] = pArr[largest]; pArr[largest] = tmp;
		MaxHeapify(largest);
	}
}


void CPriorityQueueByMaxHeap::AllElements()
{
	for (int i = 1; i <= nHeapSize; i++){
		cout << setw(3) << pArr[i];
	}
	cout << "(" << nHeapSize << "/" << pArr[0] << ")" << endl;
}
int CPriorityQueueByMaxHeap::DeQueue()
{
	if (nHeapSize < 1){
		cout << "UnderFlow !" << endl;
		return 0x80000000;
	}
	int nMax = pArr[1]; pArr[1] = pArr[nHeapSize];
	nHeapSize--;
	MaxHeapify(1);
	return nMax;
}

void CPriorityQueueByMaxHeap::EnQueue(int key)
{
	if (nHeapSize >= pArr[0]){
		cout << "OverFlow !" << endl;
		return;
	}
	nHeapSize++;
	pArr[nHeapSize] = 0x80000000;
	IncreaseKey(nHeapSize, key);
}

//测试客户端

//通过最大堆实现的优先队列
void PriorityQueueByMaxHeapTest1()
{
	int Array[] = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1 };
	int Length = sizeof(Array) / sizeof(int);

	cout << "BulidHeap : " << endl;
	CPriorityQueueByMaxHeap PriQue(Array, Length);
	PriQue.AllElements();


	cout << "IncreateKey : " << endl;
	PriQue.IncreaseKey(9, 15);
	PriQue.AllElements();

	int key = 0x80000000;
	key = PriQue.DeQueue();
	cout << "DeQueue : " << key << endl;
	PriQue.AllElements();

	for (int i = 0; i < 5; i++){
		key = 16 + i;
		PriQue.EnQueue(key);
		cout << "EnQueue : " << key << endl;
		PriQue.AllElements();
	}
	
	for (int i = 0; i < 15; i++){
		key = PriQue.DeQueue();
		cout << "DeQueue : " << key << endl;
		PriQue.AllElements();
	}

}

void PriorityQueueByMaxHeapTest2()
{
	int Array[] = { 16, 14, 10, 8, 7, 9, 3, 2, 4, 1 };
	int Length = sizeof(Array) / sizeof(int);

	cout << "EnQueue>>>>>>>>>>>>>>>>>>>>>>>>>." << endl;
	CPriorityQueueByMaxHeap PriQue;
	for (int i = 0; i < Length; i++){
		PriQue.EnQueue(Array[i]);
		PriQue.AllElements();
	}
	for (int i = 0; i < 5; i++){
		PriQue.EnQueue(i*10);
		PriQue.AllElements();
	}
	cout << "DnQueue>>>>>>>>>>>>>>>>>>>>>>>>>." << endl;
	for (int i = 0; i < 15; i++){
		PriQue.DeQueue();
		PriQue.AllElements();
	}
}

猜你喜欢

转载自blog.csdn.net/KnightOnHourse/article/details/80035697