Exemple #1
0
        /// <summary>
        /// 添加子对象
        /// </summary>
        /// <param name="child"></param>
        public TreeEx <T> AddChild(T child)
        {
            var surezen = new TreeEx <T>(child)
            {
                Deep = this.Deep + 1
            };

            if (this.Childrens == null)
            {
                this.Childrens = new List <TreeEx <T> >()
                {
                    surezen
                };
            }
            else
            {
                //排序插入
                bool isInsert = false;
                for (int i = 0; i < this.Childrens.Count; i++)
                {
                    if (child.CompareTo(this.Childrens[i].Value) >= 0)
                    {
                        this.Childrens.Insert(i, surezen);
                        isInsert = true;
                        break;
                    }
                }
                if (!isInsert)
                {
                    this.Childrens.Add(surezen);
                }
            }
            return(surezen);
        }
Exemple #2
0
        private static void FromListIterator(TreeEx <T> node, IList <T> list, Func <T, string> keySelector, Func <T, string> parentKeySelector)
        {
            string parentKey = keySelector(node.Value);

            for (int i = 0; i < list.Count; i++)
            {
                if (parentKeySelector(list[i]) == parentKey)
                {
                    var subNode = node.AddChild(list[i]);
                    FromListIterator(subNode, list, keySelector, parentKeySelector);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 从列表
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static TreeEx <T> FromList(IList <T> list, Func <T, string> keySelector, Func <T, string> parentKeySelector)
        {
            TreeEx <T> tree = new TreeEx <T>();

            if (list == null || list.Count <= 0)
            {
                return(tree);
            }
            if (keySelector == null || parentKeySelector == null)
            {
                throw new ArgumentNullException("KEY选择器和父键选择器不能为空");
            }


            FromListIterator(tree, list, keySelector, parentKeySelector);
            return(tree);
        }