二叉搜索树析构函数 [英] Binary Search Tree Destructor

查看:213
本文介绍了二叉搜索树析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中实现我自己的BST以处理这种结构的经验。

Working on implementing my own BST in C++ for the experience of dealing with such structures.

我在实现析构函数时遇到麻烦。我在我的研究中发现,一个不能真正有一个递归析构函数(由于一个标志不允许析构函数被调用后在同一个对象上被调用),但我不确定任何

I've had trouble implementing a destructor. I found in my studies, that one can't really have a recursive destructor (due to a flag that doesn't allow the destructor to be called on the same object after having been called), but I'm not really sure of any other way to successfully clean up all the nodes in the tree.

为了补偿,我已经创建了一个辅助函数,但是这会抛出一个未解决的外部错误delete n'线。任何提示?

To compensate, I've created a helper function - however this throws an unresolved external error on the 'delete n' line. Any tips?

代码:

void BinSearchTree::Clear(tNode* n)
{
    if (n->left != NULL)
        Clear(n->left);
    if (n->right != NULL)
        Clear(n->right);
    delete n;
    n = NULL;
    size--;
}


推荐答案

;

在C ++中删除树的典型方法可能是这样:

A typical way to delete a tree in C++ might be something like this:

BinSearchTree::~BinSearchTree()
{
   delete _rootNode;  // will recursively delete all nodes below it as well
}

tNode::~tNode()
{
   delete left;
   delete right;
}

关于未解决的外部错误 - 是尝试编译时抛出的错误/链接程序?如果是这样,可能是因为tNode类的代码(特别是tNode析构函数,如果你声明了一个)不存在或者没有被编译到你的项目中。

Regarding the unresolved external error -- is that error thrown when you try to compile/link the program? If so, it's probably because the code for the tNode class (and in particular the tNode destructor, if you declared one) doesn't exist or isn't getting compiled into your project.

这篇关于二叉搜索树析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