堆和优先队列的实现
来源:互联网 发布:WebView打开打开软件 编辑:程序博客网 时间:2024/04/27 09:43
// MinHeap.h: interface for the MinHeap class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MINHEAP_H__6BC12E9A_B926_47C2_8ACF_AA4004A0546F__INCLUDED_)
#define AFX_MINHEAP_H__6BC12E9A_B926_47C2_8ACF_AA4004A0546F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
template <class T>
class MinHeap
{
private:
T* heapArray;
int CurrentSize;
int MaxSize;
public:
MinHeap(const int n);
virtual ~MinHeap()
{delete []heapArray;};
void BuildHeap();
bool isLeaf(int pos) const;
int leftchild(int pos) const;
int rightchild(int pos) const;
// Return parent position
int parent(int pos) const;
// 删除给定下标的元素
bool Remove(int pos, T& node);
void SiftDown(int left);
//从position向上开始调整,使序列成为堆
void SiftUp(int position);
BOOL Insert(const T& newNode);
T& RemoveMin();
};
template<class T>
MinHeap<T>::MinHeap(const int n)
{
if(n<=0)
return;
CurrentSize=0;
MaxSize=n;
heapArray=new T[MaxSize];
BuildHeap();
}
template<class T>
void MinHeap<T>::BuildHeap()
{
for (int i=CurrentSize/2-1; i>=0; i--)
SiftDown(i);
}
template<class T>
bool MinHeap<T>::isLeaf(int pos) const
{
return (pos>=CurrentSize/2)&&(pos<CurrentSize);
}
template<class T>
int MinHeap<T>::leftchild(int pos) const
{
return 2*pos+1; //返回左孩子位置
}
template<class T>
int MinHeap<T>::rightchild(int pos) const
{
return 2*pos+2; //返回右孩子位置
}
template<class T>
int MinHeap<T>::parent(int pos) const // 返回父节点位置
{
return (pos-1)/2;
}
template<class T>
void MinHeap<T>::SiftDown(int left)
{
//准备
int i=left; //标识父结点
int j=2*i+1; //标识关键值较小的子结点
T temp=heapArray[i]; //保存父结点
//过筛
while(j<CurrentSize)
{
if((j<CurrentSize-1)&&(heapArray[j]>heapArray[j+1]))
j++; //j指向右子结点
if(temp>heapArray[j])
{
heapArray[i]=heapArray[j];
i=j;
j=2*j+1;
}
else break;
}
heapArray[i]=temp;
}
template<class T>
void MinHeap<T>::SiftUp(int position)
{//从position向上开始调整,使序列成为堆
int temppos=position;
T temp=heapArray[temppos];
while((temppos>0)&&(heapArray[parent(temppos)]>temp))
{
heapArray[temppos]=heapArray[parent(temppos)];
temppos=parent(temppos);
}
heapArray[temppos]=temp;
}
template<class T>
BOOL MinHeap<T>::Insert(const T& newNode)
{//向堆中插入一个结点
if(CurrentSize==MaxSize)
return FALSE;
heapArray[CurrentSize]=newNode;
SiftUp(CurrentSize);
CurrentSize++;
}
template<class T>
T& MinHeap<T>::RemoveMin()
{
if(CurrentSize==0)
{
AfxMessageBox("Can't Delete");
}
else
{
T temp=heapArray[0]; //取堆顶元素
heapArray[0]=heapArray[CurrentSize-1]; //堆末元素上升至堆顶
CurrentSize--;
if(CurrentSize>1)
SiftDown(0); //从堆顶开始筛选
return temp;
}
}
template<class T>
bool MinHeap<T>::Remove(int pos, T& node)
{// 删除给定下标的元素
if((pos<0)||(pos>=CurrentSize))
return false;
T temp=heapArray[pos];
heapArray[pos]=heapArray[--CurrentSize]; //指定元素置于最后
SiftUp(pos); //上升筛
SiftDown(pos); //向下筛
node=temp;
return true;
}
#endif // !defined(AFX_MINHEAP_H__6BC12E9A_B926_47C2_8ACF_AA4004A0546F__INCLUDED_)
- 堆和优先队列的实现
- 堆排和优先队列的实现
- 基于二叉堆实现的优先队列和堆排序
- 堆的实现、堆排序、优先队列
- 二叉堆的实现和详解(优先队列的基础)
- 堆的自行实现和c++优先队列模板
- 优先队列实现堆
- 优先队列(堆实现)
- 基于堆的优先队列的实现
- 堆和堆的应用:堆排序和优先队列
- 堆和优先队列
- 堆和优先队列
- 堆和优先队列
- 堆和优先队列
- 堆和优先队列
- 堆和优先队列
- 堆和优先队列
- 堆和优先队列
- 《Recommended C Style and Coding Standards》学习总结
- 二叉树的递归穿线实现
- 使用struts时,中文问题
- JavaScript 中eval()函数
- css 错误
- 堆和优先队列的实现
- [转载]标准SQL分页
- Microsoft Biztalk 2004 vs IBM WebSphere Business Integration Server Foundation 5.1
- Oracle中分区表的使用(转)
- 穿线二叉树
- 正则表达式集锦
- 阿蒙:我为什么创业?
- 出差太原
- 二叉搜索树的实现