NOJ-二叉排序树的归并-西工大数据结构

    题目如下:


    我就随便写了写,还用的上次的函数就可以。

    以下是我的实现:

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

struct binaryTreeNode
{
    int num;
    struct binaryTreeNode *left;
    struct binaryTreeNode *right;
};

struct binaryTree
{
    struct binaryTreeNode *root;
};

void run();
struct binaryTreeNode *createNewTree();
struct binaryTreeNode *createNewNode();
struct binaryTreeNode *getNode(int num);
void merge(struct binaryTreeNode *root);
void cutInNode(int num);
void print(struct binaryTreeNode *root);

struct binaryTree T1, T2, T3;

int main()
{
    run();
    return 0;
}

void run()
{
    T1.root = createNewTree();
    T2.root = createNewTree();
    merge(T2.root);
    print(T1.root);
}

struct binaryTreeNode *createNewTree()
{
    int num;
    struct binaryTreeNode *cur;
    scanf("%d", &num);
    if (num == -1) return NULL;
    cur = createNewNode();
    cur->num = num;
    cur->left = createNewTree();
    cur->right = createNewTree();
    return cur;
}

struct binaryTreeNode *createNewNode()
{
    struct binaryTreeNode *p;
    p = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
    p->num = 0;
    p->left = NULL;
    p->right = NULL;
    return p;
}

struct binaryTreeNode *getNode(int num)
{
    struct binaryTreeNode *p = T1.root;
    while(1)
    {
        if(p->num == num) return NULL;
        if(p->num < num)
        {
            if(p->right) p = p->right;
            else return p;
        }
        else
        {
            if(p->left) p = p->left;
            else return p;
        }
    }
}

void merge(struct binaryTreeNode *root)
{
    cutInNode(root->num);
    if(root->left) merge(root->left);
    if(root->right) merge(root->right);
}


void cutInNode(int num)
{
    struct binaryTreeNode *p, *newNode;
    p = getNode(num);
    if(p)
    {
        newNode = createNewNode();
        newNode->num = num;
        if(num < p->num) p->left = newNode;
        else p->right = newNode;
    }
}

void print(struct binaryTreeNode *root)
{
    if(root->left) print(root->left);
    printf("%d ", root->num);
    if(root->right) print(root->right);
}

    以下是各函数的注释:

void run()
{
    T1.root = createNewTree();//创建
    T2.root = createNewTree();
    merge(T2.root);//归并
    print(T1.root);//输出
}
//递归创建树
struct binaryTreeNode *createNewTree()
{
    int num;
    struct binaryTreeNode *cur;
    scanf("%d", &num);
    if (num == -1) return NULL;
    cur = createNewNode();
    cur->num = num;
    cur->left = createNewTree();
    cur->right = createNewTree();
    return cur;
}
//创建节点
struct binaryTreeNode *createNewNode()
{
    struct binaryTreeNode *p;
    p = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
    p->num = 0;
    p->left = NULL;
    p->right = NULL;
    return p;
}
//寻找待插入的插入位置
struct binaryTreeNode *getNode(int num)
{
    struct binaryTreeNode *p = T1.root;
    while(1)
    {
        if(p->num == num) return NULL;
        if(p->num < num)
        {
            if(p->right) p = p->right;
            else return p;
        }
        else
        {
            if(p->left) p = p->left;
            else return p;
        }
    }
}
//前序遍历合并
void merge(struct binaryTreeNode *root)
{
    cutInNode(root->num);
    if(root->left) merge(root->left);
    if(root->right) merge(root->right);
}

//插入
void cutInNode(int num)
{
    struct binaryTreeNode *p, *newNode;
    p = getNode(num);
    if(p)
    {
        newNode = createNewNode();
        newNode->num = num;
        if(num < p->num) p->left = newNode;
        else p->right = newNode;
    }
}
//中序遍历输出
void print(struct binaryTreeNode *root)
{
    if(root->left) print(root->left);
    printf("%d ", root->num);
    if(root->right) print(root->right);
}
    以上就是我的实现。







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值