Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C BST插入不工作_C_Binary Search Tree - Fatal编程技术网

C BST插入不工作

C BST插入不工作,c,binary-search-tree,C,Binary Search Tree,我试图实现一个二进制搜索树的代码。问题是下面的代码不起作用,但若我将双指针传递给insert函数,比如insert(struct bst**node,data),它就会起作用。我认为它也应该适用于传递单指针。谁能解释一下这里的错误是什么 void insert(struct bst* node, int data ) { if (node == NULL) { printf("here with %d\n",data); node = (struc

我试图实现一个二进制搜索树的代码。问题是下面的代码不起作用,但若我将双指针传递给insert函数,比如insert(struct bst**node,data),它就会起作用。我认为它也应该适用于传递单指针。谁能解释一下这里的错误是什么

void insert(struct bst* node, int data )
{
    if (node == NULL)
    {
        printf("here with %d\n",data);
        node = (struct bst*)malloc(sizeof(struct bst));
        node->data = data;
        node->left = NULL;
        node->right = NULL;
    }
    if(data < node->data)
    {
        insert(node->left,data);
    }
    else if(data > node->data)
    {
        insert(node->right,data);
    }
}
void插入(结构bst*节点,int数据)
{
if(node==NULL)
{
printf(“此处包含%d\n”,数据);
node=(struct bst*)malloc(sizeof(struct bst));
节点->数据=数据;
节点->左=空;
节点->右=空;
}
if(数据<节点->数据)
{
插入(节点->左侧,数据);
}
else if(数据>节点->数据)
{
插入(节点->右侧,数据);
}
}

如果要更改指针的值,应传递指针的地址(作为结构节点**)

使用您的代码:

node = (struct bst*)malloc(sizeof(struct bst));

节点
的值在
插入
函数中更改,但不更改调用函数中的变量。

如果要更改传递给函数的指针的值,应将其作为指针传递给指针

void alloc_int(int** p)
{
  *p = malloc(sizeof(int));
}

int main()
{
  int* p = NULL;
  alloc_int(&p);
  *p = 10; // here memory for p is allocated so you can use it
  free(p);
  return 0;
}

在你的例子中也是这样。您必须传递指针的地址才能更改其值(指针的值是实际数据的地址)。

您需要能够修改将成为
节点的父节点的指针。当您进行递归调用
insert(node->left,data)
时,如果
node
(新节点的父节点)没有左子节点(
left==null
),则您正在调用
insert(null,data)
。第一个
if
语句将创建新节点并分配其数据,但无法将该节点挂接到树中。另外,由于
insert
不会返回新节点, 节点将永远丢失

修复此问题的快速方法是返回新节点:

struct bst *insert(struct bst* node, int data, struct bst* parent )
{ /// Note new return value
    if (node == NULL)
    {
        printf("here with %d\n",data);
        node = (struct bst*)malloc(sizeof(struct bst));
        node->data = data;
        node->left = NULL;
        node->right = NULL;
        return node; /// NEW - send it back to the parent
    }

    if(data < node->data)
    {
        node->left = insert(node->left,data); /// save the new child if there wasn't one
        return node; /// otherwise, send back the node so the tree doesn't change.
    }
    else //if(data > node->data) /// On equal keys, add to the right
    {
        node->right = insert(node->right,data);
        return node;
    }
}
struct bst*插入(struct bst*节点,int数据,struct bst*父节点)
{///注意新的返回值
if(node==NULL)
{
printf(“此处包含%d\n”,数据);
node=(struct bst*)malloc(sizeof(struct bst));
节点->数据=数据;
节点->左=空;
节点->右=空;
return节点;///NEW-将其发送回父节点
}
if(数据<节点->数据)
{
node->left=insert(node->left,data);///如果没有新的子节点,请保存它
return node;///否则,发回节点,使树不会更改。
}
else//(数据>节点->数据)///在相等的键上,添加到右侧
{
节点->右侧=插入(节点->右侧,数据);
返回节点;
}
}
(免责声明:代码尚未测试)