2014/9/6 技术探讨

    说起那个无限级分类,相信很多人都知道是什么东西,也曾经做过。我也相信,大家用得最多的实现方式就是做一个递归。最近我也要做一个带无限级分类的菜单,但是我又不想用递归来做,所以我需要用其他方式来实现,那就是迭代了。

    首先,我需要定义一个实体模型,这举一个省市无限级的例子:

class Loaction
{
    public int ID { get; set; }
    public int PID { get; set; }
    public string Name { get; set; }    //地方名
    public int Level { get; set; }      //深度
}

    然后写方法,这里需要利用到栈的后进先出的特点:

    public static List<Loaction> Soft(List<Loaction> data, int pid)
        {
            Stack task = new Stack();
            task.Push(pid);
            List<Loaction> tree = new List<Loaction>();
            int level = 0;
            while (task.Count > 0)
            {
                bool flag = false;
                for (int i = 0; i < data.Count; i++)
                {
                    var l = data[i];
                    if (l.PID == pid)
                    {
                        pid = l.ID;
                        task.Push(l.ID);
                        l.Level = level;
                        level++;
                        tree.Add(l);
                        data.Remove(l);
                        i--;
                        flag=true;
                    }
                }
                if (!flag)
                {
                    task.Pop();
                    if (task.Count > 0)
                    {
                        pid = Convert.ToInt32(task.Peek());
                        level--;
                    }
                }
            }
            return tree;
        }

    最后准备数据->调用->输出:然后效果如下:

        static void Main(string[] args)
        {
            var data = new List<Loaction>();
            data.Add(new Loaction() { ID = 1, PID = 0, Name = "北京" });
            data.Add(new Loaction() { ID = 2, PID = 0, Name = "广东" });
            data.Add(new Loaction() { ID = 3, PID = 0, Name = "上海" });
            data.Add(new Loaction() { ID = 4, PID = 0, Name = "重庆" });
            data.Add(new Loaction() { ID = 5, PID = 0, Name = "黑龙江" });
            data.Add(new Loaction() { ID = 6, PID = 1, Name = "丰台" });
            data.Add(new Loaction() { ID = 7, PID = 1, Name = "海淀" });
            data.Add(new Loaction() { ID = 8, PID = 1, Name = "石景山" });
            data.Add(new Loaction() { ID = 9, PID = 3, Name = "上海市" });
            data.Add(new Loaction() { ID = 10, PID = 2, Name = "广州" });
            data.Add(new Loaction() { ID = 11, PID = 5, Name = "齐齐哈尔" });
            data.Add(new Loaction() { ID = 12, PID = 2, Name = "茂名" });
            data.Add(new Loaction() { ID = 13, PID = 2, Name = "深圳" });
            data.Add(new Loaction() { ID = 14, PID = 5, Name = "哈尔滨" });
            data.Add(new Loaction() { ID = 15, PID = 4, Name = "重庆市" });
            data.Add(new Loaction() { ID = 16, PID = 2, Name = "东莞" });
            data.Add(new Loaction() { ID = 17, PID = 2, Name = "中山" });
            data.Add(new Loaction() { ID = 18, PID = 16, Name = "厚街镇" });

            var tree = Soft(data, 0);

            foreach (var t in tree)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < t.Level; i++)
                {
                    sb.Append("  ");
                   
                } 
                sb.Append(t.Name);
                Console.WriteLine(sb.ToString());
            }
            Console.ReadKey();
        }

  

    就是这个样子。。。。。(END)

  [ .NET ]   [ 算法 ]
知识共享许可协议 本作品由小蝶惊鸿创作,采用知识共享署名 4.0 国际许可协议进行许可,转载时请保留本文署名及链接。