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;
    }

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

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

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

  • 相关阅读:
    spring 配置多个properties
    redis 一些使用过的命令
    ftpclient 遇到的一些问题
    tomcat+nginx 横向扩展
    easyui 刷新页面
    tomcat 发布本地文件
    java httpclient 跳过证书验证
    https增加临时证书,tomcat配置
    echarts getAttribute”的值: 对象为 null 或未定义 错误解决方法,
    数据的导入导出
  • 原文地址:https://www.cnblogs.com/jiang-bei/p/10115201.html
Copyright © 2011-2022 走看看