Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将一个二叉搜索树复制到另一个二叉搜索树的C程序_C_Data Structures_Binary Search Tree - Fatal编程技术网

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

将一个二叉搜索树复制到另一个二叉搜索树的C程序,c,data-structures,binary-search-tree,C,Data Structures,Binary Search Tree,所以,这里我提出了二叉搜索树prgram,在这里我创建了两棵二叉树tmp和tmp2,我试图将整个tmp2复制到tmp,作为用户输入的节点。但是我得到了一些分割错误,而且我不太确定逻辑是否正确。 这是整个程序,请让我知道t_cpy()中的错误在哪里,或者请帮我修复它 #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *rlink; struct node *llink; }*t

所以,这里我提出了二叉搜索树prgram,在这里我创建了两棵二叉树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);
    }
}
#包括
#包括
结构节点
{
int数据;
结构节点*rlink;
结构节点*llink;
}*tmp=NULL,*tmp2=NULL,*tmp3=NULL;
typedef结构节点;
节点*create();
按顺序无效(节点*);
空白插入(节点*);
void t_cpy(节点*,节点*);
int main()
{
int n,m;
做
{
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(“\n输入您的选择:”);
scanf(“%d”、&m);
开关(m)
{
案例1:tmp=create();
打破
案例2:插入(tmp);
打破
案例3:tmp2=create();
打破
案例4:
插入(tmp2);
打破
案例5:printf(“\n\n顺序树1:”);
有序(tmp);
打破
案例6:printf(“\n\n顺序树2:”);
有序(tmp2);
打破
案例7:t_cpy(tmp,tmp2);
打破
案例8:返回(0);
}
}而(n!=8);
返回(0);
}
无效插入(节点*根)
{
节点*新节点;
if(root==NULL)
{
newnode=create();
根=新节点;
}
其他的
{
newnode=create();
而(1)
{
if(newnode->datadata)
{
如果(根->llink==NULL)
{
root->llink=newnode;
打破
}
root=root->llink;
}
如果(新建节点->数据>根->数据)
{
如果(根->rlink==NULL)
{
root->rlink=newnode;
打破
}
root=root->rlink;
}
}
}
}
节点*create()
{
节点*新节点;
int n;
newnode=(NODE*)malloc(sizeof(NODE));
printf(“\n\n输入数据”);
scanf(“%d”和“&n”);
newnode->data=n;
newnode->llink=NULL;
newnode->rlink=NULL;
返回(newnode);
}
void t_cpy(节点*t1,节点*t2)
{
int-val,opt=0;
节点*温度;
如果(t1==NULL | | t2==NULL)
{
printf(“无法复制!\n”);
}
有序(t1);
printf(“\n输入应该复制树2的节点值\n”);
scanf(“%d”和&val);
温度=t1;
while(temp!=NULL)
{
if(valdata)
temp=temp->llink;
其他的
温度=温度->链接;
}
if(temp->llink!=NULL | | temp->rlink!=NULL)
printf(“无法将树复制到此节点\n”);
其他的
{
printf(“将树复制到\n 1.左节点\n 2.右节点\n输入您的选择:”);
scanf(“%d”和&opt);
如果(opt==1)
{
温度->llink=t2;
}
else if(opt==2)
{
温度->rlink=t2;
}
其他的
printf(“无效选择\n”);
}
printf(“复制后的树1是\n”);
顺序(温度);
}
无效索引(节点*tmp)
{
如果(tmp!=NULL)
{
顺序(tmp->llink);
printf(“%d”,tmp->data);
顺序(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;
    }
while(temp!=NULL&&temp->data!=val)
{
if(valdata)
temp=temp->llink;
其他的
温度=温度->链接;
如果(temp->llink==NULL&&temp->rlink==NULL&&temp->data!=val)
{ 
printf(“输入的节点值无效!\n”);
//中断;
返回0;
}
现在,如果输入的值存在于树中,它就可以正常工作


谢谢:)

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

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");

此外,您不能询问用户希望将树复制到已找到节点的哪一侧。如果您有一个二元搜索树,它必须是该值应该出现的一侧。如果您说要将其复制到右侧,但所有值都小于该节点,则它不再是BST。事实上,您甚至不能询问该值应该出现在哪里,并且仍然有一个值二元搜索树。必须从要将另一棵树放入的树的根遍历每个节点,以维护BST机制。

首次使用
insert(tmp)
调用
insert()
后,tmp的值不会更改。将tmp的地址传递到
insert()
,在其中使用*根而不是根。

导致崩溃的输入顺序是什么?它的nt输入顺序。.我想我在t_cpy()中的逻辑是错误的。是的,我需要在va;l==temp->data时中断循环,然后将其他树附加/复制到该节点。.那么while()中的条件应该是什么loop?事实上,请阅读我的进一步更新。您现在并没有寻找特定的崩溃错误--您的整个程序都错了。如果您愿意,我们可以聊一聊。我有几分钟的时间。我会教您有关二进制搜索树的知识。我希望我可以!我现在在西北大学实验室,所以我恐怕我可以聊西北:(如果你能在这里解决这个问题,我会很高兴,如果你能解释,我会跟进。)我是stackoverflow的新手:(Dodger和我聊天,他知道他现在需要做什么。我现在在创建/插入方面没有问题。插入后我遍历了它,效果很好。)问题在于t_cpy()你能不能把这个问题投票给他,让他有足够的声望,我可以和他聊天并解释一下?是的,这似乎会让插入无法工作,因为你永远不知道插入的节点在哪里,也永远不会有树根
    if(temp==NULL)
        printf("Not possible to copy tree to this node\n");