Studying BST (binary search tree) I did not understand some parts of the code in the delete function of "mycodeschool".
This part of the code, more specifically:
// Case 1: No child
if(root->left == NULL && root->right == NULL) {
delete root;
root = NULL;
}
//Case 2: One child
else if(root->left == NULL) {
struct Node *temp = root;
root = root->right;
delete temp;
}
I do not understand why in "Case 1" as root
can receive NULL
after receiving the delete
command. I could just put root=NULL
and only?
And also in "Case 2" When deleting temp
, root is also being deleted, is not it? since temp
has the same root
address.
And because in "Case 1" root=root->right
comes before the main command delete
and in "Case 2" root=NULL
is after the main command delete
?