• 数据表中有字段ID,fatherID即可存储一个树,下面说说如何遍历这个树


    数据表中有字段ID,fatherID即可存储一个树,那么如何遍历这个树呢?

    说明:

    一,下面的PlanModel是一个数据实体类,其中属性包括ID,fatherID等

    二,方法:PlanBLL.GetChildrenPlanList(......)和 PlanBLL.GetFatherPlan(......)都是自己写的方法,分别用来访问子节点列表和父节点

    三,在外部调用ShowNode()方法,即可成功遍历。

     1 //记录已经遍历过的节点
    2 private IList<int> visitedModelIDList = new List<int>();
    3
    4 //层级系数
    5 private int index = 0;
    6
    7 /// <summary>
    8 /// 递归,显示计划节点
    9 /// </summary>
    10 /// <param name="currentModel">当前节点实体</param>
    11 private void ShowNode(PlanModel currentModel)
    12 {
    13
    14 //如果没有被遍历,就显示该节点
    15 if (visitedModelIDList.Contains(currentModel.ID) == false)
    16 {
    17 ShowPlanIntoTable(currentModel, index); //显示到页面
    18 visitedModelIDList.Add(currentModel.ID);
    19 }
    20
    21 //如果有没有被访问的子节点,就转向该子节点
    22 if (PlanBLL.GetChildrenPlanList(currentModel.ID).Where(m => visitedModelIDList.Contains(m.ID) == false).Count() > 0)
    23 {
    24 PlanModel model = PlanBLL.GetChildrenPlanList(currentModel.ID).Where(m => visitedModelIDList.Contains(m.ID) == false).First();
    25 index++;
    26 ShowNode(model);
    27 }
    28 else //如果没了未遍历的子节点
    29 {
    30 if(currentModel.FatherID == 0)
    31 {
    32 return;
    33 }
    34 else
    35 {
    36 PlanModel model = PlanBLL.GetFatherPlan(currentModel.FatherID);
    37 index--;
    38 ShowNode(model);
    39 }
    40 }
    41 }

      

  • 相关阅读:
    学习笔记 Nim
    学习笔记 Multi-Nim
    学习笔记 Anti-Nim
    机器学习 2
    机器学习 1
    2020 ICPC 南京 by chinakevin @CraZyMoon 狂月
    浙江省赛 2020 (ZJCPC2020)(The 17th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple)
    Selection Contest for Rookies 4 E Polycarp's New Job
    AtCoder Beginner Contest 166 F Three Variables Game
    Codeforces Round #638 (Div. 2) E Phoenix and Berries
  • 原文地址:https://www.cnblogs.com/wangfupeng1988/p/2133454.html
Copyright © 2020-2023  润新知