C ++: Pointer vs Pointer of Pointer zum Einfügen eines Knotens in einen Binary Tree
Ich habe eine Funktion zum Einfügen eines Elements in einen Binärbaum erstellt und in Visual Studio 2012 Folgendes ausgeführt:
void Insert(Nodo *root, int x){
if(root == NULL){
Nodo *n = new Nodo();
n->value = x
root = n;
return;
}
else{
if(root->value > x)
Insert(&(root)->left, x);
else
Insert(&(root)->right, x);
}
}
Aber dieser Code funktioniert in Dev-C ++ nicht, ich muss Pointer of Pointer verwenden, damit er funktioniert:
void Insert(Nodo **root, int x){
if(*root == NULL){
Nodo *n = new Nodo();
n->value = x
*root = n;
return;
}
else{
if((*root)->value > x)
Insert(&(*root)->left, x);
else
Insert(&(*root)->right, x);
}
}
Weiß jemand, warum es passiert?