Searching a tree using LINQ

C#.NetLinq

C# Problem Overview


I have a tree created from this class.

class Node
{
    public string Key { get; }
    public List<Node> Children { get; }
}

I want to search in all children and all their children to get the ones matching a condition:

node.Key == SomeSpecialKey

How can I implement it?

C# Solutions


Solution 1 - C#

It's a misconception that this requires recursion. It will require a stack or a queue and the easiest way is to implement it using recursion. For sake of completeness I'll provide a non-recursive answer.

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);
    }
}

Use this expression for example to use it:

root.Descendants().Where(node => node.Key == SomeSpecialKey)

Solution 2 - C#

Searching a Tree of Objects with 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;
        }

    }
}

Solution 3 - C#

If you want to maintain Linq like syntax, you can use a method to obtain all the descendants (children + children's children etc.)

static class NodeExtensions
{
    public static IEnumerable<Node> Descendants(this Node node)
    {
        return node.Children.Concat(node.Children.SelectMany(n => n.Descendants()));
    }
}

This enumerable can then be queried like any other using where or first or whatever.

Solution 4 - C#

You can try this extension method to enumerate the tree nodes:

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;
    }
}

Then use that with a Where() clause:

var matchingNodes = rootNode.GetTreeNodes().Where(x => x.Key == SomeSpecialKey);

Solution 5 - C#

Why not use an IEnumerable<T> extension method

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;
        }
    }
}

then just do this

var result = nodes.Children.SelectHierarchy(n => n.Children, n => n.Key.IndexOf(searchString) != -1);

Solution 6 - C#

Perhaps you need just

node.Children.Where(child => child.Key == SomeSpecialKey)

Or, if you need to search one level deeper,

node.Children.SelectMany(
        child => child.Children.Where(child => child.Key == SomeSpecialKey))

If you need to search on all levels, take the following:

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)));
}

Solution 7 - C#

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;
        }
    }

And then you can search like:

    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");

Solution 8 - C#

A while back I wrote a codeproject article which describes how to use Linq to query tree-like structures:

http://www.codeproject.com/KB/linq/LinqToTree.aspx

This provides a linq-to-XML style API where you can search descendants, children, ancestors etc...

Probably overkill for your current problem, but might be of interest to others.

Solution 9 - C#

You can use this extension method to query the tree.

    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;
    }

Solution 10 - C#

I have a generic extension method that can flatten any IEnumerable<T> and from that flattened collection, you can get the node you want.

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;
            }
        }
    }
}

Use this like this:

var q = from node in myTree.FlattenHierarchy(x => x.Children)
        where node.Key == "MyKey"
        select node;
var theNode = q.SingleOrDefault();

Solution 11 - C#

I use the following implementations for enumerating Tree items

    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 in implementation above uses queue of node sequences instead of nodes queue. This is not classic BFS algorithm way.

Solution 12 - C#

And just for fun (almost a decade later) an answer also using Generics but with a Stack and While loop, based off the accepted answer by @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);
        }
    }
}

Given a collection one can use like this

        var myNode = ListNodes.Descendants(x => x.Children).Where(x => x.Key == SomeKey);

or with a root object

        var myNode = root.Descendants(x => x.Children).Where(x => x.Key == SomeKey);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUfuk HacıoğullarıView Question on Stackoverflow
Solution 1 - C#vidstigeView Answer on Stackoverflow
Solution 2 - C#CD..View Answer on Stackoverflow
Solution 3 - C#ForbesLindesayView Answer on Stackoverflow
Solution 4 - C#dlevView Answer on Stackoverflow
Solution 5 - C#Dean ChalkView Answer on Stackoverflow
Solution 6 - C#VladView Answer on Stackoverflow
Solution 7 - C#Varun ChatterjiView Answer on Stackoverflow
Solution 8 - C#ColinEView Answer on Stackoverflow
Solution 9 - C#BitKFuView Answer on Stackoverflow
Solution 10 - C#Mikael ÖstbergView Answer on Stackoverflow
Solution 11 - C#Valentine ZakharenkoView Answer on Stackoverflow
Solution 12 - C#George AlbertynView Answer on Stackoverflow