0
Hello, folks. I’d like to know why the function below would need a pointer to pointer, in case a **root
, for the line /1/
could modify the *root
, while the line /2/ can modify it without having to use it. What’s the difference between:
root->right = remover(root->right,valor);
and
root = NULL;
?
Aren’t the two trying to modify the pointer without using a pointer to the pointer? So, the two shouldn’t be wrong?
FUNCTION:
Node * remover(Node *root, int valor){
if(root == NULL){
cout<<"Elemento não encontrado."<<endl;
}
else{
if(root->data == valor){
root = NULL;
cout<<"Elemento removido!"<<endl;
return root;
}
if(root->data > valor){
/* 1 */ //remover(root->left,valor); não funciona
/* 2 */root->left = remover(root->left,valor); // funciona
return root;
}
if(root->data < valor){
/* 1 */ //remover(root->right,valor); não funciona
/* 2 */root->right = remover(root->right,valor); // funciona
return root;
}
}
}
Where are you seeing pointer pointer? I don’t understand what the doubt is. Without seeing the whole, it seems to me that the code is right.
– Maniero
I said if instead of
*root
I had used**root
, the program would work with the /1/ line as well. I just wanted to understand why line 2 works, even using only a simple pointer,*root
.– João Luca
The function should be void there is no reason to edit a variable and return it.
– krystalgamer