Transferred from: https://www.jb51.net/article/125747.htm
Tree structure: Recently in the task management, the task can derive the subtasks infinitely and there is no quantitative limit, the front end adopts the Easyui Treegrid tree display control.
A.json Data format:
[ {" children": [ { "Children": [ ], "username": "username2", "password": "Password2", "id": "2", "PId": "1", "name": "Node 2" }, { "Children": [ ], "username": " UserName2 ", " password ":" Password2 ", " id ":" A2 ", " PId ":" 1 ", " name ":" Node 2 " } ], "username": "username1", "password": "Password1", "id": "1", "pId": "0", "name": "Node 1" } ]
B. Defining the required fields for an entity
For the versatility of the tree structure, we can define an abstract common entity treeobject to ensure that subsequent list<t> transform tree JSON is involved
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. The actual required entity TreeModel Let it inherit treeobject, so that for id,pid,name,children we can apply to other entities, which is also equivalent to our code special contract:
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. Recursive traversal
Get all tasks and convert to tree
Getting all tasks into a tree is simpler, we first get the top-level data of pid=0 (that is, a task that does not have a parent), and we recursively traverse their child nodes in turn by the top-level tasks.
/// recursively get data for all tree structures 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; } Recursive get child node 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. Non-recursive traversal
Non-recursive traversal does not require the participation of recursive methods in the operation to realize the mosaic of the tree
For the above code, we do not need to modify, only need to define a non-recursive traversal method Notrecursion:
public static void Notrecursion () { #region non-recursive traversal 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 ("----------Non-recursive traversal time:" + SW2. Elapsedmilliseconds + "----------Thread Name:" + T2. Name + ", Thread 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 ("Number of build tasks:" +count+ "X"); Models = GetData (count); MODELS2 = GetData (count); T1 = new Thread (recursion); t2 = new Thread (notrecursion); T1. Name = "recursive traversal"; T2. Name = "Non-recursive traversal"; T1. Start (); T2. Start (); Console.read ();}
C # Building a tree data structure