ฉันมีต้นไม้ที่สร้างขึ้นจากคลาสนี้
class Node
{
public string Key { get; }
public List<Node> Children { get; }
}
ฉันต้องการค้นหาเด็กทุกคนและลูก ๆ ทุกคนเพื่อให้ได้คนที่ตรงกับเงื่อนไข:
node.Key == SomeSpecialKey
ฉันจะใช้งานได้อย่างไร?
ฉันมีต้นไม้ที่สร้างขึ้นจากคลาสนี้
class Node
{
public string Key { get; }
public List<Node> Children { get; }
}
ฉันต้องการค้นหาเด็กทุกคนและลูก ๆ ทุกคนเพื่อให้ได้คนที่ตรงกับเงื่อนไข:
node.Key == SomeSpecialKey
ฉันจะใช้งานได้อย่างไร?
คำตอบ:
เป็นความเข้าใจผิดที่ต้องมีการเรียกซ้ำ มันจะต้องมีสแต็คหรือคิวและวิธีที่ง่ายที่สุดคือการดำเนินการโดยใช้การเรียกซ้ำ เพื่อความสมบูรณ์ฉันจะให้คำตอบแบบไม่เรียกซ้ำ
static IEnumerable<Node> Descendants(this Node root)
{
var nodes = new Stack<Node>(new[] {root});
while (nodes.Any())
{
Node node = nodes.Pop();
yield return node;
foreach (var n in node.Children) nodes.Push(n);
}
}
ตัวอย่างเช่นใช้นิพจน์นี้เพื่อใช้:
root.Descendants().Where(node => node.Key == SomeSpecialKey)
StackOverflowException.
Queue<Node>(ด้วยการเปลี่ยนแปลงที่เกี่ยวข้องกับEnqueue/ DequeueจากPush/ Pop)
ค้นหา Tree of Objects ด้วย Linq
public static class TreeToEnumerableEx
{
public static IEnumerable<T> AsDepthFirstEnumerable<T>(this T head, Func<T, IEnumerable<T>> childrenFunc)
{
yield return head;
foreach (var node in childrenFunc(head))
{
foreach (var child in AsDepthFirstEnumerable(node, childrenFunc))
{
yield return child;
}
}
}
public static IEnumerable<T> AsBreadthFirstEnumerable<T>(this T head, Func<T, IEnumerable<T>> childrenFunc)
{
yield return head;
var last = head;
foreach (var node in AsBreadthFirstEnumerable(head, childrenFunc))
{
foreach (var child in childrenFunc(node))
{
yield return child;
last = child;
}
if (last.Equals(node)) yield break;
}
}
}
headและchildrenFuncแบ่งวิธีการออกเป็นสองส่วนดังนั้นการตรวจสอบพารามิเตอร์จึงไม่ถูกเลื่อนไปเป็นเวลาข้าม
หากคุณต้องการรักษา Linq เหมือนไวยากรณ์คุณสามารถใช้วิธีการรับลูกหลานทั้งหมด (เด็ก + ลูกของเด็ก ฯลฯ )
static class NodeExtensions
{
public static IEnumerable<Node> Descendants(this Node node)
{
return node.Children.Concat(node.Children.SelectMany(n => n.Descendants()));
}
}
จากนั้นสามารถแจงนับได้เช่นเดียวกับที่อื่นโดยใช้ที่ไหนหรือก่อนหรืออะไรก็ได้
คุณสามารถลองวิธีส่วนขยายนี้เพื่อระบุโหนดต้นไม้:
static IEnumerable<Node> GetTreeNodes(this Node rootNode)
{
yield return rootNode;
foreach (var childNode in rootNode.Children)
{
foreach (var child in childNode.GetTreeNodes())
yield return child;
}
}
จากนั้นใช้กับWhere()ประโยค:
var matchingNodes = rootNode.GetTreeNodes().Where(x => x.Key == SomeSpecialKey);
บางทีคุณอาจต้องการเพียง
node.Children.Where(child => child.Key == SomeSpecialKey)
หรือหากคุณต้องการค้นหาลึกลงไปอีกระดับหนึ่ง
node.Children.SelectMany(
child => child.Children.Where(child => child.Key == SomeSpecialKey))
หากคุณต้องการค้นหาในทุกระดับให้ดำเนินการดังต่อไปนี้:
IEnumerable<Node> FlattenAndFilter(Node source)
{
List<Node> l = new List();
if (source.Key == SomeSpecialKey)
l.Add(source);
return
l.Concat(source.Children.SelectMany(child => FlattenAndFilter(child)));
}
public class Node
{
string key;
List<Node> children;
public Node(string key)
{
this.key = key;
children = new List<Node>();
}
public string Key { get { return key; } }
public List<Node> Children { get { return children; } }
public Node Find(Func<Node, bool> myFunc)
{
foreach (Node node in Children)
{
if (myFunc(node))
{
return node;
}
else
{
Node test = node.Find(myFunc);
if (test != null)
return test;
}
}
return null;
}
}
จากนั้นคุณสามารถค้นหาเช่น:
Node root = new Node("root");
Node child1 = new Node("child1");
Node child2 = new Node("child2");
Node child3 = new Node("child3");
Node child4 = new Node("child4");
Node child5 = new Node("child5");
Node child6 = new Node("child6");
root.Children.Add(child1);
root.Children.Add(child2);
child1.Children.Add(child3);
child2.Children.Add(child4);
child4.Children.Add(child5);
child5.Children.Add(child6);
Node test = root.Find(p => p.Key == "child6");
ทำไมไม่ใช้IEnumerable<T>วิธีการขยาย
public static IEnumerable<TResult> SelectHierarchy<TResult>(this IEnumerable<TResult> source, Func<TResult, IEnumerable<TResult>> collectionSelector, Func<TResult, bool> predicate)
{
if (source == null)
{
yield break;
}
foreach (var item in source)
{
if (predicate(item))
{
yield return item;
}
var childResults = SelectHierarchy(collectionSelector(item), collectionSelector, predicate);
foreach (var childItem in childResults)
{
yield return childItem;
}
}
}
จากนั้นทำสิ่งนี้
var result = nodes.Children.SelectHierarchy(n => n.Children, n => n.Key.IndexOf(searchString) != -1);
ในขณะที่ฉันเขียนบทความ codeproject ซึ่งอธิบายถึงวิธีการใช้ Linq เพื่อค้นหาโครงสร้างที่เหมือนต้นไม้:
http://www.codeproject.com/KB/linq/LinqToTree.aspx
สิ่งนี้ให้ API สไตล์ linq-to-XML ที่คุณสามารถค้นหาลูกหลานลูกหลานบรรพบุรุษ ฯลฯ ...
อาจมากเกินไปสำหรับปัญหาปัจจุบันของคุณ แต่อาจเป็นที่สนใจของผู้อื่น
คุณสามารถใช้วิธีการขยายนี้เพื่อค้นหาต้นไม้
public static IEnumerable<Node> InTree(this Node treeNode)
{
yield return treeNode;
foreach (var childNode in treeNode.Children)
foreach (var flattendChild in InTree(childNode))
yield return flattendChild;
}
ฉันมีวิธีการขยายทั่วไปที่สามารถทำให้แบนใด ๆIEnumerable<T>และจากคอลเลกชันที่แบนคุณจะได้รับโหนดที่คุณต้องการ
public static IEnumerable<T> FlattenHierarchy<T>(this T node, Func<T, IEnumerable<T>> getChildEnumerator)
{
yield return node;
if (getChildEnumerator(node) != null)
{
foreach (var child in getChildEnumerator(node))
{
foreach (var childOrDescendant in child.FlattenHierarchy(getChildEnumerator))
{
yield return childOrDescendant;
}
}
}
}
ใช้สิ่งนี้:
var q = from node in myTree.FlattenHierarchy(x => x.Children)
where node.Key == "MyKey"
select node;
var theNode = q.SingleOrDefault();
ฉันใช้การนำไปใช้ต่อไปนี้เพื่อแจกแจงรายการ Tree
public static IEnumerable<Node> DepthFirstUnfold(this Node root) =>
ObjectAsEnumerable(root).Concat(root.Children.SelectMany(DepthFirstUnfold));
public static IEnumerable<Node> BreadthFirstUnfold(this Node root) {
var queue = new Queue<IEnumerable<Node>>();
queue.Enqueue(ObjectAsEnumerable(root));
while (queue.Count != 0)
foreach (var node in queue.Dequeue()) {
yield return node;
queue.Enqueue(node.Children);
}
}
private static IEnumerable<T> ObjectAsEnumerable<T>(T obj) {
yield return obj;
}
BreadthFirstUnfold ในการนำไปใช้ข้างต้นใช้คิวของลำดับโหนดแทนคิวโหนด นี่ไม่ใช่วิธีอัลกอริทึม BFS แบบคลาสสิก
และเพื่อความสนุกสนาน (เกือบทศวรรษต่อมา) คำตอบที่ใช้ Generics แต่ใช้ Stack และ While loop โดยอิงจากคำตอบที่ยอมรับโดย @vidstige
public static class TypeExtentions
{
public static IEnumerable<T> Descendants<T>(this T root, Func<T, IEnumerable<T>> selector)
{
var nodes = new Stack<T>(new[] { root });
while (nodes.Any())
{
T node = nodes.Pop();
yield return node;
foreach (var n in selector(node)) nodes.Push(n);
}
}
public static IEnumerable<T> Descendants<T>(this IEnumerable<T> encounter, Func<T, IEnumerable<T>> selector)
{
var nodes = new Stack<T>(encounter);
while (nodes.Any())
{
T node = nodes.Pop();
yield return node;
if (selector(node) != null)
foreach (var n in selector(node))
nodes.Push(n);
}
}
}
ให้คอลเลกชันหนึ่งสามารถใช้เช่นนี้
var myNode = ListNodes.Descendants(x => x.Children).Where(x => x.Key == SomeKey);
หรือด้วยวัตถุราก
var myNode = root.Descendants(x => x.Children).Where(x => x.Key == SomeKey);