zoukankan      html  css  js  c++  java
  • 数据结构

    Description

    一棵树采用孩子兄弟表示法存储,每个结点的值为一个字母,要求:
    (1)编写算法,输入该树对应的二叉树的先序序列建立二叉链表;
    (2)统计并输出该树的叶子结点数。
    (3)输出根结点的度。(注意:不是树的度)

    Input

    该树对应的二叉树的先序序列,空指针位置输入#

    Output

     叶子点树和根结点的度,以空格分隔。

    Sample Input

    AB#CD##E###
    

    Sample Output

    3 3

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

    /*
     *writer :J
     *woker :J_1
     */
    typedef struct CsNode{        //结构体
        char date;
        struct Node * firstChild,* nextSibling;
    }CsNode, *CsTree;
    CsNode* CreateTree()
    {
        CsNode* bt = NULL;
        char ch;
        ch = getchar();
        if(ch != '#')
        {
            bt = (CsNode*)malloc(sizeof(CsNode));
            bt->date = ch;
            bt->firstChild = CreateTree();
            bt->nextSibling = CreateTree();
        }
        return bt;
    }
    void Inorder(CsNode* head,int *count)
    {
        if(head != NULL)
        {
            if(head->firstChild == NULL)
            {
                (*count)++;
            }
            Inorder(head->firstChild,count);
            Inorder(head->nextSibling,count);
        }
    }
    void Rootdu(CsNode *head,int *num)
    {
        head = head->firstChild;
        while(head)
        {
            (*num)++;
            head = head->nextSibling;
        }
    }
    int main(int argc, char *argv[])
    {
        int count = 0;
        CsNode* head;
        head = CreateTree();
        Inorder(head,&count);
        //printf("%d ",count);
        int num = 0;
        Rootdu(head,&num);
        printf("%d %d",count,num);
        return 0;
    }

    在数据结构的这个题目里的,遍历选用了递归的算法。

    在遍历的过程中,采用先序中序后序都可以进行运算的到。

    关键是采用的数据的算法。

  • 相关阅读:
    U盘分区 将一个u盘分为3个区
    InnoDB索引最通俗的解释
    Centos7 安全加固
    final/static
    Java继承,方法重写
    UnrealEngine4血溅效果
    UnrealEngine4第一人称射击游戏之触碰掉血与掉盔甲功能实现
    UnrealEngine4第一人称射击游戏UI
    String字符串
    构造方法
  • 原文地址:https://www.cnblogs.com/jiang-bei/p/10115201.html
Copyright © 2011-2022 走看看