我正在建立一个二叉搜索树。代码如下:
#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
分支中有正确的版本。关于c - 二叉搜索树构造中的编译错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19602638/