二叉排序树的合并

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node
{
    int data;
    struct node*lchild;
    struct node*rchild;
}BiTNode,*BiTree;
void creat(BiTree*root)
{
    int ch;
    scanf("%d",&ch);
    if(ch==-1)
    {
    *root=NULL;
    }
    else
    {
    *root=(BiTree)malloc(sizeof(BiTNode));
    (*root)->data=ch;
    creat(&((*root)->lchild));
    creat(&((*root)->rchild));
    }
}
void insert(BiTree *root,int c)
{
    BiTree s;
    if(*root==NULL)
    {
        s=(BiTree)malloc(sizeof(BiTNode));
        s->data=c;
        s->lchild=NULL;
        s->rchild=NULL;
        *root=s;
    }
    else if(c<(*root)->data)
        insert(&(*root)->lchild,c);
    else if(c>(*root)->data)
        insert(&(*root)->rchild,c);
}
void inorder(BiTree root)
{
    if(root!=NULL)
    {
        inorder(root->lchild);
        printf("%d ",root->data);
        inorder(root->rchild);
    }
}
void combine(BiTree T1,BiTree T2)
{
    if(T2!=NULL)
    {
        insert(&T1,T2->data);
        combine(T1,T2->lchild);
        combine(T1,T2->rchild);
    }
}
int main()
{
    BiTree root1=NULL;
    BiTree root2=NULL;
    creat(&root1);
    creat(&root2);
    combine(root1,root2);
    inorder(root1);
    printf("\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值