日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

构建树形结构数据(全部构建,查找构建)C#版

發布時間:2024/4/17 C# 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 构建树形结构数据(全部构建,查找构建)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 = 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;}

?

      編寫調用遞歸方法Recursion:

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}

?

      編寫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 = 1001;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 == "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函數:

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();}

?

?      啟動查看執行結果:

      發現一個問題,遞歸3s,非遞歸0s,隨后我又進行了更多的測試:

執行時間測試
任務個數? ? ? ? ? 遞歸(ms)? ??????? ? ? 非遞歸(ms)
630
610
610
10110
10140
10150
10011965
10014131
10012337
500146675
5001464528
500150557
10001StackOverflowException66
10001StackOverflowException81
10001StackOverflowException69
50001-46
50001-47
50001-42
100001-160
100001-133
100001-129

      StackOverflowException:因包含的嵌套方法調用過多而導致執行堆棧溢出時引發的異常。?此類不能被繼承。StackOverflowException?執行堆棧溢出發生錯誤時引發,通常發生非常深度或無限遞歸。

      -:沒有等到結果。

?      當然這個測試并不專業,但是也展示出了它的效率的確滿足了當前的需求。

?   4、查找構建樹形結果

      原理同上述非遞歸相同,不同之處是我們通過查找的數據去構建樹形

    

?      我們通過查找獲取到圈中的任務,再通過當前節點獲取到父級節點,因為當時沒考慮到任務層級的關系,因此為添加層級編號,為此可能會有重復的存在,因此我們使用HashSet<T>來剔除我們的重復數據,最終獲取到有用數據再通過非遞歸遍歷方法,我們便可以再次構建出樹形(tree),來轉化為JSON數據。

?      

?

    參考文檔:快速構建樹形結構數據(非遞歸)

?

?

轉載于:https://www.cnblogs.com/umeall/p/7660351.html

總結

以上是生活随笔為你收集整理的构建树形结构数据(全部构建,查找构建)C#版的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。