我尝试创建这个Detor并得到一个“ Stack Overflow”,我知道为什么,但是我想要它的工作...
#include <iostream>
#include "printTreeToFile.h"
#include "BSNode.h"
#define _BS BSNode::
#define _BSNode _BS BSNode
_BS ~BSNode()
{
Del(this);
}
void _BS Del(BSNode *x,int y)
{
if (x->isLeaf())
{
delete x;
return;
}
if (x->_Right != NULL)
Del(x->_Right,y += 1);
if (x->_Left != NULL)
Del(x->_Left,y += 1);
if (y != 1)
{
delete x;
}
return;
}
我尝试以递归的方式执行此操作,但对Del的删除调用使我们得到了无限循环
_BS ~BSNode()
{
Del(this);
}
void _BS Del(BSNode *x,int y)
{
if (x->isLeaf())
{
delete x;
return;
}
if (x->_Right != NULL)
Del(x->_Right,y += 1);
if (x->_Left != NULL)
Del(x->_Left,y += 1);
if (y != 1)
{
delete x;
}
return;
}
您正在this
递归删除。您不需要所有这些。你太想了。所有你需要的是:
BSNode::~BSNode()
{
delete _Left;
delete _Right;
}
这将自动向左和向右子树递归,并在null处停止。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句