SCAU 8606、SCAU 17121、SCAU 18924、SCAU 18724、SCAU 18923

#include <stdio.h>
#include <string.h>
#define ElemType char
#define Status int
#define OK 1
#define ERROR 0

typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;

Status CreateBiTree(BiTree &T)
{
    char ch;
    scanf("%c", &ch);
    if (ch == '#')
        T = NULL;
    else
    {
        T = new BiTNode;
        T->data = ch;
        CreateBiTree(T->lchild);
        CreateBiTree(T->rchild);
    }
    return OK;
}

Status PreOrderTraverse(BiTree T)
{
    if (T)
    {
        printf("%c", T->data);
        PreOrderTraverse(T->lchild);
        PreOrderTraverse(T->rchild);
    }
    return OK;
}

Status InOrderTraverse(BiTree T)
{
    if (T)
    {
        InOrderTraverse(T->lchild);
        printf("%c", T->data);
        InOrderTraverse(T->rchild);
    }
    return OK;
}

Status PostOrderTraverse(BiTree T)
{
    if (T)
    {
        PostOrderTraverse(T->lchild);
        PostOrderTraverse(T->rchild);
        printf("%c", T->data);
    }
    return OK;
}

int n2 = 0, n1 = 0, n0 = 0;
void Count(BiTree T)
{
    if (T)
    {
        if (T->lchild && T->rchild)
            n2++;
        else if (!T->lchild && !T->rchild)
            n0++;
        else
            n1++;
        Count(T->lchild);
        Count(T->rchild);
    }
}

void BiTreeBreadth()
{
    int n, temp[49][2] = {0};
    scanf("%d", &n);
    for (int i = 0; i < n - 1; i++) //将输入存在temp数组内
        scanf("%d %d", &temp[i][0], &temp[i][1]);
    for (int i = 0; i < n - 2; i++) //利用简单选择排序,将数组按照父节点递增的形式排序
    {
        int min = i;
        for (int j = i + 1; j < n - 1; j++)
            if (temp[j][0] < temp[min][0])
                min = j;
        if (min != i)
        {
            int t1 = temp[min][0], t2 = temp[min][1];
            temp[min][0] = temp[i][0], temp[min][1] = temp[i][1];
            temp[i][0] = t1, temp[i][1] = t2;
        }
    }
    int depth[51] = {1}; //depth[i]是i的层数,初始化根结点层数为1
    for (int i = 0; i < n - 1; i++)
        depth[temp[i][1]] = depth[temp[i][0]] + 1; //每一个子节点层数等于其父节点层数+1
    int count[51] = {0};
    for (int i = 1; i <= n; i++)
        count[depth[i]]++; //count数组从1开始记录每一个节点的层数
    int max = count[0];
    for (int i = 1; i <= 50; i++)
        if (count[i] > max)
            max = count[i]; //max为节点数最多的那一层的节点数
    printf("%d", max);
}

char pre[100], in[100], post[100];
void PostTraverseTree(char pre[], char in[], int length, int root) //root指向最后一个空间,在这一次的函数调用中,它将被赋值为pre数组的第一个值
{
    if (length > 0) //长度小于等于0是不合法的字符串
    {
        post[root] = pre[0];
        int i = 0;
        while (in[i] != pre[0])
            i++;                                                             //找到先序遍历第一个字符在中序遍历中的位置,以此确定左右子树
        PostTraverseTree(pre + 1, in, i, root - (length - 1 - i) - 1);       //对左子树递归调用,得到左子树中的“最后访问节点”
        PostTraverseTree(pre + i + 1, in + i + 1, length - 1 - i, root - 1); //对右子树递归调用
    }
}

void BiTreeTraverse()
{
    gets(pre), gets(in);
    int length = strlen(pre);
    PostTraverseTree(pre, in, length, length - 1);
    puts(post);
}

int BiTreeDepth(BiTree T)
{
    if (!T)
        return 0;
    int m = BiTreeDepth(T->lchild);
    int n = BiTreeDepth(T->rchild);
    if (m > n)
        return (m + 1);
    else
        return (n + 1);
}

void BiTreeDiameter()
{
    int n;
    scanf("%d", &n);             //输入节点个数
    BiTree Forest[n + 1];        //使用Forest数组存储n个节点,在输入具体关系后将其连接为一棵二叉树
    for (int i = 1; i <= n; i++) //初始化n个节点
    {
        Forest[i] = new BiTNode;
        Forest[i]->lchild = Forest[i]->rchild = NULL;
    }
    for (int i = 1; i < n; i++) //构建二叉树
    {
        int parent, child;
        scanf("%d %d", &parent, &child);
        if (!Forest[parent]->lchild)
            Forest[parent]->lchild = Forest[child];
        else
            Forest[parent]->rchild = Forest[child];
    }
    int max = 0;
    for (int i = 1; i <= n; i++) //分别考察以每一个节点为根节点的二叉树的任意两个子孙节点之间的距离的最大值,取其中的最大值为整个二叉树的直径
    {
        int Diameter = BiTreeDepth(Forest[i]->lchild) + BiTreeDepth(Forest[i]->rchild);
        if (Diameter > max)
            max = Diameter;
    } //左子树深度代表左侧节点到根节点的距离最大值,右子树深度代表右侧节点到根节点的距离最大值,
    printf("%d", max);
}

void CopyBiTree(BiTree T, BiTree &NewBiTree) //复制二叉树T到NewBiTree
{
    if (!T)
    {
        NewBiTree = NULL;
        return;
    }
    else
    {
        NewBiTree = new BiTNode;
        NewBiTree->data = T->data;
        CopyBiTree(T->lchild, NewBiTree->lchild);
        CopyBiTree(T->rchild, NewBiTree->rchild);
    }
}

int main()
{
    /*SCAU 8606
    BiTree T;
    CreateBiTree(T);
    PreOrderTraverse(T);
    printf("\n");
    InOrderTraverse(T);
    printf("\n");
    PostOrderTraverse(T);
    */
    /*SCAU 17121
    BiTree T;
    CreateBiTree(T);
    Count(T);
    printf("%d\n%d\n%d", n2, n1, n0);
    */
    /*SCAU 18924
    BiTreeBreadth();
    */
    /*SCAU 18724
    BiTreeTraverse();
    */
    /*SCAU 18923
    BiTreeDiameter();
    */
    return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值