最小堆優先隊列C++實現

在建立最小堆或最大堆時。最主要的就是理解。SiftDown和SiftUp算法的實現問題。其實我覺得自己在畫一棵樹。先比較左右再比較父點之間。是最大堆往上。最小堆往下。就很能理解了,

下面程序是最小堆。
#include <iostream>
using namespace std;

template<class Elem>
class MinHeap
{
private:
 Elem* 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,Elem& node);
 void SiftDown(int left);
 void SiftUp(int position);
 bool Insert(const Elem& newNode);
 Elem& RemoveMin();
 bool print() const;
};
template<class Elem>
MinHeap<Elem>::MinHeap(const int n)
{
 if(n<=0)
  return ;
 CurrentSize=0;
 MaxSize=n;
 heapArray=new Elem[MaxSize];
 BuildHeap();
}

template<class Elem>
void MinHeap<Elem>::BuildHeap()
{
 for(int i=CurrentSize/2-1;i>=0;i--)
  SiftDown(i);
}

template<class Elem>
bool MinHeap<Elem>::isLeaf(int pos) const
{
 return (pos>=CurrentSize/2) && (pos<CurrentSize);
}

template<class Elem>
int MinHeap<Elem>::leftChild(int pos) const
{
 return 2*pos+1;
}

template<class Elem>
int MinHeap<Elem>::rightChild(int pos) const
{
 return 2*pos+2;
}

template<class Elem>
int MinHeap<Elem>::parent(int pos) const
{
 return (pos-1)/2;
}
//向下篩選。
template<class Elem>
void MinHeap<Elem>::SiftDown(int left)
{
 int i=left;          。                  //標記父結點
 int j=2*i+1;
 Elem temp=heapArray[i];
 
 while(j<CurrentSize)
 {
  if((j<CurrentSize-1) && (heapArray[j]>heapArray[i]))
     j++;                          //指向右子結點
     if(temp>heapArray[j])
   {
    heapArray[i]=heapArray[j];
    i=j;
    j=2*j+1;
   }
     else
     break;
  }
 heapArray[i]=temp;
}
//向上篩選
template<class Elem>
void MinHeap<Elem>::SiftUp(int position)
{
 int temppos=position;
 Elem temp=heapArray[temppos];
 while((temppos>0) && (heapArray[parent(temppos)]>temp))
 {
  heapArray[temppos]=heapArray[parent(temppos)];
  temppos=parent(temppos);
 }
 heapArray[temppos]=temp;
}

template<class Elem>
bool MinHeap<Elem>::Insert(const Elem& newNode)
{
 if(CurrentSize==MaxSize)
  return false;
 heapArray[CurrentSize]=newNode;
 SiftUp(CurrentSize);
 CurrentSize++;
 return true;
}

template<class Elem>
Elem& MinHeap<Elem>::RemoveMin()
{
 if(CurrentSize==0)
 {
    cout<<"Can't Delete ";
 }
 else
 {
  Elem temp=heapArray[0];
  heapArray[0]=heapArray[CurrentSize-1];
  CurrentSize--;
  if(CurrentSize>1)
   SiftDown(0);
  return temp;
 }
}
  
template<class Elem>
bool MinHeap<Elem>::Remove(int pos,Elem& node)
{
 if((pos<0) || (pos>CurrentSize))
  return false;
 Elem temp=heapArray[pos];
 heapArray[pos]=heapArray[--CurrentSize];
 SiftUp(pos);
 SiftDown(pos);
 node=temp;
 return true;
}

template<class Elem>
bool MinHeap<Elem>::print() const
{
  if(CurrentSize<0)
   return false;
  for(int i=0;i<CurrentSize;i++)
   cout<<heapArray[i]<<" ";
  return true;
}

int main()
{
 MinHeap<char> A(20);
 char item;
 int pos;
 cout<<"Enter 20 number:"<<endl;
 for(int i=0;i<20;i++)
 {
  cout<<"the"<<i<<"NO: ";
  cin>>item;
  A.Insert(item);
 }
 cout<<"/nPrint all the number: ";
 A.print();
 A.RemoveMin();
 cout<<"/nAfter delete the Min number:";
 A.print();
 cout<<"/nEnter the positon you want:";
 cin>>pos;
 A.Remove(pos,item);
 cout<<"/nThe data is:"<<item<<endl;
 return 0;
}

//程序示例:
/*
Enter 20 number:
the0NO: a
the1NO: q
the2NO: z
the3NO: w
the4NO: s
the5NO: x
the6NO: e
the7NO: d
the8NO: c
the9NO: r
the10NO: f
the11NO: v
the12NO: t
the13NO: g
the14NO: b
the15NO: y
the16NO: h
the17NO: n
the18NO: u
the19NO: j

Print all the number: a c b d f t e h n j r z v x g y w q u s
After delete the Min number:c f b d r t e h n j s z v x g y w q u
Enter the positon you want:13

The data is:x
*/

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章