[Data structure oj] Degree of tree (mutual transformation between tree and binary tree)

image
Input: ABC##DE#F#G####
Output: 3

image

 

it's easy to see,

For a tree stored in the form of a binary tree, the left child of the binary tree node a is the left child of the tree node a, and the right child of the binary tree a is the brother of the tree node a, which is a child of the parent node f_a of a (not the eldest child)

Then, by traversing each node of the tree, write an algorithm to calculate the degree of each node (that is, the number of children of the tree), find the largest degree among them, and output it as the degree of the tree.

first version code

#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);

}


version 2 code

Traverse each node in the getpiontDegree() function, calculate the degree of each node, and then traverse each node in the firstprintfBinTreeDegree() function to find the node with the largest degree, and each node is traversed twice, wasting time. We can use the traversal in firstprintfBinTreeDegree() to perform these two operations at the same time. The getpiontDegree() function is called in the firstprintfBinTreeDegree() function to calculate the degree of each node under the loop

give the code

 

#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
image

ps: This is the first time I do this question, I have an idea soon, but the code can't be typed out. The iteration I tried for the first time, I couldn't figure out how to combine the speed of each iteration with the jump out of the iteration, and the code was messed up. Finally made it, oj still didn’t give it to ac, didn’t figure it out, came back and wrote the code again, this time it passed, it’s amazing

Guess you like

Origin blog.csdn.net/qq_25218219/article/details/121339269