我正在从包含一些整数的输入文件中读取整数并尝试创建二叉树。我无法弄清楚为什么它一直在 (l==NULL)。第二次它应该向左或向右取决于读取值是否大于根值。感谢任何帮助。谢谢!
leaf* create_leaf(int i)
{
leaf* l = (leaf*)malloc(sizeof(leaf));
l->left = NULL;
l->right = NULL;
l->data = i;
}
leaf* l=NULL;
while(fscanf(input,"%d",&i) != EOF)
{
add_leaf(l,i);
}
void add_leaf(leaf* l, int i)
{
if(l == NULL)
{
printf("adding root\n");
l = create_leaf(i);
}
else if(i <= l->data)
{
if(l->left == NULL)
{
l->left = create_leaf(i);}
else {
add_leaf(l->left,i); }
}
else
{
if(l->right == NULL)
{
l->right = create_leaf(i); }
else {
add_leaf(l->right,i); }
}
}