堆树中出现编译错误
compilation error in HeapTree
这是我实现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
之后,currentnum
的Sort
值是0
,所以对Remove
的另一次调用无法断言您指定的条件。(currentnum>0
)
注意:我认为你不应该通过对树进行排序来清空它!
缺少缩进使代码有点难以理解,但看起来Sort
方法清空了树,并返回了一个新分配的所有元素的数组。
当您稍后到达main
中的Remove
时,树已经为空,因此您的断言失败。
当你对树进行排序时,你真的想清空它吗?
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?