美文网首页
统计二叉树叶子结点的数目

统计二叉树叶子结点的数目

作者: 周末的游戏之旅 | 来源:发表于2019-08-03 10:39 被阅读0次

算法分析

统计叶子结点数目和输出叶子结点类似,不过是把输出换成累加。设计一个全局变量即可。

算法实现

TreeNode

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tree
{
    class TreeNode<T>
    {
        T data;
        TreeNode<T> LChrild;
        TreeNode<T> RChirld;
        
        public T Data { get => data; set => data = value; }
        internal TreeNode<T> LChrild1 { get => LChrild; set => LChrild = value; }
        internal TreeNode<T> RChirld1 { get => RChirld; set => RChirld = value; }

        public TreeNode(T data)
        {
            this.Data = data;
        }
    }
}

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tree
{
    class Program
    {
        static int total = 0; //叶子结点数目

        static void Main(string[] args)
        {
            TreeNode<string> A = new TreeNode<string>("a");
            TreeNode<string> B = new TreeNode<string>("b");
            TreeNode<string> C = new TreeNode<string>("c");
            TreeNode<string> D = new TreeNode<string>("d");
            TreeNode<string> E = new TreeNode<string>("e");
            TreeNode<string> F = new TreeNode<string>("f");
            TreeNode<string> G = new TreeNode<string>("g");
            TreeNode<string> H = new TreeNode<string>("h");

            A.LChrild1 = B;
            B.RChirld1 = D;
            A.RChirld1 = C;
            D.LChrild1 = F;
            D.RChirld1 = G;
            C.RChirld1 = E;
            E.RChirld1 = H;

            Leef(A);
            Console.WriteLine(total);
        }

        /// <summary>
        /// 统计叶子结点数目,全局变量法
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        static void Leef(TreeNode<string> node)
        {
            if (node != null)
            {
                if (node.LChrild1 == null && node.RChirld1 == null)
                    ++total;
                Leef(node.LChrild1);
                Leef(node.RChirld1);
            }
        }
    }
}

思考

虽然用全局变量累加的方法统计叶子结点的数量实现起来很容易,不过这样做会加大程序间的耦合度。所以要换一种方法,最好是只需要调用一个函数就可以得到叶子结点数量,而函数所需要的条件都在函数内实现,从而加大其内聚度。
针对这个问题,可以换一种思路,统计叶子结点的数量,就是统计根节点左边的叶子结点数量和根节点右边的叶子结点数量。这样就会发现跟斐波那契题类似,斐波那契的解决思路是第i个数等于第i-1个数加上第i-2个数。
这样就把求树的叶子结点的大问题,分解成了求每个结点的左孩子的叶子节点数量和右孩子叶子结点数量的小问题。

实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tree
{
    class TreeNode<T>
    {
        T data;
        TreeNode<T> LChrild;
        TreeNode<T> RChirld;
        
        public T Data { get => data; set => data = value; }
        internal TreeNode<T> LChrild1 { get => LChrild; set => LChrild = value; }
        internal TreeNode<T> RChirld1 { get => RChirld; set => RChirld = value; }

        public TreeNode(T data)
        {
            this.Data = data;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tree
{
    class Program
    {
        static void Main(string[] args)
        {
            TreeNode<string> A = new TreeNode<string>("a");
            TreeNode<string> B = new TreeNode<string>("b");
            TreeNode<string> C = new TreeNode<string>("c");
            TreeNode<string> D = new TreeNode<string>("d");
            TreeNode<string> E = new TreeNode<string>("e");
            TreeNode<string> F = new TreeNode<string>("f");
            TreeNode<string> G = new TreeNode<string>("g");
            TreeNode<string> H = new TreeNode<string>("h");

            A.LChrild1 = B;
            B.RChirld1 = D;
            A.RChirld1 = C;
            D.LChrild1 = F;
            D.RChirld1 = G;
            C.RChirld1 = E;
            E.RChirld1 = H;

            Console.WriteLine(Leef(A););
        }

        /// <summary>
        /// 统计叶子结点的数目
        /// /*采用递归算法,如果是空树,返回0;如果只有一个结点,返回1;否则为左右子树的叶子结点数之和*/
        /// </summary>
        /// <param name="node"></param>
        ///// <returns></returns>
        static int Leef(TreeNode<string> node)
        {
            //特殊情况
            if (node == null)
            {
                return 0;
            }
            //递归结束点
            else if (node.LChrild1 == null && node.RChirld1 == null)
            {
                return 1;
            }
            //递归过程
            else
            {
                return Leef(node.LChrild1) + Leef(node.RChirld1);
            }
        }
    }
}

相关文章

  • 统计二叉树叶子结点的数目

    算法分析 统计叶子结点数目和输出叶子结点类似,不过是把输出换成累加。设计一个全局变量即可。 算法实现 TreeNo...

  • 65_二叉树中属性操作的实现

    关键词:二叉树中结点的数目、二叉树的高度、二叉树的度树 0. 二叉树中结点的数目 定义功能函数count(node...

  • 二叉树入门

    树的常用名词 度:结点拥有子结点的数目 孩子节点:结点的子结点 双亲结点:结点的父结点 叶子结点:度为0的结点 分...

  • 4.4 哈夫曼树和哈夫曼编码

    1.带权路径长度(WPL): 设二叉树有n个叶子结点,每个叶子结点带有权值 wk,从根结点到每个叶子结点的长度为 ...

  • 数据结构题目37:求该二叉树中叶结点的数目

    题目:已知二叉树采用二叉链表存储结构,根结点所在链结点的地址为T,写一递归算法,求该二叉树中叶结点的数目。 具体算...

  • 数据结构笔记(树->哈夫曼树)

    带权路径长度(WPL):设二叉树有N个叶子结点,每个叶子结点带有权值Wk,从根节点到每个叶子结点的长度为lk,则每...

  • 堆排序

    对于一个完全二叉树来说,如果所有的结点(叶子结点除外)的值都大于其左右孩子结点的值,那么这个完全二叉树就被成为一个...

  • 第十五讲 数据结构之二叉树(三)

    线索二叉树 产生背景 现有一棵结点数目为n的二叉树,采用二叉链表的形式存储。对于每个结点均有指向左右孩子的两个指针...

  • 二叉树的最小深度

    求给定二叉树的最小深度。最小深度是指树的根结点到最近叶子结点的最短路径上结点的数量。

  • 几种特殊的二叉树

    二叉树:有序树,左右孩子不能颠倒 1、满二叉树:对于h层的结点有2^h-1个结点。叶子结点都集中在最下面一层,除了...

网友评论

      本文标题:统计二叉树叶子结点的数目

      本文链接:https://www.haomeiwen.com/subject/eetjdctx.html