本章將和大家分享C#中如何通過遞歸獲取所有的子分類。廢話不多說,我們直接來看一下Demo,如下所示: 遞歸寫法如下: using System; using System.Collections.Generic; using System.Linq; namespace RecursionDemo ...
本章將和大家分享C#中如何通過遞歸獲取所有的子分類。廢話不多說,我們直接來看一下Demo,如下所示:
遞歸寫法如下:
using System; using System.Collections.Generic; using System.Linq; namespace RecursionDemo { /// <summary> /// 樹節點 /// </summary> public class TreeNode { /// <summary> /// 節點Id /// </summary> public int Id { get; set; } /// <summary> /// 節點名稱 /// </summary> public string Name { get; set; } /// <summary> /// 父節點Id /// </summary> public int ParentId { get; set; } /// <summary> /// 子節點 /// </summary> public List<TreeNode> Children { get; set; } } /// <summary> /// 樹節點業務邏輯 /// </summary> public class TreeNodeBLL { /// <summary> /// 通過遞歸獲取所有子孫節點 /// </summary> /// <param name="parentId">父節點Id</param> /// <param name="listAllTreeNode">所有節點數據</param> /// <returns>返回所有子孫節點</returns> public List<TreeNode> GetChildren(int parentId, List<TreeNode> listAllTreeNode) { var listResult = new List<TreeNode>(); //存放返回結果的集合 var listChildren = listAllTreeNode.FindAll(node => node.ParentId == parentId); if (listChildren.Count <= 0) { //此處是返回 null 值,還是要返回 listResult ,則需要根據具體的業務邏輯來決定 return null; //return listResult; } //遍歷指定parentId下的第一級子節點 foreach (var item in listChildren) { var childNode = new TreeNode(); childNode.Id = item.Id; childNode.Name = item.Name; childNode.ParentId = item.ParentId; childNode.Children = GetChildren(item.Id, listAllTreeNode); //遞歸獲取子節點下的子節點 listResult.Add(childNode); } return listResult; } /// <summary> /// 初始化級聯數據(用於演示) /// </summary> /// <returns></returns> public List<TreeNode> InitData() { //級聯數據,此處以省市區級聯數據為例 var listAllTreeNode = new List<TreeNode>() { new TreeNode() { Id = 1, ParentId = 0, Name = "福建省" }, new TreeNode() { Id = 2, ParentId = 0, Name = "上海市" }, new TreeNode() { Id = 3, ParentId = 1, Name = "福州市" }, new TreeNode() { Id = 4, ParentId = 1, Name = "廈門市" }, new TreeNode() { Id = 5, ParentId = 3, Name = "永泰縣" }, new TreeNode() { Id = 6, ParentId = 2, Name = "黃浦" }, new TreeNode() { Id = 7, ParentId = 2, Name = "虹口" }, new TreeNode() { Id = 8, ParentId = 2, Name = "寶山" } }; return listAllTreeNode; } } }
調用如下:
using Newtonsoft.Json; namespace RecursionDemo { /// <summary> /// 遞歸Demo /// </summary> internal class Program { static void Main(string[] args) { //示例1:C#中通過遞歸獲取所有的子分類 var treeNodeBLL = new TreeNodeBLL(); var listAllTreeNode = treeNodeBLL.InitData(); var childrenNode = treeNodeBLL.GetChildren(0, listAllTreeNode); var childrenNodeJson = JsonConvert.SerializeObject(childrenNode); //序列化成Json字元串 Console.WriteLine(childrenNodeJson); //換行 Console.WriteLine(); //示例2:求 斐波那契數列 第n項是多少?(通過遞歸方式獲取) int n = 8; //第8項 int result = GetFibonacciSequence(n); Console.WriteLine(string.Format(@"斐波那契數列 第{0}項是:{1}", n, result)); Console.ReadKey(); } /// <summary> /// 獲取 斐波那契數列 第n項的值 (通過遞歸方式獲取) /// </summary> /// <param name="n">第n項</param> /// <returns>第n項的值</returns> static int GetFibonacciSequence(int n) { //斐波那契數列指的是這樣一個數列:1,1,2,3,5,8,13,21,34,55,89... //這個數列從第3項開始,每一項都等於前兩項之和。 if (n < 3) { return 1; } return GetFibonacciSequence(n - 1) + GetFibonacciSequence(n - 2); //通過遞歸獲取前兩項之和 } } }
運行結果如下所示:
其中 childrenNodeJson 值格式化後結果如下所示:
[ { "Id":1, "Name":"福建省", "ParentId":0, "Children":[ { "Id":3, "Name":"福州市", "ParentId":1, "Children":[ { "Id":5, "Name":"永泰縣", "ParentId":3, "Children":null } ] }, { "Id":4, "Name":"廈門市", "ParentId":1, "Children":null } ] }, { "Id":2, "Name":"上海市", "ParentId":0, "Children":[ { "Id":6, "Name":"黃浦", "ParentId":2, "Children":null }, { "Id":7, "Name":"虹口", "ParentId":2, "Children":null }, { "Id":8, "Name":"寶山", "ParentId":2, "Children":null } ] } ]
Demo源碼:
鏈接:https://pan.baidu.com/s/16PpxARzJbwNUeU26hJi0MA 提取碼:sezs
此文由博主精心撰寫轉載請保留此原文鏈接:https://www.cnblogs.com/xyh9039/p/17419432.html
版權聲明:如有雷同純屬巧合,如有侵權請及時聯繫本人修改,謝謝!!!