分享最近學到的遞迴邏輯的替代寫法。
舉個實例比較容易說明,假設公司組織樹狀結構以部門資料物件形式呈現:
publicclass Dept
{publicstring Name;
public List<Dept> Children = new List<Dept>();
}
組織架構範例如下:
{"Name": "總經理",
"Children": [ {"Name": "行政部",
"Children": [ { "Name": "人資組" }, { "Name": "總務組" }]
},
{"Name": "資訊部",
"Children": [ { "Name": "網路組" }, { "Name": "研發組" }]
},
{"Name": "業務部",
"Children": [ {"Name": "海外組",
"Children": [ { "Name": "海外一科" }, { "Name": "海外二科" }]
},
{"Name": "通路組",
"Children": [ { "Name": "行銷科" }, { "Name": "電銷科" }]
}
]
}
]
}
若要列舉所有部門名稱,過去我慣用遞迴(Recursive,在函式中呼叫自己)來解,身為程式老鳥,不爬文不查書徒手寫遞迴是基本功,難不倒我:
using Newtonsoft.Json;using System;using System.Collections.Generic;using System.IO;namespace LinqRecursive{class Program {staticvoid Main(string[] args)
{ var root = JsonConvert.DeserializeObject<Dept>(File.ReadAllText("Org.json"));var deptNames = new List<string>();
ExploreOrg(root, deptNames);
Console.WriteLine(string.Join(",", deptNames.ToArray()));
Console.Read();
}
staticvoid ExploreOrg(Dept dept, List<string> list)
{list.Add(dept.Name);
dept.Children.ForEach(o => ExploreOrg(o, list));
}
}
publicclass Dept
{publicstring Name;
public List<Dept> Children = new List<Dept>();
}
}
執行成功:

最近再遇到相同案例,突發奇想有沒有更巧妙的做法? 爬文查得 LINQ 美技一枚– 為 IEnumerable<T> 新增擴充方法 Traverse(),參數為可取得子物件 IEnumerable 集合的 Lambda 運算式,即可產出樹狀結構下所有節點的 IEnumerable<T> 集合繼續串接 LINQ 方法,十分巧妙:
publicstaticclass LinqExt
{//REF: https://stackoverflow.com/a/20975582/288936publicstatic IEnumerable<T> Traverse<T>(this IEnumerable<T> items,
Func<T, IEnumerable<T>> childSelector)
{ var stack = new Stack<T>(items);while (stack.Any()) {var next = stack.Pop();
yieldreturn next;
foreach (var child in childSelector(next))
stack.Push(child);
}
}
有了萬用 Traverse 擴充方法,不必花時間另寫遞迴函式,程式更簡潔,而傳回型別為 IEnumerable<T>,能與其他 LINQ 操作無縫接軌,極為方便。
staticvoid Main(string[] args)
{ var root = JsonConvert.DeserializeObject<Dept>(File.ReadAllText("Org.json"));Console.WriteLine(string.Join(",",
new List<Dept>() { root }.Traverse<Dept>(o => o.Children)
.Select(o => o.Name).ToArray()));
Console.Read();
}
回頭看 Traverse 方法,利用 Stack<T> 資料結構,一方面取得子元素集合推入 Stack<T>,另一方面則從 Stack<T> 取出元素查詢其子元素,堆與取之間把所有元素都巡過一輪。還有一個巧妙處在於 yield,可以無腦地在迴圈裡遇到條件符合就 return 結果,交由 .NET 在背後蒐集結果彙整成 IEnumerable<T>。若不用 yield 也可以,替代做法是準備一個 List<T> 蒐集結果最後再傳回,像這個樣子:
publicstatic IEnumerable<T> Traverse<T>(this IEnumerable<T> items,
Func<T, IEnumerable<T>> childSelector)
{ var stack = new Stack<T>(items); var results = new List<T>();while (stack.Any()) {var next = stack.Pop();
results.Add(next);
foreach (var child in childSelector(next))
stack.Push(child);
}
return results;}
由此可見 yield 簡化邏輯的效果,收入工具箱,未來遇類似情境多了新武器可用。對 yield 的原理有興趣深入了解的同學,推薦安德魯的系列文: