gpt4 book ai didi

C程序将一个二叉搜索树复制到另一个

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:25 25 4
gpt4 key购买 nike

所以,在这里我想出了二叉搜索树 prgram,我正在创建 2 个二叉树 tmp 和 tmp2,我试图将整个 tmp2 复制到 tmp,该节点被视为用户的输入。但是我遇到了一些段错误,而且我不太确定逻辑是否正确。这是整个程序,请让我知道它在 t_cpy() 中哪里出了问题,或者请帮我修复它..

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *rlink;
struct node *llink;
}*tmp=NULL,*tmp2=NULL,*tmp3=NULL;

typedef struct node NODE;

NODE *create();

void inorder(NODE *);
void insert(NODE *);
void t_cpy(NODE *,NODE *);

int main()
{

int n,m;
do
{
printf("\n1.create tree 1\n2.Insert element to tree1\n3.create tree 2\n4.Insert element to tree2\n5.Inorder tree1\n6.Inorder tree2\n7.Copy tree2 to tree1\n8.exit\n\n");
printf("\nEnter ur choice: ");
scanf("%d",&m);
switch(m)
{
case 1: tmp=create();
break;
case 2: insert(tmp);
break;
case 3: tmp2=create();
break;
case 4:
insert(tmp2);
break;
case 5: printf("\n\nInorder Tree1: ");
inorder(tmp);
break;
case 6: printf("\n\nInorder Tree 2: ");
inorder(tmp2);
break;
case 7: t_cpy(tmp,tmp2);

break;
case 8: return(0);
}
}while(n!=8);
return(0);
}
void insert(NODE *root)
{
NODE *newnode;
if(root==NULL)
{
newnode=create();
root=newnode;
}
else
{
newnode=create();
while(1)
{
if(newnode->data<root->data)
{
if(root->llink==NULL)
{
root->llink=newnode;
break;
}
root=root->llink;
}
if(newnode->data>root->data)
{
if(root->rlink==NULL)
{
root->rlink=newnode;
break;
}
root=root->rlink;
}
}
}
}

NODE *create()
{
NODE *newnode;
int n;
newnode=(NODE *)malloc(sizeof(NODE));
printf("\n\nEnter the Data ");
scanf("%d",&n);
newnode->data=n;
newnode->llink=NULL;
newnode->rlink=NULL;
return(newnode);
}


void t_cpy(NODE *t1,NODE *t2)
{
int val,opt=0;
NODE *temp;
if(t1==NULL || t2==NULL)
{
printf("Can not copy !\n");
}
inorder(t1);

printf("\nEnter the node value where tree 2 should be copied\n");
scanf("%d",&val);
temp=t1;
while(temp!=NULL)
{
if(val<temp->data)
temp=temp->llink;
else
temp=temp->rlink;
}
if(temp->llink!=NULL || temp->rlink!=NULL)
printf("Not possible to copy tree to this node\n");
else
{
printf("Copy tree to \n 1.Left Node \n 2.Right Node\n Enter your choice : ");
scanf("%d",&opt);
if(opt==1)
{
temp->llink=t2;
}
else if(opt==2)
{
temp->rlink=t2;
}
else
printf("Invalid choice\n");
}
printf("Tree1 after copying is\n");
inorder(temp);
}


void inorder(NODE *tmp)
{
if(tmp!=NULL)
{
inorder(tmp->llink);
printf("%d",tmp->data);
inorder(tmp->rlink);
}
}

编辑:感谢@xaxxon,他帮助了我。只需更新 while 即可使其工作:

while(temp!=NULL&&temp->data!=val)
{
if(val<temp->data)
temp=temp->llink;
else
temp=temp->rlink;
if(temp->llink==NULL && temp->rlink==NULL && temp->data!=val)
{
printf("Invalid Node value entered !\n");
//break;
return 0;
}

并且,如果输入的值存在于树中,现在它可以正常工作。

谢谢:)

最佳答案

在其他可能的问题中,您遍历 temp 直到它为 null,然后在下一行取消引用它。

while(temp!=NULL)
{
if(val<temp->data)
temp=temp->llink;
else
temp=temp->rlink;
}
if(temp->llink!=NULL || temp->rlink!=NULL)
printf("Not possible to copy tree to this node\n");

如果 val == temp->data,你很可能想跳出这个循环,但你没有。此外,您仍然需要在循环后检查 temp 是否为空,以防您在树中找不到 val 。很可能你只是想说:

    if(temp==NULL)
printf("Not possible to copy tree to this node\n");

此外,您不能询问用户要将树复制到找到的节点的哪一侧。如果你有一个二叉搜索树,它必须是值应该去的那一边。如果你说复制到右边,但是所有的值都小于这个节点,就不是BST了。事实上,你甚至不能问这个值应该去哪里,仍然有一个二叉搜索树。必须从要将另一棵树放入的树的根开始遍历每个节点,以维护 BST 机制。

关于C程序将一个二叉搜索树复制到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16827428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com