集合中各元素任意组合的枚举方法 2

public void test03()
        {
            ConsoleApplication1.TreeLayoutClass tree = new ConsoleApplication1.TreeLayoutClass();
            List<string> qtys = new List<string>();
            qtys.Add("A");
            qtys.Add("B");
            qtys.Add("C");
            qtys.Add("D");
            qtys.Add("E");
            qtys.Add("F");
            //qtys.Add("3210");
            //qtys.Add("500");
            //qtys.Add("600");
            //qtys.Add("800");
            //qtys.Add("900");
            //lis
            List<string> list = tree.Get_EnumList2(qtys);
            foreach (string s in list)
                Console.WriteLine(s);
            Console.WriteLine(list.Count +"--------------------------------------------");
}

 

using System;
using System.Collections;
using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1
{
    public class TreeLayoutClass
    {
        /// <summary>
        /// 取所有的排列组合方式
        /// </summary>
        /// <param name="strList"></param>
        /// <returns></returns>
        public List<string> Get_EnumList2(List<string> strList)
        {
            List<string> result = new List<string>();
            result.Add(string.Join("+", strList.ToArray()));
            LayoutTreeNode rootNode = new LayoutTreeNode(
            
                strList[0],
                null,
                new List<LayoutTreeNode>()
            );
            //}
            Stack<LayoutTreeNode> stack = new Stack<LayoutTreeNode>();
            stack.Push(rootNode);

            //构建树
            while (stack.Count > 0)
            {
                LayoutTreeNode node = stack.Pop();
                //顶级
                if (node.Parent == null)
                {
                    if (node.ChildNodes == null)
                        node.ChildNodes = new List<LayoutTreeNode>();
                    int partIndex = strList.IndexOf(node.N);
                    //List<string> mySubStr = strList.FindAll(s => s != node.N);

                    for (int i = 0; i < strList.Count; i++)
                    {
                        if (i == partIndex)
                            continue;
                        string s = strList[i];
                        LayoutTreeNode subNode = new LayoutTreeNode
                        (
                            s,
                             node,
                             new List<LayoutTreeNode>(),
                             node.ChildNodes.Count
                        );
                        node.ChildNodes.Add(subNode);
                        stack.Push(subNode);
                    }
                }
                else
                {
                    
                    if (node.Parent.ChildNodes != null && node.Parent.ChildNodes.Count > 0)
                    {
                        for (int i = 0; i < node.Parent.ChildNodes.Count; i++)
                        {
                            if (i == node.PartIndex)
                                continue;
                            LayoutTreeNode n = node.Parent.ChildNodes[i];
                            LayoutTreeNode subNode = new LayoutTreeNode
                            (
                                n.N,
                                 node,
                                  new List<LayoutTreeNode>(),
                                  node.ChildNodes.Count
                            );
                            node.ChildNodes.Add(subNode);
                            stack.Push(subNode);
                        }
                    }
                    
                }
            }

            //遍历树
            stack = new Stack<LayoutTreeNode>();
            stack.Push(rootNode);
            while (stack.Count > 0)
            {
                LayoutTreeNode node = stack.Pop();
                if (node.ChildNodes != null && node.ChildNodes.Count > 0)
                {
                    //剔除重复的节点
                    if (!CheckNodePathExistLevel2Full(node))
                    {
                        result.Add(GetNodePath(node, "+"));
                        if (node.ChildNodes.Count > 1)
                            result.Add(GetNodePath(node, ","));
                        for (int i = 0; i < node.ChildNodes.Count; i++)
                        {
                            stack.Push(node.ChildNodes[i]);
                        }
                    }
                    
                }

            }

            return result;
        }
    
        private string GetNodeParentPath(LayoutTreeNode node)
        {
            string path = "";
            if (node.Parent != null)
            {
                Stack<LayoutTreeNode> stack = new Stack<LayoutTreeNode>();

                stack.Push(node);
                while (stack.Count > 0)
                {
                    LayoutTreeNode n = stack.Pop();
                    if (n != null)
                    {
                        if (!string.IsNullOrEmpty(path))
                        {
                            path = "+" + path;
                        }
                        path = n.N + path;
                        if (n.Parent != null)
                            stack.Push(n.Parent);
                    }
                }
            }
            else
            {
                path = node.N;
            }
            return path;
        }
        /// <summary>
        /// 取节点路径
        /// </summary>
        /// <param name="node"></param>
        /// <param name="split"></param>
        /// <returns></returns>
        private string GetNodePath(LayoutTreeNode node, string split)
        {
            string path = "";
            if (node.Parent != null)
            {
                Stack<LayoutTreeNode> stack = new Stack<LayoutTreeNode>();
                stack.Push(node);
                while (stack.Count > 0)
                {
                    LayoutTreeNode n = stack.Pop();
                    if (n != null)
                    {

                        if (!string.IsNullOrEmpty(path))
                        {
                            path = "+" + path;
                        }
                        path = n.N + path;
                        if (n.Parent != null)
                            stack.Push(n.Parent);
                    }
                }
            }
            else
            {
                path = node.N;
            }
            if (node.ChildNodes != null && node.ChildNodes.Count > 0)
            {
                return path + "," + GetNodeChildPath(node, split);
            }
            else
                return path;
        }

        /// <summary>
        /// 取子节点路径
        /// </summary>
        /// <param name="node"></param>
        /// <param name="split"></param>
        /// <returns></returns>
        private string GetNodeChildPath(LayoutTreeNode node, string split)
        {
            // if (node.ChildNodes != null && node.ChildNodes.Count > 0) {
            string path = "";
            for (int i = 0; i < node.ChildNodes.Count; i++)
            {
                if (!string.IsNullOrEmpty(path))
                {
                    path += split;
                }
                path += node.ChildNodes[i].N;
            }
            return path;
            //}
            //return string.Empty;
        }

        /// <summary>
        /// 检测路径是否在前面一级存在
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private bool CheckNodePathExist(LayoutTreeNode node)
        {


            if (node.Parent != null)
            {
                string path = node.N + node.Parent.N;
                //int index = node.Parent.ChildNodes.IndexOf(node);
                for (int i = 0; i < node.PartIndex; i++)
                {
                    LayoutTreeNode bn = node.Parent.ChildNodes[i];
                    foreach (LayoutTreeNode n2 in bn.ChildNodes)
                    {
                        if (path == bn.N + n2.N)
                            return true;
                    }
                }
 
            }
            return false;
        }

        /// <summary>
        /// 检测路径上一级两级节点是否存在
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool CheckNodePathExistLevel2Full(LayoutTreeNode node)
        {


            if (node.Parent != null)
            {
                string path = node.N + node.Parent.N;
                //int index = node.Parent.ChildNodes.IndexOf(node);
                for (int i = 0; i < node.PartIndex; i++)
                {
                    LayoutTreeNode bn = node.Parent.ChildNodes[i];
                    foreach (LayoutTreeNode n2 in bn.ChildNodes)
                    {
                        if (path == bn.N + n2.N)
                            return true;
                    }
                }

                if (node.Parent.Parent != null)
                {
                    //index = node.Parent.Parent.ChildNodes.IndexOf(node.Parent);
                    for (int i = 0; i < node.Parent.PartIndex; i++)
                    {
                        LayoutTreeNode bn = node.Parent.Parent.ChildNodes[i];
                        foreach (LayoutTreeNode n2 in bn.ChildNodes)
                        {
                            if (path == bn.N + n2.N)
                                return true;
                        }
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 检查节点路径是否存在, 只检查上两级
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private bool CheckNodePathExistLevel2(LayoutTreeNode node)
        {
            if (node.Parent != null)
            {
                string path = node.N + node.Parent.N;
                //int index = node.Parent.ChildNodes.IndexOf(node);
                //for (int i = 0; i < index; i++)
                //{
                //    LayoutTreeNode bn = node.Parent.ChildNodes[i];
                //    foreach (LayoutTreeNode n2 in bn.ChildNodes)
                //    {
                //        if (path == bn.N + n2.N)
                //            return true;
                //    }
                //}

                if (node.Parent.Parent != null)
                {
                   // int index = node.Parent.Parent.ChildNodes.IndexOf(node.Parent);
                    for (int i = 0; i < node.Parent.PartIndex; i++)
                    {
                        LayoutTreeNode bn = node.Parent.Parent.ChildNodes[i];
                        foreach (LayoutTreeNode n2 in bn.ChildNodes)
                        {
                            if (path == bn.N + n2.N)
                                return true;
                        }
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 树结点
        /// </summary>
        public class LayoutTreeNode
        {
            public LayoutTreeNode() { }

            public LayoutTreeNode(string n, LayoutTreeNode parent, List<LayoutTreeNode> childs){
                this.N = n;
                this.Parent = parent;
                this.ChildNodes = childs;
            }
            public LayoutTreeNode(string n, LayoutTreeNode parent, List<LayoutTreeNode> childs, int index)
            {
                this.N = n;
                this.Parent = parent;
                this.ChildNodes = childs;
                this.PartIndex = index;
            }

            public string N;

            public int PartIndex;

            public LayoutTreeNode Parent;

            public List<LayoutTreeNode> ChildNodes;
        }
    }


}

 

posted on 2012-08-29 11:26  zlinke  阅读(274)  评论(0编辑  收藏  举报

导航