一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C# - C#構(gòu)建樹形結(jié)構(gòu)數(shù)據(jù)(全部構(gòu)建,查找構(gòu)建)

C#構(gòu)建樹形結(jié)構(gòu)數(shù)據(jù)(全部構(gòu)建,查找構(gòu)建)

2022-01-24 13:48umeall C#

這篇文章主要介紹了C#構(gòu)建樹形結(jié)構(gòu)數(shù)據(jù)(全部構(gòu)建,查找構(gòu)建),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

摘要:

最近在做任務(wù)管理,任務(wù)可以無限派生子任務(wù)且沒有數(shù)量限制,前端采用easyui的treegrid樹形展示控件。

一、遇到的問題

獲取全部任務(wù)拼接樹形速度過慢(數(shù)據(jù)量大約在900條左右)且查詢速度也并不快;

二、解決方法

1、tree轉(zhuǎn)化的json數(shù)據(jù)格式

a.json數(shù)據(jù)格式:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[
  {
    "children":[
      {
        "children":[
 
        ],
        "username":"username2",
        "password":"password2",
        "id":"2",
        "pid":"1",
        "name":"節(jié)點(diǎn)2"
      },
      {
        "children":[
 
        ],
        "username":"username2",
        "password":"password2",
        "id":"a2",
        "pid":"1",
        "name":"節(jié)點(diǎn)2"
      }
    ],
    "username":"username1",
    "password":"password1",
    "id":"1",
    "pid":"0",
    "name":"節(jié)點(diǎn)1"
  },
  {
    "children":[
 
    ],
    "username":"username1",
    "password":"password1",
    "id":"a1",
    "pid":"0",
    "name":"節(jié)點(diǎn)1"
  }
]

b.定義實(shí)體必要字段

為了tree結(jié)構(gòu)的通用性,我們可以定義一個抽象的公用實(shí)體treeobject以保證后續(xù)涉及到的list<t>轉(zhuǎn)化樹形json

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.實(shí)際所需實(shí)體treemodel讓它繼承treeobject,這樣對于id,pid,name,children我們就可以適用于其它實(shí)體了,這也相當(dāng)于我們代碼的特殊約定:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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、遞歸遍歷

獲取全部任務(wù)并轉(zhuǎn)化為樹形

獲取全部任務(wù)轉(zhuǎn)化為樹形是比較簡單的,我們首先獲取到pid=0的頂級數(shù)據(jù)(即不存在父級的任務(wù)),我們通過頂級任務(wù)依次遞歸遍歷它們的子節(jié)點(diǎn)。

C#構(gòu)建樹形結(jié)構(gòu)數(shù)據(jù)(全部構(gòu)建,查找構(gòu)建)

b.我們暫時id以1開始則pid=0的都為頂級任務(wù)

我們首先寫一段生成數(shù)據(jù)的方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 = "節(jié)點(diǎn)" + i,
      username = "username" + i,
      password = "password" + i
    });
    datas.add(new treemodel
    {
      id = "a" + i.tostring(),
      pid = (i - 1).tostring(),
      name = "節(jié)點(diǎn)" + i,
      username = "username" + i,
      password = "password" + i
    });
  }
  return datas;
}

其次我們定義一些變量:

?
1
2
3
4
5
6
7
8
9
10
11
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("生成任務(wù)數(shù):"+count+"個");
 
  console.read();
}

我們再寫一個遞歸獲取子節(jié)點(diǎn)的遞歸方法:

?
1
2
3
4
5
6
7
8
9
10
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;
 
}

編寫調(diào)用遞歸方法recursion:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void recursion()
{
  #region 遞歸遍歷
  system.diagnostics.stopwatch sw = new system.diagnostics.stopwatch();
 
  sw.start();
 
  var mds_0 = models.where(c => c.pid == "0");//獲取頂級任務(wù)
  foreach (var item in mds_0)
  {
    item.children = getchildrens(item);
  }
  sw.stop();
  console.writeline("----------遞歸遍歷用時:" + sw.elapsedmilliseconds + "----------線程名稱:"+t1.name+",線程id:"+t1.managedthreadid);
 
  #endregion
}

