简体   繁体   English

二叉搜索树删除

[英]Binary Search Tree deletion

I am a beginner in programming and currently learning about bst.我是编程初学者,目前正在学习 bst。 I have found several functions which delete bst, but they seemed to me overcomplexed.我发现了几个删除 bst 的函数,但在我看来它们过于复杂。 So i am interested if my own code cleans all the memory, every pointer and value of a binary search tree.所以我很感兴趣,如果我自己的代码清理所有内存,二叉搜索树的每个指针和值。

void deletebst (Node*& node)
{
if (node!=NULL)
{
deletebst (node->ldes); //deletes a left descendant recursively
deletebst (node->rdes); //same but with the right one
delete node; //deletes the value which a pointer 'node' points to
node=NULL; //sets a pointer 'node' to NULL, so deletes a pointer itself
}
}

As pointed out in the comments section, there's no need to set node = NULL, after it's been deleted.正如评论部分所指出的,删除后无需设置 node = NULL。 The rest of the code is fine.其余的代码很好。 Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM