【发布时间】:2013-05-15 08:31:14
【问题描述】:
完整代码http://pastebin.com/6bdVTyPt
我的树代码运行良好,直到我发现我需要验证它不是文本的 id 所以它必须是字符串
插入函数
90 和 129 的字符串比较返回 8
尝试使用 (atoi) 并比较为整数不起作用
任何帮助表示赞赏
谢谢
这里是使用 atoi 代替 strcomp 的插入函数
http://pastebin.com/yeuktyAF 还是不行
插入函数
struct node * insert2(struct node *root, char x[],char id[])
{
if(!root)
{
root=(struct node*)malloc(sizeof(struct node));
free( root->data );
free( root->id );// free previously allocated memory, if any
root->data = strdup( x ); // malloc and copy
root->id=strdup(id);
root->left = NULL;
root->right = NULL;
// printf("1\n");
return(root);
}
printf("string comp %d of %s of %s\n",strcmp(root->id,id),root->id,id);
if((strcmp(root->id,id))>0){
root->left = insert(root->left,x,id);
printf("go left\n");
}
else
{
if(strcmp(root->id,id)<0){
printf("go right\n");
root->right = insert(root->right,x,id);}
}
return(root);
}
【问题讨论】:
标签: c data-structures tree