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"
rootcan receiveNULLafter receiving the commanddelete. I could just putroot=NULLand only?And also in "Case 2" When you delete
temp, root is also being deleted, not? sincetemphas 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
deletecomes before the main commandroot=NULLand in "Case 2"deleteis after the main commandroot=root->right?