Tree 由于二叉搜索树插入有问题,它对左树工作正常,但对右树工作不正常
这是我正在使用的插入函数。根创建和作为左子项插入工作正常。但是作为右子项插入只发生两次Tree 由于二叉搜索树插入有问题,它对左树工作正常,但对右树工作不正常,tree,binary-tree,binary-search-tree,insertion,perl-data-structures,Tree,Binary Tree,Binary Search Tree,Insertion,Perl Data Structures,这是我正在使用的插入函数。根创建和作为左子项插入工作正常。但是作为右子项插入只发生两次 struct node*insert(struct node*root1,struct node*new1) {printf(“根地址=%u”,root1); if(root1==NULL){ printf(“xyz”); root1=new1; 返回根1; } 如果(root1->data>new1->data) { 如果(root1->lchild==NULL){ root1->lchild=new1;
struct node*insert(struct node*root1,struct node*new1)
{printf(“根地址=%u”,root1);
if(root1==NULL){
printf(“xyz”);
root1=new1;
返回根1;
}
如果(root1->data>new1->data)
{
如果(root1->lchild==NULL){
root1->lchild=new1;
printf(“A1”);
}
否则{
printf(“A2”);
插入(root1->lchild,new1);
}
}
if(root1->datadata)
{
如果(root1->rchlid==NULL){
root1->rchlid=new1;
printf(“B1”);
}
否则{
printf(“B2”);
插入(root1->rchlid,new1);
}
}
printf(“FFF”);
返回根;
}
简化:
使用返回值,卢克!在何处使用sir:
root1!=root
(root从未定义)先生,我使用过此代码,但问题仍然存在。
struct node * insert(struct node *root1, struct node *new1)
{ printf("root address=%u",root1);
if(root1==NULL){
printf("xyz");
root1=new1;
return root1;
}
if(root1->data>new1->data)
{
if(root1->lchild==NULL){
root1->lchild=new1;
printf("A1");
}
else{
printf("A2");
insert(root1->lchild,new1);
}
}
if(root1->data < new1->data)
{
if(root1->rchlid==NULL){
root1->rchlid=new1;
printf("B1");
}
else{
printf("B2");
insert(root1->rchlid,new1);
}
}
printf("FFF");
return root;
}
struct node * insert(struct node *zroot, struct node *new1)
{
if(zroot==NULL) return new1;
if (zroot->data>new1->data) zroot->lchild = insert(zroot->lchild,new1);
else zroot->rchild = insert(zroot->rchild,new1);
return zroot;
}