【数据结构oj】树的度(树和二叉树的相互转化)

简介: 【数据结构oj】树的度(树和二叉树的相互转化)

e2e49c5493f2bf1706c67e06d97b3815_b753d377208e6d06bdc781838fc9a8d7.png

输入:ABC##DE#F#G####

输出:3

dce427095fa5dbe1f94e070a4d301639_1b268b61416a41fbbf428c7e39cebee1.png


不难看出,

以二叉树方式存储的树,二叉树结点a的左孩子就是树结点a的左孩子,而二叉树a的右孩子是树节点的a的兄弟,既a父节点f_a的某个孩子(非长子)

那么,可以通过遍历树的每个结点,编写算法,计算出每个结点的度(既树的孩子数目),找出其中最大的度,作为该树的度输出。


第一版代码


#include <stdio.h>
#include <stdlib.h>
int Max_degree = 0;
typedef struct node{
    char data;
    int degree;
    struct node *lchild,*rchild;
}BinTree;
/*---创建二叉树---*/
BinTree *CreateBinTree()
{/*先序建立一个二叉树链表*/
    BinTree *bt = NULL;
    char ch;
    ch = getchar();
    if(ch!='#')
    {
        bt = (BinTree *)malloc(sizeof(BinTree));
        bt->data = ch;
        bt->lchild = CreateBinTree();
        bt->rchild = CreateBinTree();
    }
    return bt;
}
int getpiontDegree(BinTree *bin)
{
    /*遍历找到一个不为空的节点,先判断它是否有左孩子(树的结点是否有孩子),然后计算左孩子的右孩子数目(树结点长子的兄弟数)*/
    if(bin!=NULL)
    {
        if(bin->lchild != NULL)
        {
            BinTree *p = bin->lchild;
            int n = 1;
            while(p->rchild !=NULL)
            {
                p= p->rchild;
                n++;
            }
            bin->degree = n;
        }
        getpiontDegree(bin->lchild);
        getpiontDegree(bin->rchild);
    }
}
void  firstprintfBinTreeDegree(BinTree *bt)
{
    int a;
    /*遍历找到一个非空结点,将该结点与已经存储的Max_degree比较大小,找出最大度的结点*/
    if(bt != NULL)
    {
        if(bt->lchild)
        {
            if(Max_degree<bt->degree)
            {
                Max_degree = bt->degree;
            }
        }
        firstprintfBinTreeDegree(bt->lchild);
        firstprintfBinTreeDegree(bt->rchild);
    }
}
int main()
{
    BinTree *bin;
    bin = CreateBinTree();
    getpiontDegree(bin);
    firstprintfBinTreeDegree(bin);
    printf("%d",Max_degree);
}


第二版代码


在getpiontDegree()函数中遍历每个结点,计算出每个结点的度,然后又在firstprintfBinTreeDegree()函数中遍历每个结点,找出其中度最大的结点,其中每个结点被重复遍历了两次,浪费了时间。我们完全可以利用firstprintfBinTreeDegree()中的遍历进行同时进行这两个操作,getpiontDegree()函数在firstprintfBinTreeDegree()函数中被调用,用来计算该循环下的每一个结点的度


给出代码

#include <stdio.h>
#include <stdlib.h>
typedef char dataType;
int Max_degree = 0;
typedef struct node{
    dataType data;
    int degree;
    struct node *lchild,*rchild;
}BinTree;
/*---创建二叉树---*/
BinTree *CreateBinTree()
{/*先序建立一个二叉树链表*/
    BinTree *bt = NULL;
    char ch;
    ch = getchar();
    if(ch!='#')
    {
        bt = (BinTree *)malloc(sizeof(BinTree));
        bt->data = ch;
        bt->lchild = CreateBinTree();
        bt->rchild = CreateBinTree();
    }
    return bt;
}
int getpointDegree(BinTree *bin)
{
    if(bin->lchild!=NULL)
    {
        int degree = 1;
        BinTree *p = bin->lchild;
       while(p->rchild!=NULL)
       {
           degree++;
           p = p->rchild;
       }
       return degree;
    }
    return 0;
}
void  firstprintfBinTreeDegree(BinTree *bt)
{
    if(bt != NULL)
    {/*---先序二叉树遍历---*/
        bt->degree = getpointDegree(bt);
        //printf("%c:%d ",bt->data,bt->degree);
        if(bt->degree > Max_degree)
        {
            Max_degree = bt->degree;
        }
        firstprintfBinTreeDegree(bt->lchild);
        firstprintfBinTreeDegree(bt->rchild);
    }
}
int main()
{
    BinTree *bin;
    bin = CreateBinTree();
    firstprintfBinTreeDegree(bin);
    printf("%d",Max_degree);
    return 0;
}

image.pngimage.png

ps:第一次做这个题,思路快就有了,但是代码总是打不出来。第一次尝试用的迭代,想不出来怎么把每次的度和迭代的跳出结合起来,代码乱套了。最后做出了来,oj还是不给ac,没弄明白,回来又把代码写了一遍,这次居然过了,真神奇


相关文章
|
27天前
|
算法
【算法与数据结构】二叉树(前中后)序遍历2
【算法与数据结构】二叉树(前中后)序遍历
|
4天前
|
算法
数据结构与算法-AVL树入门
数据结构与算法-AVL树入门
8 0
|
4天前
|
算法
数据结构与算法-Trie树添加与搜索
数据结构与算法-Trie树添加与搜索
5 0
|
12天前
二叉树和数据结构
二叉树和数据结构
18 0
|
13天前
|
算法 DataX
二叉树(中)+Leetcode每日一题——“数据结构与算法”“剑指Offer55-I. 二叉树的深度”“100.相同的树”“965.单值二叉树”
二叉树(中)+Leetcode每日一题——“数据结构与算法”“剑指Offer55-I. 二叉树的深度”“100.相同的树”“965.单值二叉树”
|
23天前
|
算法 索引
【算法与数据结构】深入二叉树实现超详解(全源码优化)
【算法与数据结构】深入二叉树实现超详解(全源码优化)
|
23天前
|
存储 算法
【算法与数据结构】深入解析二叉树(二)之堆结构实现
【算法与数据结构】深入解析二叉树(二)之堆结构实现
|
2月前
|
存储 算法 程序员
【数据结构】【版本2.0】【树形深渊】——二叉树入侵
【数据结构】【版本2.0】【树形深渊】——二叉树入侵
|
2月前
|
算法 C++ 开发者
【C/C++ 数据结构 】二叉树基本性质:具有n个结点的完全二叉树的深度为[log2n]+1或者[log2(n+1)]...
【C/C++ 数据结构 】二叉树基本性质:具有n个结点的完全二叉树的深度为[log2n]+1或者[log2(n+1)]...
13 0
|
2月前
|
存储 算法 数据库
【C/C++ 数据结构 】树的 四种表示方法
【C/C++ 数据结构 】树的 四种表示方法
31 0