二叉树的建立与基本操作

编写程序实现二叉树的如下操作:
1) 建立二叉链表
2) 二叉树的先序、中序、后序遍历
3) 求二叉树的叶子结点个数
4) 将二叉树中所有结点的左、右子树相互交换

输入:
  按完全二叉树的层次关系给出二叉树的遍历序列(#表示虚结点,数据结点为单一字符)。

输出:
  二叉树的凹入表示
  二叉树的先序序列、中序序列、后序序列
  二叉树叶子结点个数
  左、右子树相互交换后的二叉树的凹入表示
  左、右子树相互交换后的二叉树的先序序列、中序序列、后序序列。

说明:
  在输出凹入表示的二叉树时,先输出根结点,然后依次输出左右子树,上下层结点之间相隔 3 个空格。

测试输入期待的输出时间限制内存限制额外进程
测试用例 1以文本方式显示
  1. abc#de↵
以文本方式显示
  1. BiTree↵
  2. a↵
  3.     b↵
  4.         d↵
  5.     c↵
  6.         e↵
  7. pre_sequence  : abdce↵
  8. in_sequence   : bdaec↵
  9. post_sequence : dbeca↵
  10. Number of leaf: 2↵
  11. BiTree swapped↵
  12. a↵
  13.     c↵
  14.         e↵
  15.     b↵
  16.         d↵
  17. pre_sequence  : acebd↵
  18. in_sequence   : ceadb↵
  19. post_sequence : ecdba↵
1秒64M0
测试用例 2以文本方式显示
  1. abcdefg↵
以文本方式显示
  1. BiTree↵
  2. a↵
  3.     b↵
  4.         d↵
  5.         e↵
  6.     c↵
  7.         f↵
  8.         g↵
  9. pre_sequence  : abdecfg↵
  10. in_sequence   : dbeafcg↵
  11. post_sequence : debfgca↵
  12. Number of leaf: 4↵
  13. BiTree swapped↵
  14. a↵
  15.     c↵
  16.         g↵
  17.         f↵
  18.     b↵
  19.         e↵
  20.         d↵
  21. pre_sequence  : acgfbed↵
  22. in_sequence   : gcfaebd↵
  23. post_sequence : gfcedba↵
1秒64M0

代码如下:

相当套路化的一道题,不想加注释了

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

int leafamount;
typedef struct BITree
{
    char data;
    struct BITree *r;
    struct BITree *l;
} Tree;

Tree *createNode(char data)
{
    Tree *node = (Tree *)malloc(sizeof(Tree));
    node->data = data;
    node->l = NULL;
    node->r = NULL;
    return node;
}
Tree *createBiTree(char *input, int length, int index)
{
    if (index >= length || input[index] == '#')
        return NULL;
    Tree *root = createNode(input[index]);
    int leftIndex = 2 * index + 1;
    int rightIndex = 2 * index + 2;
    if (leftIndex < length)
        root->l = createBiTree(input, length, leftIndex);
    if (rightIndex < length)
        root->r = createBiTree(input, length, rightIndex);
    return root;
}
void printTree(Tree *root, int rank)
{
    if (root == NULL)
        return;
    for (int i = 0; i < rank; i++)
        printf("    ");
    printf("%c\n", root->data);
    printTree(root->l, rank + 1);
    printTree(root->r, rank + 1);
}
void Preorder(Tree *root)
{
    if (root == NULL)
        return;
    printf("%c", root->data);
    Preorder(root->l);
    Preorder(root->r);
}
void Inorder(Tree *root)
{
    if (root == NULL)
        return;
    Inorder(root->l);
    printf("%c", root->data);
    Inorder(root->r);
}
void Postorder(Tree *root)
{
    if (root == NULL)
        return;
    Postorder(root->l);
    Postorder(root->r);
    printf("%c", root->data);
}
int Leafamount(Tree *root)
{
    if (root == NULL)
        return 0;
    if (root->l == NULL && root->r == NULL)
        leafamount++;
    Leafamount(root->l);
    Leafamount(root->r);
    return 0;
}
Tree *swapSubtrees(Tree *root)
{
    if (root == NULL)
        return NULL;
    Tree *temp = root->l;
    root->l = root->r;
    root->r = temp;
    swapSubtrees(root->l);
    swapSubtrees(root->r);
    return root;
}
const char *traversalNames[] =
    {
        "pre_sequence  : ",
        "in_sequence   : ",
        "post_sequence : "};
void (*traversalFunctions[])(Tree *) =
    {
        Preorder,
        Inorder,
        Postorder};
void output(Tree *root)
{
    for (int i = 0; i < 3; i++)
    {
        printf("%s", traversalNames[i]);
        traversalFunctions[i](root);
        printf("\n");
    }
}
int main()
{
    char input[200];
    memset(input, '\0', sizeof(input));
    scanf("%s", input);
    int length = strlen(input);
    int index = 0;
    Tree *root = createBiTree(input, length, index);

    printf("BiTree\n");
    printTree(root, 0);
    output(root);

    leafamount = 0;
    Leafamount(root);
    printf("Number of leaf: %d\n", leafamount);

    root = swapSubtrees(root);

    printf("BiTree swapped\n");
    printTree(root, 0);
    output(root);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值