构建树形结构数据(全部构建,查找构建)C#版
摘要:
最近在做任务管理,任务可以无限派生子任务且没有数量限制,前端采用Easyui的Treegrid树形展示控件。
一、遇到的问题
获取全部任务拼接树形速度过慢(数据量大约在900条左右)且查询速度也并不快;
二、解决方法
1、Tree转化的JSON数据格式
a.JSON数据格式:
[
{
"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"
}
]
b.定义实体必要字段
为了Tree结构的通用性,我们可以定义一个抽象的公用实体TreeObject以保证后续涉及到的List<T>转化树形JSON
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);
}
}
}
c.实际所需实体TreeModel让它继承TreeObject,这样对于id,pId,name,children我们就可以适用于其它实体了,这也相当于我们代码的特殊约定:
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; }
}
}
2、递归遍历
获取全部任务并转化为树形
获取全部任务转化为树形是比较简单的,我们首先获取到pId=0的顶级数据(即不存在父级的任务),我们通过顶级任务依次递归遍历它们的子节点。
b.我们暂时id以1开始则pId=0的都为顶级任务
我们首先写一段生成数据的方法:
public static IList<TreeObejct> GetData(int number = )
{
IList<TreeObejct> datas = new List<TreeObejct>();
for (int i = ; i < number; i++)
{
datas.Add(new TreeModel
{
id = i.ToString(),
pId = (i - ).ToString(),
name = "节点" + i,
username = "username" + i,
password = "password" + i
});
datas.Add(new TreeModel
{
id = "A" + i.ToString(),
pId = (i - ).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 = ;
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; }
编写调用递归方法Recursion:
public static void Recursion()
{
#region 递归遍历
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); var mds_0 = models.Where(c => c.pId == "");//获取顶级任务
foreach (var item in mds_0)
{
item.children = GetChildrens(item);
}
sw.Stop();
Console.WriteLine("----------递归遍历用时:" + sw.ElapsedMilliseconds + "----------线程名称:"+t1.Name+",线程ID:"+t1.ManagedThreadId); #endregion
}
编写main函数启动测试:
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 = ;
Console.WriteLine("生成任务数:"+count+"个");
models = GetData(count); t1 = new Thread(Recursion); t1.Name = "递归遍历";
t1.Start(); Console.Read();
}
输出结果:
递归遍历至此结束。
3、非递归遍历
非递归遍历在操作中不需要递归方法的参与即可实现Tree的拼接
对于以上的代码,我们不需要修改,只需要定义一个非递归遍历方法NotRecursion:
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 == "")
{
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
}
编写main函数:
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 = ;
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();
}
启动查看执行结果:
发现一个问题,递归3s,非递归0s,随后我又进行了更多的测试:
任务个数 | 递归(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 |
StackOverflowException:因包含的嵌套方法调用过多而导致执行堆栈溢出时引发的异常。 此类不能被继承。StackOverflowException 执行堆栈溢出发生错误时引发,通常发生非常深度或无限递归。
-:没有等到结果。
当然这个测试并不专业,但是也展示出了它的效率的确满足了当前的需求。
4、查找构建树形结果
原理同上述非递归相同,不同之处是我们通过查找的数据去构建树形
我们通过查找获取到圈中的任务,再通过当前节点获取到父级节点,因为当时没考虑到任务层级的关系,因此为添加层级编号,为此可能会有重复的存在,因此我们使用HashSet<T>来剔除我们的重复数据,最终获取到有用数据再通过非递归遍历方法,我们便可以再次构建出树形(tree),来转化为JSON数据。
参考文档:快速构建树形结构数据(非递归)
构建树形结构数据(全部构建,查找构建)C#版的更多相关文章
- 树形结构数据存储方案的选择和java list转tree
树形结构数据存储方案 Adjacency List:每一条记录存parent_idPath Enumerations:每一条记录存整个tree path经过的node枚举Nested Sets:每一条 ...
- java构建树形菜单递归工具类
1.设计菜单实体 import java.util.List; public class Menu { //菜单id private Long id; //父节点id private Long par ...
- java构建树形列表(带children属性)
一些前端框架提供的树形表格需要手动构建树形列表(带children属性的对象数组),这种结构一般是需要在Java后台构建好. 构建的方式是通过id字段与父id字段做关联,通过递归构建children字 ...
- [App Store Connect帮助]五、管理构建版本(2)查看构建版本和文件大小
您可以查看您为某个 App 上传的所有构建版本,和由 App Store 创建的变体版本的大小.一些构建版本在该 App 发布到 App Store 上后可能不会显示. 必要职能:“帐户持有人”职能. ...
- C# 动态构建表达式树(二)——构建 Select 和 GroupBy 的表达式
C# 动态构建表达式树(二)--构建 Select 和 GroupBy 的表达式 前言 在上篇中写了表达式的基本使用,为 Where 方法动态构建了表达式.在这篇中会写如何为 Select 和 Gro ...
- .NET Core中间件的注册和管道的构建(1)---- 注册和构建原理
.NET Core中间件的注册和管道的构建(1)---- 注册和构建原理 0x00 问题的产生 管道是.NET Core中非常关键的一个概念,很多重要的组件都以中间件的形式存在,包括权限管理.会话管理 ...
- Eclipse的maven构建一个web项目,以构建SpringMVC项目为例
http://www.cnblogs.com/javaTest/archive/2012/04/28/2589574.html springmvc demo实例教程源代码下载:http://zuida ...
- 快速构建Windows 8风格应用15-ShareContract构建
原文:快速构建Windows 8风格应用15-ShareContract构建 本篇博文主要介绍共享数据包.如何构建共享源.如何构建共享目标.DataTransferManager类. 共享数据包 Da ...
- 快速构建Windows 8风格应用13-SearchContract构建
原文:快速构建Windows 8风格应用13-SearchContract构建 本篇博文主要介绍如何在应用中构建SearchContract,相应的原理已经在博文<快速构建Windows 8风格 ...
随机推荐
- mina 通讯框架
Apache Mina Server 是一个网络通信应用框架,也就是说,它主要是对基于TCP/IP.UDP/IP协议栈的通信框架(当然,也可以提供JAVA 对象的序列化服务.虚拟机管道通信服务等),M ...
- iOS 数据安全、数据加密传输
近期接到一个新需求:APP企业版需要接入热更新功能. 热更新需要下发补丁脚本, 脚本下发过程中需要保证脚本传输安全,且需要避免中间人攻击. 需要用到数据加密传输方面的知识,以下是我设计的加密解密流程: ...
- Java编写画图板程序细节-保存已画图形
没有Java编写画图板程序细节-保存已画图形 一.为何我们要保存画图板上已画图形呢? 有很多人会问,为什么我们一定要保存画图板上已经画好了的图形呢?原因很简单.当我们在画图板上画完自己想画的图形后 ...
- OO 第三次博客总结
调研规格化设计 1950年代,第一次分离,主程序和子程序的分离程序结构模型是树状模型,子程序可先于主程序编写.通过使用库函数来简化编程,实现最初的代码重用.产生基本的软件开发过程:分析—设计—编码—测 ...
- angular路由传参和获取路由参数的方法
1.首先是需要导入的模块 import { Router } from "@angular/router";//路由传参用到 import{ActivatedRoute,Param ...
- Mysql完全卸载(Windows版本)
(1)控制面板 ---> 程序和功能 ---> 卸载MySQL Installer: (2)删除MySQL软件安装路径下的MySQL目录,默认目录为 C:\Program Files (x ...
- thinkPHP写txt日志文件
file_put_contents(DATA_PATH.'文件名.txt', '收到请求:' . date('Y-m-d H:i:s') . PHP_EOL . '通知信息:' . $显示的变量名. ...
- bmob关联表
var DDB_User = Bmob.Object.createWithoutData("DDB_User", "b2fd2fe68f"); // var T ...
- 第六节 Go数据结构之集合
一.什么是集合 集合就是不同对象的无序聚集.那么链表和集合有什么关系呢?我们来变个魔术.如下图是各种颜色组成的链表: 下面我们一步步把链表变成集合. 第一步砍去链接 第二步去掉重复 第三步放到一个框里 ...
- scala (8) 模糊匹配
object MatchDemo { /** * 定义偏函数用PartialFunction来表示 * PartialFunction[T1,T2]要求传入一个参数T1,T2代表返回的类型. * 偏函 ...