[
{
"children":[
{
"children":[
],
"username":"username2",
"password":"password2",
"id":"2",
"pId":"1",
"name":"节点2"
},
{
"children":[
],
"username":"username2",
"password":"password2",
"id":"A2",
"pId":"1",
"name":"节点2"
}
],
"username":"username1",
"password":"password1",
"id":"1",
"pId":"0",
"name":"节点1"
},
{
"children":[
],
"username":"username1",
"password":"password1",
"id":"A1",
"pId":"0",
"name":"节点1"
}
]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyTree.Abs
{
public abstract class TreeObejct
{
public string id { set; get; }
public string pId { set; get; }
public string name { set; get; }
public IList<TreeObejct> children = new List<TreeObejct>();
public virtual void Addchildren(TreeObejct node)
{
this.children.Add(node);
}
}
}
using MyTree.Abs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyTree.Models
{
public class TreeModel : TreeObejct
{
public string username { set; get; }
public string password { set; get; }
}
}
public static IList<TreeObejct> GetData(int number = 11)
{
IList<TreeObejct> datas = new List<TreeObejct>();
for (int i = 1; i < number; i++)
{
datas.Add(new TreeModel
{
id = i.ToString(),
pId = (i - 1).ToString(),
name = "节点" + i,
username = "username" + i,
password = "password" + i
});
datas.Add(new TreeModel
{
id = "A" + i.ToString(),
pId = (i - 1).ToString(),
name = "节点" + i,
username = "username" + i,
password = "password" + i
});
}
return datas;
}
private static IList<TreeObejct> models;
private static IList<TreeObejct> models2;
private static Thread t1;
private static Thread t2;
static void Main(string[] args)
{
int count = 21;
Console.WriteLine("生成任务数:"+count+"个");
Console.Read();
}
public static IList<TreeObejct> GetChildrens(TreeObejct node)
{
IList<TreeObejct> childrens = models.Where(c => c.pId == node.id.ToString()).ToList();
foreach (var item in childrens)
{
item.children = GetChildrens(item);
}
return childrens;
}
public static void Recursion()
{
#region 递归遍历
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
var mds_0 = models.Where(c => c.pId == "0");//获取顶级任务
foreach (var item in mds_0)
{
item.children = GetChildrens(item);
}
sw.Stop();
Console.WriteLine("----------递归遍历用时:" + sw.ElapsedMilliseconds + "----------线程名称:"+t1.Name+",线程ID:"+t1.ManagedThreadId);
#endregion
}
private static IList<TreeObejct> models;
private static IList<TreeObejct> models2;
private static Thread t1;
private static Thread t2;
static void Main(string[] args)
{
int count = 1001;
Console.WriteLine("生成任务数:"+count+"个");
models = GetData(count);
t1 = new Thread(Recursion);
t1.Name = "递归遍历";
t1.Start();
Console.Read();
}
public static void NotRecursion()
{
#region 非递归遍历
System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();
sw2.Start();
Dictionary<string, TreeObejct> dtoMap = new Dictionary<string, TreeObejct>();
foreach (var item in models)
{
dtoMap.Add(item.id, item);
}
IList<TreeObejct> result = new List<TreeObejct>();
foreach (var item in dtoMap.Values)
{
if (item.pId == "0")
{
result.Add(item);
}
else
{
if (dtoMap.ContainsKey(item.pId))
{
dtoMap[item.pId].AddChilrden(item);
}
}
}
sw2.Stop();
Console.WriteLine("----------非递归遍历用时:" + sw2.ElapsedMilliseconds + "----------线程名称:" + t2.Name + ",线程ID:" + t2.ManagedThreadId);
#endregion
}
private static IList<TreeObejct> models;
private static IList<TreeObejct> models2;
private static Thread t1;
private static Thread t2;
static void Main(string[] args)
{
int count = 6;
Console.WriteLine("生成任务数:"+count+"个");
models = GetData(count);
models2 = GetData(count);
t1 = new Thread(Recursion);
t2 = new Thread(NotRecursion);
t1.Name = "递归遍历";
t2.Name = "非递归遍历";
t1.Start();
t2.Start();
Console.Read();
}
| 任务个数 | 递归(ms) | 非递归(ms) |
| 6 | 3 | 0 |
| 6 | 1 | 0 |
| 6 | 1 | 0 |
| 101 | 1 | 0 |
| 101 | 4 | 0 |
| 101 | 5 | 0 |
| 1001 | 196 | 5 |
| 1001 | 413 | 1 |
| 1001 | 233 | 7 |
| 5001 | 4667 | 5 |
| 5001 | 4645 | 28 |
| 5001 | 5055 | 7 |
| 10001 | StackOverflowException | 66 |
| 10001 | StackOverflowException | 81 |
| 10001 | StackOverflowException | 69 |
| 50001 | - | 46 |
| 50001 | - | 47 |
| 50001 | - | 42 |
| 100001 | - | 160 |
| 100001 | - | 133 |
| 100001 | - | 129 |
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有