//处理优先队列的头文件
#ifndef MAXHEAP_H
#define MAXHEAP_H
template<typename T>
class MaxHeap{
private:
T* heapArray;
int CurrSize;//当前堆中的元素的个数,从1开始
int MaxSize;//对所能容纳的元素数目
public:
//因为是完全二叉树,所以用数组来存储
MaxHeap();
~MaxHeap();
void Print()const;
void BulidHeap();
bool IsLeaf(int pos)const;
int LeftChild(int pos)const;
int RightChild(int pos)const;
int Parent(int pos)const;
//先不考虑
//void Remove(int pos);//删除给定下标的元素
void Insert(const T& newNode);//插入新元素
void MoveMax();//把堆顶的元素移到堆的尾部
void RemoveMax();//删除堆尾部的最大元素
private:
void Ready();//此函数是准备初始化堆,并给变量赋值
void SiftDown(int pos);//筛选法函数,left表示开始处理的数组下标
//该函数的应用函数是Insert()函数
void SiftUp(int pos);//从position开始向上调整,使序列成为堆
};
template<typename T>
MaxHeap<T>::MaxHeap()
{
this->heapArray = NULL;
this->CurrSize = 0;
this->MaxSize = 0;
}
template<typename T>
MaxHeap<T>::~MaxHeap()
{
if (this->heapArray != NULL)
delete heapArray;
heapArray = NULL;
}
template<typename T>
void MaxHeap<T>::Ready()
{
cout << "输入堆需要存储的最大长度:";
cin >> this->MaxSize;
this->heapArray = new T[this->MaxSize];
if (this->heapArray == NULL)
{
cout << "未成功生成空间!" << endl;
exit(true);
}
cout << "输入你要初始化的堆的长度:";
cin >> this->CurrSize;
T data;
for (int i = 0; i < this->CurrSize; i++)
{
cin >> data;
heapArray[i] = data;
}
}
template<typename T>
void MaxHeap<T>::BulidHeap()
{
this->Ready();
for (int i = this->CurrSize / 2 - 1; i >= 0; i--)
{
this->SiftDown(i);
}
}
template<typename T>
bool MaxHeap<T>::IsLeaf(int pos)const
{
//因为存储结构是数组,所以当满足pos * 2 + 1 < this->CurrSize
//即说明该节点不是叶子节点
if (pos * 2 + 1 < this->CurrSize)
{
return false;
}
return true;
}
template<typename T>
int MaxHeap<T>::LeftChild(int pos)const
{
if (this->IsLeaf(pos))
{
cout << "该节点是叶子节点,不存在孩子" << endl;
exit(true);
}
//不是叶子节点,就一定存在左孩子,但是不一定存在右孩子
return pos * 2 + 1;
}
template<typename T>
int MaxHeap<T>::RightChild(int pos)const
{
if (this->IsLeaf(pos))
{
cout << "该节点是叶子节点,不存在孩子" << endl;
exit(true);
}
if (pos * 2 + 2 >= this->CurrSize)
{
cout << "该节点不存在右孩子节点" << endl;
exit(true);
}
return pos * 2 + 2;
}
template<typename T>
int MaxHeap<T>::Parent(int pos)const
{
//分奇偶情况
if (pos % 2 == 0)
{
return (pos - 2) / 2;
}
else
{
return (pos - 1) / 2;
}
}
template<typename T>
void MaxHeap<T>::SiftDown(int pos)
{
//筛选法的规则是从下开始向上寻找,找出不符合规则的,改变他
int parent = pos;//标识父节点
int left = pos * 2 + 1;//该节点的左孩子
T val = this->heapArray[pos];
while (left < this->CurrSize)
{
if (left < this->CurrSize - 1 && this->heapArray[left] < this->heapArray[left + 1])
{
//如果该节点有右孩子,且他的右孩子的关键吗大于左孩子的关键吗,执行该操作
left++;
}
if (val < this->heapArray[left])
{
this->heapArray[parent] = this->heapArray[left];
//改变此时的parent,将他指向下一个父节点
parent = left;
//继续向下操作
left = left * 2 + 1;
}
else
{
break;
}
}
this->heapArray[parent] = val;
}
template<typename T>
void MaxHeap<T>::Insert(const T& newNode)
{
//插入行的元素,将钙元素插入数组的最后位置
if (this->CurrSize >= this->MaxSize)
{
cout << "堆空间已存满" << endl;
return;
}
this->heapArray[this->CurrSize++] = newNode;
//开始从该节点调整,是数组满足最大堆
this->SiftUp(this->CurrSize - 1);
}
template<typename T>
void MaxHeap<T>::SiftUp(int pos)
{
int position = pos;//记录该节点的位置
int parent = this->Parent(pos);
T val = this->heapArray[position];
while (parent >= 0)
{
//当时不是根节点的时候,一直持续的比较
if (this->heapArray[parent] < val)
{
//当父节点的值要小于当前节点的值的时候
//不满足最大堆的概念,所以要交换彼此的值
this->heapArray[position] = this->heapArray[parent];
this->heapArray[parent] = val;
position = parent;
parent = this->Parent(parent);
}
else
{
break;
}
}
}
template<typename T>
void MaxHeap<T>::MoveMax()
{
T val = this->heapArray[0];
this->heapArray[0] = this->heapArray[this->CurrSize - 1];
this->heapArray[this->CurrSize - 1] = val;
}
template<typename T>
void MaxHeap<T>::RemoveMax()
{
if (this->CurrSize == 0)
{
cout << "此时堆为空" << endl;
exit(true);
}
else
{
this->MoveMax();
this->CurrSize--;
if (this->CurrSize > 1)
{
//因为在引动堆顶元素时,破坏了对的规则,所以从堆顶开始对堆进行规则化
this->SiftDown(0);
}
}
}
template<typename T>
void MaxHeap<T>::Print()const
{
for (int i = 0; i < this->CurrSize; i++)
{
cout << this->heapArray[i] << " ";
}
cout << endl;
}
#endif
//主函数
// MaxHeap.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
MaxHeap<int>heap;
heap.BulidHeap();
heap.Print();
int value;
cout << "输入你要插入的值:";
cin >> value;
heap.Insert(value);
heap.Print();
return 0;
}