简体   繁体   中英

Binary Search Tree deletion

I am a beginner in programming and currently learning about bst. I have found several functions which delete bst, but they seemed to me overcomplexed. 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. The rest of the code is fine. Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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