我尝试实现 BST 程序,但由于运行时错误,执行失败。请告诉我如何纠正它。我创建树的代码是:
struct node *createBinTree()
{ int val,size;
struct node *bintree, *newnode;
bintree= NULL;
printf("Enter the values of nodes terminated by a negetive no.\n");
val=0;
while(val>=0)
{
printf("\nEnter value");
scanf("%d",&val);
if(val>=0)
{ newnode = (struct node*)sizeof(struct node);
newnode->val=val;
newnode->lchild= NULL;
newnode->rchild= NULL;
bintree=attach(bintree,newnode);
}
}
return bintree;
}
struct node *attach(struct node *tree,struct node* tnode)
{if(tree==NULL)
tree=tnode;
else{
if(tnode->val<tree->val)
tree->lchild= attach(tree->lchild,tnode);
else
tree->rchild= attach(tree->rchild,tnode);
}
return tree;
}