zoukankan      html  css  js  c++  java
  • 数据结构与算法——数组转化为Tree数据结构(C#)

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

        public class TreeNode
        {
            public int val;
            public TreeNode left;
            public TreeNode right;
            public TreeNode(int x)
            {
                val = x;
            }
        }
    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;
            }
        }
    }

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

  • 相关阅读:
    A1143. Lowest Common Ancestor
    三个loading小动画实例
    CSS之圣杯布局与双飞翼布局
    sublime个人快捷键
    响应式之表格
    CSS之column语法
    使用column简单实现瀑布流效果
    Flex 布局教程:实例篇(转)
    Flex 布局教程:语法篇(转)
    简单实现瀑布流效果
  • 原文地址:https://www.cnblogs.com/Arvin-zhang/p/12665673.html
Copyright © 2011-2022 走看看