zoukankan      html  css  js  c++  java
  • C语言实现二叉树中统计叶子结点的个数&度为1&度为2的结点个数

    算法思想

    统计二叉树中叶子结点的个数和度为1、度为2的结点个数,因此可以参照二叉树三种遍历算法(先序、中序、后序)中的任何一种去完成,只需将访问操作具体变为判断是否为叶子结点和度为1、度为2的结点及统计操作即可。

    Code

    #include <stdio.h>
    #include <stdlib.h>
    
    int LeafCount=0;
    int Degree1Count=0;
    int Degree2Count=0;
    
    typedef char DataType;             //二叉链表结点的数据类型 
    
    typedef struct Node               //定义二叉树的二叉链表结点结构 
    {
        DataType data;
        struct Node *LChild;         //左子树 
        struct Node *RChild;         //右子树     
    }BiTNode,*BiTree;
    
    void CreateBiTree(BiTree *bt);   //创建二叉链表函数 
    void PreOrder(BiTree root);      //先序遍历二叉树
    void InOrder(BiTree root);       //中序遍历二叉树
    void PostOrder(BiTree root);     //后序遍历二叉树
    void Leaf(BiTree root);          //统计叶子结点数目 
    void Degree1(BiTree root);        //统计度为1的结点数目
    void Degree2(BiTree root);        //统计度为2的结点数目
    
    int main()
    {
        BiTree bt;
        int choice;
        while(true)
        {                   //二叉树操作选择菜单 
        printf("*****************Please enter your choice*****************
    
    ");
        printf("                choice 1:创建二叉树
    ");
            printf("                choice 2:先序遍历二叉树
    ");
            printf("                choice 3:中序遍历二叉树
    ");
            printf("                choice 4:后序遍历二叉树
    ");
            printf("                choice 5:打印叶子结点数目
    ");
            printf("                choice 6:打印度为1的结点数目
    ");
            printf("                choice 7:打印度为2的结点数目
    ");
            printf("                choice 0:退出
    
    ");
            scanf("%d",&choice);
            switch(choice)
            {
                case 1: CreateBiTree(&bt);
                        break;
                case 2: PreOrder(bt);             
                          printf("
    ");
                           break;
    
                case 3: InOrder(bt);               
                        printf("
    ");
                           break;
                case 4: PostOrder(bt);                
                        printf("
    ");
                           break;
                case 5: Leaf(bt);             
                          printf("该二叉树叶子结点的数目为:%d
    ",LeafCount);
                           break;
                case 6: Degree1(bt);               
                        printf("该二叉树度为1的结点数目为:%d
    ",Degree1Count);
                           break;
                case 7: Degree2(bt);                
                        printf("该二叉树度为2的结点数目为:%d
    ",Degree2Count);
                           break;
                case 0: exit(0);                
                        break;
                default:
                    printf("ERROR!!
    ");
                    exit(0);
                    break;
            }        
        }
        return 0; 
    } 
    
    void CreateBiTree(BiTree *bt)
    {
        char ch;
        printf("Please enter data:");
        getchar();
        ch = getchar();
        if(ch == '.')              //读入的数据是'.'则将当前树根置为空 
        {
            *bt = NULL;
        }
        else                      //读入正常数据,为当前树根分配地址空间 
        {
            *bt = (BiTree)malloc(sizeof(BiTNode));
            (*bt)->data = ch;
            CreateBiTree(&((*bt)->LChild)); //递归调用CreateBiTree()函数,处理左子树 
            CreateBiTree(&((*bt)->RChild)); //递归调用CreateBiTree()函数,处理右子树 
        }
    }
    
    void PreOrder(BiTree root) //先序遍历二叉树,root为指向二叉树根结点的指针 
    {
        if(root!=NULL)    
        {
            printf("%c ",root->data);      //访问根结点 
            PreOrder(root->LChild);        //先序遍历左子树 
            PreOrder(root->RChild);        //先序遍历右子树
        }
    }
    
    void InOrder(BiTree root)    //中序遍历二叉树,root为指向二叉树根结点的指针
    { 
        if(root!=NULL)    
        {
            InOrder(root->LChild);      //中序遍历左子树
            printf("%c ",root->data);      //访问根结点 
            InOrder(root->RChild);      //中序遍历右子树
        }
    }
    
    void PostOrder(BiTree root)     //中序遍历二叉树,root为指向二叉树根结点的指针
    { 
        if(root!=NULL)             
        {
            PostOrder(root->LChild);      //后序遍历左子树
            PostOrder(root->RChild);      //后序遍历右子树
            printf("%c ",root->data);      //访问根结点 
        }
    }
    
    void Leaf(BiTree root)
    {
        if(root!=NULL)
        {
            Leaf(root->LChild);
            Leaf(root->RChild);
            if(root->LChild==NULL && root->RChild==NULL)
            {
                LeafCount++;   //统计叶子结点数目
            }
        }
    } 
    
    void Degree1(BiTree root)
    {
        if(root!=NULL)
        {
            Degree1(root->LChild);
            Degree1(root->RChild);
            if((root->LChild==NULL && root->RChild!=NULL)||(root->LChild!=NULL && root->RChild==NULL))
            {
                Degree1Count++;   //统计度为1的结点数目
            }
        }
    } 
    
    void Degree2(BiTree root)
    {
        if(root!=NULL)
        {
            Degree2(root->LChild);
            Degree2(root->RChild);
            if(root->LChild!=NULL && root->RChild!=NULL)
            {
                Degree2Count++;   //统计度为2的结点数目
            }
        }
    } 
     
  • 相关阅读:
    PHP 中各种命名规则的总结
    Linux 下安装mysql 8.0.11(CentOS 7.4 系统)
    常用端口号(转)
    Centos7 FPM 安装mysql8
    windows 下升级安装mysql8,与旧版本5.6共存
    【Eclipse】修改项目访问名称
    【Eclipse】报错提示删掉@Override
    【霓虹语】バスの火事
    【博客园】使用ifream的两种方法
    【Eclipse】调试java程序的九个技巧
  • 原文地址:https://www.cnblogs.com/qftm/p/10317159.html
Copyright © 2011-2022 走看看