C#树形结构格式化

参考:构建树形结构数据(全部构建,查找构建)C#版 - umeall - 博客园 (cnblogs.com)

  1 public class TreeObejct
  2 {
  3     public string id { set; get; }
  4     public string pId { set; get; }
  5     public string name { set; get; }
  6     public IList<TreeObejct> children = new List<TreeObejct>();
  7 
  8     public string username { set; get; }
  9     public string password { set; get; }
 10 }
 11 
 12 private static IList<TreeObejct> models;
 13 private static IList<TreeObejct> models2;
 14 private static Thread t1;
 15 private static Thread t2;
 16 static void Main(string[] args)
 17 {
 18     int count = 1000;
 19     Console.WriteLine("生成任务数:" + count + "");
 20     models = GetData(count);
 21     models2 = GetData(count);
 22     t1 = new Thread(Recursion);
 23     t2 = new Thread(NotRecursion);
 24     t1.Name = "递归遍历";
 25     t2.Name = "非递归遍历";
 26     t1.Start();
 27     t2.Start();
 28 
 29     Console.Read();
 30 }
 31 
 32 public static IList<TreeObejct> GetData(int number = 11)
 33 {
 34     IList<TreeObejct> datas = new List<TreeObejct>();
 35     for (int i = 1; i < number; i++)
 36     {
 37         datas.Add(new TreeObejct
 38         {
 39             id = i.ToString(),
 40             pId = (i - 1).ToString(),
 41             name = "节点" + i,
 42             username = "username" + i,
 43             password = "password" + i
 44         });
 45         datas.Add(new TreeObejct
 46         {
 47             id = "A" + i.ToString(),
 48             pId = (i - 1).ToString(),
 49             name = "节点" + i,
 50             username = "username" + i,
 51             password = "password" + i
 52         });
 53     }
 54     //Console.WriteLine(datas);
 55     return datas;
 56 }
 57 public static IList<TreeObejct> GetChildrens(TreeObejct node)
 58 {
 59     IList<TreeObejct> childrens = models.Where(c => c.pId == node.id.ToString()).ToList();
 60     foreach (var item in childrens)
 61     {
 62         item.children = GetChildrens(item);
 63     }
 64     return childrens;
 65 
 66 }
 67 public static void Recursion()
 68 {
 69     #region  递归遍历
 70     System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
 71 
 72     sw.Start();
 73 
 74     var mds_0 = models.Where(c => c.pId == "0");//获取顶级任务
 75     foreach (var item in mds_0)
 76     {
 77         item.children = GetChildrens(item);
 78     }
 79     sw.Stop();
 80     Console.WriteLine("----------递归遍历用时:" + sw.ElapsedMilliseconds + "----------线程名称:" + t1.Name + ",线程ID:" + t1.ManagedThreadId);
 81 
 82     #endregion
 83 }
 84 public static void NotRecursion()
 85 {
 86     #region 非递归遍历
 87 
 88     System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();
 89 
 90     sw2.Start();
 91     Dictionary<string, TreeObejct> dtoMap = new Dictionary<string, TreeObejct>();
 92     foreach (var item in models)
 93     {
 94         dtoMap.Add(item.id, item);
 95     }
 96     IList<TreeObejct> result = new List<TreeObejct>();
 97     foreach (var item in dtoMap.Values)
 98     {
 99         if (item.pId == "0")
100         {
101             result.Add(item);
102         }
103         else
104         {
105             if (dtoMap.ContainsKey(item.pId))
106             {
107                 dtoMap[item.pId].children.Add(item);
108             }
109         }
110     }
111 
112     sw2.Stop();
113     Console.WriteLine("----------非递归遍历用时:" + sw2.ElapsedMilliseconds + "----------线程名称:" + t2.Name + ",线程ID:" + t2.ManagedThreadId);
114 
115     Console.WriteLine(result);
116     //Console.WriteLine(dtoMap);
117     #endregion
118 }

结果:

生成任务数:1000个
----------非递归遍历用时:5----------线程名称:非递归遍历,线程ID:14

----------递归遍历用时:86----------线程名称:递归遍历,线程ID:5

posted @ 2022-03-04 11:10  chenjingchun  阅读(72)  评论(0编辑  收藏  举报