【发布时间】:2018-03-18 11:41:37
【问题描述】:
所以这里是可以正常工作的函数:
void Insert(node ** root, int inpdata){//tree's root should be passed here
if(*root == NULL){
*root = createNode(inpdata);
}
else if(inpdata < (*root)->data){
Insert(&(*root)->left,inpdata);
}
else{
Insert(&(*root)->right,inpdata);
}
}
但我不明白为什么我们必须使用双指针。例如,为什么以下代码不起作用:
void Insert(node * root, int inpdata){//tree's root should be passed here
if(root == NULL){
root = createNode(inpdata);
}
else if(inpdata < root->data){
Insert(root->left,inpdata);
}
else{
Insert(root->right,inpdata);
}
}
另外,在第一个函数中,我无法理解&(*root) 的用法。这不是没有意义,因为*root 本身就是一个指针。因此,“指针的地址”是多余的,因为指针已经存储了地址值。我可能有点困惑,所以非常感谢您的帮助。
谢谢!
【问题讨论】:
标签: c pointers binary-tree double-pointer