C语言 二叉搜索树构造中的编译错误



我正在构建一个二叉搜索树。这是代码:

#include<stdio.h>
#include<stdlib.h>

struct tree_node
{
    int val;
    struct tree_node *left;
    struct tree_node *right;
};
void insert(struct tree_node **, int);
int main(void)
{
    struct tree_node *tree;
    tree = NULL;
    insert(&tree, 10);
    insert(&tree, 20);
    insert(&tree, 5);
    insert(&tree, 7);
    return 0;   
}
void insert(struct tree_node **tree1, int value)
{
    struct tree_node *temp, *start, *tem;
    tem = NULL;
    temp = (struct tree_node *)malloc(sizeof(struct tree_node) );
    start = *tree1;
    temp->val = value;
    temp->left = NULL;
    temp->right = NULL;
    if(*tree1 == NULL)
        *tree1 = temp;
    else
    {
        while(*tree1 != NULL )
        {
            if(value <= (*tree1)->val)
            {
                *tem = *tree1;
                *tree1 = (*tree1)->left;
            }
            else
            {
                tem = *tree1;
                *tree1 = (*tree1)->right;   
            }
        }
        if((tem->left) ==  *tree1)
            tem->left = temp;
        else
            tem->right = temp;
    }
    *tree1 = start;
}

我收到编译错误:

prog.c:44:5: error: invalid operands to binary * (have ‘struct tree_node *’ and ‘struct tree_node **’)

为什么?

if(value <= (*tree1)->val)里面,

*tem = *tree1;

应该是:

tem = *tree1;

您在else分支中具有正确的版本。

在这里:

void insert(struct tree_node **tree1, int value)

tree 是指向指针的指针,并且
tem是指针
所以声明:

*tem = *tree1;

应该是:

tem = *tree1;

相关内容

  • 没有找到相关文章

最新更新