我试图在C++中删除二叉树,但我遇到的问题是树的大小似乎没有改变.这是我正在使用的尺寸函数:
int BST::size(Node *& cur_root)
{
if (cur_root == NULL) {
return 0;
} else {
return(size(cur_root->m_left) + 1 + size(cur_root->m_right));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我正在尝试使用它的功能:
void BST::deletetree(Node *& cur_root)
{
cout << "tree size: " << size() << endl;
if (cur_root!=NULL)
{
deletetree(cur_root->m_left);
deletetree(cur_root->m_right);
delete cur_root;
if(cur_root->m_left != NULL) {
cur_root->m_left = NULL;
}
if(cur_root->m_right != NULL) {
cur_root->m_right = NULL;
}
cur_root=NULL;
}
}
Run Code Online (Sandbox Code Playgroud)
对于三个(1,2,3)的树大小,我的输出是:
tree size: 3
tree size: 3
tree size: 3
tree size: 3
tree size: 3 …Run Code Online (Sandbox Code Playgroud)