0

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

#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 **’)

为什么?

4

2 回答 2

2

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

*tem = *tree1;

应该:

tem = *tree1;

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

于 2013-10-26T04:01:54.580 回答
1

这里:

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

tree是一个指向指针的指针,
并且tem是一个指针
so语句:

*tem = *tree1;

应该:

tem = *tree1;
于 2013-10-26T04:09:07.863 回答