堆树中出现编译错误

compilation error in HeapTree

本文关键字:编译 错误      更新时间:2023-10-16

这是我实现HeapTree 的代码

 #include<iostream>
#include<assert.h>
using namespace std;
template <class Elem>
class HeapTree
{
  public:
    HeapTree(int MaxSize=500);
    HeapTree(const HeapTree<Elem> &OtherTree);
    HeapTree(Elem *Array, int ElemNum, int MaxSize);
    Elem *Sort(void); // Built-in HeapSort Algorithm
    ~HeapTree(void);
    bool Add(const Elem &Item);  // Add the Item to Heap
    Elem Remove(void);           // Remove and return Item from Heap
    inline int GetSize(void);    // Returns the number of nodes in the Heap
protected:
        Elem *Data;//actual data array
        int currentnum;
        const int Max_size;
        void shiftup(int node);
        void shiftdown(int node);
        inline int parent(int node);
 inline  int leftchild(int node);
};
template <class Elem>
inline int HeapTree<Elem>::parent(int node){
assert(node>0);
return (node-1)/2;
}
template <class Elem>
inline int HeapTree<Elem>::leftchild(int node){
 return (node*2)+1;
}
//HeapTree construction
template<class Elem>
HeapTree<Elem>::HeapTree(int maxsize)
:Max_size(maxsize)
{
Data=new Elem[Max_size];
currentnum=0;
}
//HeapTree copy construction function
template<class Elem>
HeapTree<Elem>::HeapTree(const HeapTree<Elem> &other)
        :Max_size(other.Max_size)
{
        Data=new Elem[Max_size];
        int  current=other.currentnum;
         for (int i=0;i<other.currentnum;++i)
                 Data[i]=other.Data[i];

}
template <class Elem>
HeapTree<Elem>::HeapTree(Elem *Array,int ElemNum,int Maxsize)
:Max_size(Maxsize)
{
Data=new Elem[Max_size];
currentnum=ElemNum;
 for (int i=0;i<ElemNum;++i)
         Data[i]=Array[i];
  for (int i=parent(currentnum-1);i>=0;--i)
          shiftdown(i);
}
// Built-in Heap Sort algorithm
template<class Elem>
Elem *HeapTree<Elem>::Sort(void)
{
Elem * newArray=new Elem[currentnum];
for (int i=currentnum-1;i>=0;--i)
{
newArray[i]=Remove();

}
 return newArray;
}
//HeapTree destruction function
template <class Elem>
HeapTree<Elem>::~HeapTree(void)
{
if (Data)
 delete Data;
}
template <class Elem>
bool HeapTree<Elem>::Add(const Elem &item)
{
if (currentnum>=Max_size)
 return false;
Data[currentnum]=item;
shiftup(currentnum++);
return true;
}
//remove function
template <class Elem>
Elem HeapTree<Elem>::Remove(void)
{
assert(currentnum>0);
Elem temp=Data[0];
Data[0]=Data[--currentnum];//replace with last element;
shiftdown(0);
return temp;

}
// GetSize() function
template<class Elem>
inline int HeapTree<Elem>::GetSize(void)
{
return currentnum;
}
//shift up
template<class Elem>
void HeapTree<Elem>::shiftup(int node)
{
int current=node;
int Parent=parent(current);
Elem item=Data[current];
while(current>0)
{
if(Data[Parent]<item)
{
Data[current]=Data[Parent];
current=Parent;
Parent=parent(current);
}
else
break;
}
Data[current]=item;
}
// ShiftDown() function
template <class Elem>
void HeapTree<Elem>::shiftdown(int node)
{
int current=node,
child=leftchild(current);
Elem item=Data[current];
while(child<currentnum)
{
if (child<(currentnum-1))
if(Data[child]<Data[child+1])
++child;
if(item<Data[child]){
Data[current]=Data[child];
current=child;
child=leftchild(current);
}
else
break;
}
Data[current]=item;
}
int main(){
        int a[]={12,31,10,6,4,7,9,8,0,11};
        int n=sizeof(a)/sizeof(a[0]);
        HeapTree<int>hp(a,n,n);
        hp.Sort();
        cout<<hp.Remove()<<" ";
return 0;
}

但是当我运行它时,它写了compiltion错误,我认为,在main()地方,我没有正确创建HeapTree的实例,请帮助我确定这个代码

有什么问题

HeapTree::Sort调用HeapTree::Remove,直到对堆进行排序。在最后一次调用Remove之后,currentnumSort值是0,所以对Remove的另一次调用无法断言您指定的条件。(currentnum>0

注意:我认为你不应该通过对树进行排序来清空它!

缺少缩进使代码有点难以理解,但看起来Sort方法清空了树,并返回了一个新分配的所有元素的数组。

当您稍后到达main中的Remove时,树已经为空,因此您的断言失败。

当你对树进行排序时,你真的想清空它吗?