編寫main函數(shù)啟動測試:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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("生成任務(wù)數(shù):"+count+"個");
  models = getdata(count);
 
  t1 = new thread(recursion);
 
  t1.name = "遞歸遍歷";
  t1.start();
 
 
  console.read();
}

輸出結(jié)果:

C#構(gòu)建樹形結(jié)構(gòu)數(shù)據(jù)(全部構(gòu)建,查找構(gòu)建)

遞歸遍歷至此結(jié)束。

3、非遞歸遍歷

非遞歸遍歷在操作中不需要遞歸方法的參與即可實(shí)現(xiàn)tree的拼接

對于以上的代碼,我們不需要修改,只需要定義一個非遞歸遍歷方法notrecursion:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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函數(shù):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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("生成任務(wù)數(shù):"+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();
}

啟動查看執(zhí)行結(jié)果:

C#構(gòu)建樹形結(jié)構(gòu)數(shù)據(jù)(全部構(gòu)建,查找構(gòu)建)

發(fā)現(xiàn)一個問題,遞歸3s,非遞歸0s,隨后我又進(jìn)行了更多的測試:

執(zhí)行時間測試

 

任務(wù)個數(shù)           遞歸(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:因包含的嵌套方法調(diào)用過多而導(dǎo)致執(zhí)行堆棧溢出時引發(fā)的異常。 此類不能被繼承。

stackoverflowexception 執(zhí)行堆棧溢出發(fā)生錯誤時引發(fā),通常發(fā)生非常深度或無限遞歸。

-:沒有等到結(jié)果。

當(dāng)然這個測試并不專業(yè),但是也展示出了它的效率的確滿足了當(dāng)前的需求。

4、查找構(gòu)建樹形結(jié)果

原理同上述非遞歸相同,不同之處是我們通過查找的數(shù)據(jù)去構(gòu)建樹形

C#構(gòu)建樹形結(jié)構(gòu)數(shù)據(jù)(全部構(gòu)建,查找構(gòu)建)    

我們通過查找獲取到圈中的任務(wù),再通過當(dāng)前節(jié)點(diǎn)獲取到父級節(jié)點(diǎn),因?yàn)楫?dāng)時沒考慮到任務(wù)層級的關(guān)系,因此為添加層級編號,為此可能會有重復(fù)的存在,因此我們使用hashset<t>來剔除我們的重復(fù)數(shù)據(jù),最終獲取到有用數(shù)據(jù)再通過非遞歸遍歷方法,我們便可以再次構(gòu)建出樹形(tree),來轉(zhuǎn)化為json數(shù)據(jù)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/umeall/p/7660351.html?utm_source=tuicool&utm_medium=referral

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 秋霞网毛片 | 91视频国产自拍 | 韩国三级 720p | 国产福利在线免费观看 | 国产午夜视频在线观看网站 | 亚洲精品成人A8198A片漫画 | 国产伦码精品一区二区三区 | 亚洲精品乱码蜜桃久久久 | 九草视频在线 | 日韩视频免费 | 四虎现在的网址入口2022 | 国产精品日本一区二区不卡视频 | 精品无人乱码一区二区三区 | 女人把私密部位张开让男人桶 | 精品女同同性视频很黄很色 | 免费观看www视频 | 亚洲精品国产在线网站 | 免费看日产一区二区三区 | 麻豆婷婷 | 女人肮脏的交易中文字幕未删减版 | 国产亚洲精品91 | 日韩经典在线观看 | 久久99亚洲AV无码四区碰碰 | 91精品久久 | 高h视频免费观看 | 99热久久这里只有精品23 | 99久久久久国产精品免费 | 99成人| 国内在线观看 | 日日摸夜夜爽色婷婷91 | 污污在线免费观看 | 亚洲一区二区三区91 | 日本三级免费观看 | 无人影院在线播放 | 久久这里只有精品视频e | 好涨好爽好大视频免费 | 日韩亚洲人成在线 | 欧美性f | 亚洲 欧美 清纯 校园 另类 | 射逼视频| 日本一区二区免费在线 |