3
Studying BST (binary search tree) did not understand some parts of the code in the function delete of "mycodeschool".
That 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 don’t understand why in "Case 1"
root
can receiveNULL
after receiving the commanddelete
. I could just putroot=NULL
and only?And also in "Case 2" When you delete
temp
, root is also being deleted, not? sincetemp
has the same address asroot
.And I also did not understand the need to delete since there is the command
root=root->right
.And why in "Case 1" the
delete
comes before the main commandroot=NULL
and in "Case 2"delete
is after the main commandroot=root->right
?