ian-ru 2017-04-12 04:10
浏览 559

求大佬解答下我代码的问题,左倾堆(leftistheap),编译错误

提示
错误 C2065 “heap”: 未声明的标识符

#include "common.h"

template
class leftistheap
{
public:
leftistheap():root{nullptr}
{}

leftistheap(const leftistheap &rhs);


~leftistheap();

leftistheap &operator=(const leftistheap &rhs);

bool isempty() const;
const comparable &findmin() const;

void insert(const comparable &x);
void insert(comparable &&x);
void deletemin();
void deletemin(comparable &minitem);
void makeempty();
void merge(leftistheap &rhs);

private:
struct leftistnode
{
comparable element;
leftistnode *left;
leftistnode *right;
int npl;

    leftistnode(const comparable &e, leftistnode *lt = nullptr, leftistnode *rt = nullptr, int np = 0) :
        element{ e }, left{ lt }, right{ rt }, npl{ np } {}
    leftistnode(const comparable &&e, leftistnode *lt = nullptr, leftistnode *rt = nullptr, int np = 0) :
        element{ std::move(e) }, left{ lt }, right{ rt }, npl{ np } {}


};

leftistnode *root;
leftistnode *merge(leftistnode *h1, leftistnode *h2);
leftistnode *merge1(leftistnode *h1, leftistnode *h2);

void swapchildren(leftistnode *t);
void reclaimmemory(leftistnode *t);
leftistnode *clone(leftistnode *t) const;

};

template
void leftistheap::merge(leftistheap &rhs)
{
if (this == &rhs)
{
return;
}
root = merge(root, rhs.root);
rhs.root = nullptr;
}

template
leftistheap::leftistnode * leftistheap::merge(leftistnode *h1,leftistnode *h2)
{
if (h1 == nullptr)
return h2;
if (h2 == nullptr)
return h1;
if (h1->element < h2->element)
return merge1(h1, h2);
else
return merge1(h2, h1);
}

template
leftistheap::leftistnode * leftistheap::merge1(leftistnode *h1, leftistnode *h2)
{
if (h1->left == nullptr)
{
h1->left = h2;
}
else
{
h1->right = merge(h1->right, h2);
if (h1->left->npl < h1->right->npl)
swapchildren(h1);
h1->npl = h1->right->npl + 1;
}
return h1;
}

template
void leftistheap::insert(const comparable &x)
{
root = merge(new leftistnode{ x }, root);
}

template
void leftistheap::insert(comparable &&x)
{
root = merge(new leftistnode{ std::move(x) }, root);
}

template
void leftistheap::deletemin()
{
if (isempty())
cout << "is empty!!!" << endl;
leftistnode *oldroot = root;
root = merge(root->left, root->right);
delete oldroot;
}

template
void leftistheap::deletemin(comparable &minitem)
{
minitem = findmin();
deletemin();
}

template
leftistheap::leftistheap(const leftistheap &rhs)
{
clone(rhs.root);
}

template
leftistheap::~leftistheap()
{
reclaimmemory(root);
}

template
leftistheap::leftistnode leftistheap::operator=(const leftistheap &rhs)
{
clone(rhs.root);
}

template
bool leftistheap::isempty() const
{
if(root==nullptr)
return true
}

template
void leftistheap::swapchildren(leftistnode *t)
{
std::swap(t->left, t->right);
}

template
void leftistheap::makeempty()
{
reclaimmemory(root);
}

template
void leftistheap::reclaimmemory(leftistnode *t)
{
if (t != nullptr)
{
reclaimmemory(t->left);
reclaimmemory(t->right);
delete t;
}
t = nullptr;
}

template
leftistheap::leftistnode* leftistheap::clone(leftistnode *t) const
{
if (t == nullptr)
return nullptr;
else
{
return new leftistnode(t->element, clone(t->left), clone(t->right),t->npl);
}
}

template
const comparable & leftistheap::findmin() const
{
return root->element;
}

#endif // !LEFISTHEAP_H

#include "leftistheap.h"

int main()
{

leftistheap<int> heap;
int n;
while (cin >> n)
{
    heap.insert(n);
    if (cin.get() == '\n')
        break;
    if (cin.peek() == '\n')
        break;
}
system("pause");
return 0;

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
    • ¥15 onvif+openssl,vs2022编译openssl64
    • ¥15 iOS 自定义输入法-第三方输入法