欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

数据结构与算法——Tree数据结构的生成(C#)

程序员文章站 2023-12-31 15:34:58
在做剑指offer的题目时,发现每次需要用到树做为参数来测试自己写的方法时,都很是痛苦。于是乎就萌生了写一个利用数据生成树的方法。简单测试了下,没什么毛病。在这里贴出来,如果以后发现有bug了会持续在后面更新,也欢迎大家指出其中的不足。 using System; using System.Coll ......

在做剑指offer的题目时,发现每次需要用到树做为参数来测试自己写的方法时,都很是痛苦。于是乎就萌生了写一个利用数据生成树的方法。简单测试了下,没什么毛病。在这里贴出来,如果以后发现有bug了会持续在后面更新,也欢迎大家指出其中的不足。

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;

namespace offertest.helper
{
    class treehelper
    {
        /// <summary>
        /// 根据数组按顺序从上到下生成树
        /// </summary>
        /// <param name="arraytree">生成树的节点数据,无节点的位置需要设置为null,一个有值的节点必须设置两个左右子节点。</param>
        /// <returns></returns>
        public static treenode createtreebyarry(int?[] arraytree)
        {
            queue<treenode> treenodes = new queue<treenode>();
            if(arraytree==null ||!arraytree[0].hasvalue)
            {
                throw new argumentexception("生成树的根节点不能为null");
            }
            var root = new treenode(arraytree[0].value);
            treenodes.enqueue(root);
            createtree(arraytree, 1, treenodes);
            return root;
        }

        /// <summary>
        /// 为父节点赋左右子节点值,null父节点不操作。
        /// </summary>
        /// <param name="arraytree">数据源</param>
        /// <param name="startindex">开始填充的值的索引</param>
        /// <param name="qroot">需要赋子节点的父节点队列</param>
        /// <returns></returns>
        public static bool createtree(int?[] arraytree, int startindex, queue<treenode> qroot)
        {
            if (startindex > 0 && arraytree.count() > startindex)
            {
                queue<treenode> treenodes = new queue<treenode>();
                foreach(var root in qroot)
                {
                    //为null代表该节点没有
                    if (root == null)
                        continue;
                    if(arraytree.count() > startindex)
                    {
                        if (arraytree[startindex].hasvalue)
                            root.left = new treenode(arraytree[startindex].value);
                        else
                            root.left = null;
                    }
                    if (arraytree.count() > startindex + 1)
                    {
                        if (arraytree[++startindex].hasvalue)
                            root.right = new treenode(arraytree[startindex].value);
                        else
                            root.right = null;
                    }
                    else
                        return false;
                    //将父节点放入队列,
                    treenodes.enqueue(root.left);
                    treenodes.enqueue(root.right);
                    startindex += 1;
                }
                return !createtree(arraytree, startindex, treenodes);
            }
            else
                return false;
        }
    }
}

怎么使用呢?一张自画图体会一下

数据结构与算法——Tree数据结构的生成(C#)

上一篇:

下一篇: