二叉树插入问题

[复制链接]
查看11 | 回复1 | 2010-5-31 09:14:59 | 显示全部楼层 |阅读模式
#include
#include
#include
typedef int datatype;
typedef struct tree{
char value[88];
struct tree *left,*right;
}tree;
void into_tree(tree *root,char value[])
{
tree *current;
tree **link;
link=&root;
while( (current=*link)!=NULL )
{
if( strcmp(current->value,value)==1)
link=&current->left;
else
link=&current->right;
}
current=malloc(sizeof(tree));
strcpy(current->value,value);
current->left=current->right=NULL;
*link=current;
}
void print_tree(tree *t)
{
if(t)
{
print_tree(t->left);
printf("%s\n",t->value);
print_tree(t->right);
}
}
main()
{struct tree *root=NULL;
char s[][88]={"ha","daf","dad"};
into_tree(root,s[0]);
print_tree(root);
}
为什么我插入s[0]后,root还是null?
怎么就没有改变root中value的值呢???

回复

使用道具 举报

千问 | 2010-5-31 09:14:59 | 显示全部楼层
这个地方的错误在于,into_tree的参数不对,应该使用指向指针的指针,用于在函数内部来改变采用指针方式传进函数的主函数的指什的指向。修改后程序如下:#include "stdafx.h"#include #include#include#includetypedef int datatype;typedef struct tree{ char value[88]; struct tree *left,*right;}tree;void into_tree(tree **root,char value[])//多一个*号
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行