C#构建树形数据结构
转自:https://www.jb51.net/article/125747.htm
树形结构:最近在做任务管理,任务可以无限派生子任务且没有数量限制,前端采用Easyui的Treegrid树形展示控件。
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"
} ]
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 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的顶级数据(即不存在父级的任务),我们通过顶级任务依次递归遍历它们的子节点。
/ //递归获取所有树结构的数据
public IList<TreeObject> GetData()
{
List<TreeObject> nodes = _context.Node.Where(x => x.parent_node_id == 0).Select(x=>new TreeObject { id=x.id,pId=x.parent_node_id,name=x.name}).ToList();
foreach(TreeObject item in nodes)
{
item.children = GetChildrens(item);
}
return nodes;
}
//递归获取子节点
public IList<TreeObject> GetChildrens(TreeObject node)
{
IList<TreeObject> childrens = _context.Node.Where(c => c.parent_node_id == node.id).Select(x => new TreeObject { id = x.id, pId = x.parent_node_id, name = x.name }).ToList();
foreach (TreeObject item in childrens)
{
item.children = GetChildrens(item);
}
return childrens;
}
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 == "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
main.cs
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();
}
C#构建树形数据结构的更多相关文章
- SQLServer树形数据结构的数据进行数据统计
前言 前几天朋友问我,关于SQLServer数据库中对树形结构的表数据统计问题,需求大致如下: 分类表(递归数据),A的子分类是B,B的子分类是C--分类关系不间断,A为第一层,B为第二层,C为第三层 ...
- java构建树形菜单递归工具类
1.设计菜单实体 import java.util.List; public class Menu { //菜单id private Long id; //父节点id private Long par ...
- js treeData 树形数据结构 无限层级(转载)
js实现无限层级树形数据结构(创新算法) 转载:https://blog.csdn.net/Mr_JavaScript/article/details/82817177 由于做项目的需要,把一个线性数 ...
- C# 通用树形数据结构
前言 树在图论中是一种重要的图,由于其自身的许多特殊性质,也是一种重要的计算机数据结构,在很多地方都有用.但是这些树大多都是作为其他应用的内部数据结构来使用.我们无法了解这些树的详细信息,而 .Net ...
- 构建树形结构数据(全部构建,查找构建)C#版
摘要: 最近在做任务管理,任务可以无限派生子任务且没有数量限制,前端采用Easyui的Treegrid树形展示控件. 一.遇到的问题 获取全部任务拼接树形速度过慢(数据量大约在900条左右)且查询速度 ...
- [SinGuLaRiTy] (树形)数据结构题目复习
[SinGuLaRiTy-1023] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 普通平衡树 题目描述 你需要写一种数据结构(可参考题目标 ...
- java构建树形列表(带children属性)
一些前端框架提供的树形表格需要手动构建树形列表(带children属性的对象数组),这种结构一般是需要在Java后台构建好. 构建的方式是通过id字段与父id字段做关联,通过递归构建children字 ...
- ExtJS4.2 根据数据库记录构建树形菜单
背景:最近用ExtJS4.2做一个系统,需要在前端展示资源菜单,为树形结构,该树形结构是从数据库动态加载的. ExtJS的树形结构大致有两种情况: 1.静态树形结构,此处不多说,看API就能简单明白: ...
- 使用zTree插件构建树形菜单
zTree下载:https://github.com/zTree/zTree_v3 目录: 就我看来,zTree较为实用的有以下几点: zTree 是一个依靠 jQuery 实现的多功能 “树插件”. ...
随机推荐
- DOS文件操作命令
内部命令 COPY---文件固执命令 格式:COPY [源盘:][路径]<源文件名> [目标盘][路径]<目标文件名> 拷贝一个或多个文件到指定盘上 1)COPY是文件对文件的 ...
- [ActionScript 3.0] 幻灯片效果实例
package com.fylibs.components.effects { import com.fylibs.utils.LoaderQueues; import com.tweener.tra ...
- C++基础知识-inline、const、mutable、this、static
一.在类定义中实现成员函数inline 类内的成员函实现其实也叫作类内的成员函数定义. 这种直接在类的定义中实现的函数,会被当做inline内联函数来处理. 二.成员函数末尾的const const: ...
- iOS 本地时间 / UTC时间 / 时间戳等操作 / 获取当前年月日
//获得当前时间并且转为字符串 - (NSString *)dateTransformToTimeString { NSDate *currentDate = [NSDate date];//获得当前 ...
- 2016级算法第六次上机-B.ModricWang's FFT : EASY VERSION
1114 ModricWang's FFT EASY VERSION 思路 利用FFT做大整数乘法,实际上是把大整数变成多项式,然后做多项式乘法. 例如,对于\(1234\),改写成\(f(x)=1* ...
- SpringBoot + Scala环境部署
在pom.xml文件中添加: <dependencies>中 <!-- 添加Scala依赖 --> <dependency> <groupId>org. ...
- mysql 与sqlser group by
mysql select * ,count(1) from simccbillm18 group by MonthNum; SqlSer select col1,col2 from table g ...
- springmvc 运行原理 Spring ioc的实现原理 Mybatis工作流程 spring AOP实现原理
SpringMVC的工作原理图: SpringMVC流程 . 用户发送请求至前端控制器DispatcherServlet. . DispatcherServlet收到请求调用HandlerMappin ...
- Spring Security 入门
一.Spring Security简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配 ...
- [转] ELK 之 Logstash
[From] https://blog.csdn.net/iguyue/article/details/77006201 ELK 之 Logstash 简介: ELK 之 LogstashLogsta ...