src/Shared/EntityFramework/Core/Common/Utils/Helpers.cs
// <summary>
// Uses a stack to non-recursively traverse a given tree structure and retrieve the leaf nodes.
// </summary>
// <typeparam name="TNode"> The type of each node in the tree structure </typeparam>
// <param name="root"> The node that represents the root of the tree </param>
// <param name="isLeaf"> A function that determines whether or not a given node should be considered a leaf node </param>
// <param name="getImmediateSubNodes">
// A function that traverses the tree by retrieving the <b>immediate</b> descendants of a (non-leaf) node.
// </param>
// <returns>
// An enumerable containing the leaf nodes (as determined by <paramref name="isLeaf" /> ) retrieved by traversing the tree from
// <paramref
//     name="root" />
// using <paramref name="getImmediateSubNodes" /> .
// </returns>
Uses a stack to non-recursively traverse a given tree structure and retrieve the leaf nodes.
Uses a stack to non - recursively traverse a given tree structure and retrieve the leaf nodes .
internal static IEnumerable<TNode> GetLeafNodes<TNode>(
            TNode root, Func<TNode, bool> isLeaf, Func<TNode, IEnumerable<TNode>> getImmediateSubNodes)
        {
            DebugCheck.NotNull(isLeaf);
            DebugCheck.NotNull(getImmediateSubNodes);
            var nodes = new Stack<TNode>();
            nodes.Push(root);
            while (nodes.Count > 0)
            {
                var current = nodes.Pop();
                if (isLeaf(current))
                {
                    yield return current;
                }
                else
                {
                    var childNodes = new List<TNode>(getImmediateSubNodes(current));
                    for (var idx = childNodes.Count - 1; idx > -1; idx--)
                    {
                        nodes.Push(childNodes[idx]);
                    }
                }
            }
        }
internal static IEnumerable < TNode > GetLeafNodes < TNode > ( TNode root , Func < TNode , bool > isLeaf , Func < TNode , IEnumerable < TNode > > getImmediateSubNodes ) { DebugCheck . NotNull ( isLeaf ) ; DebugCheck . NotNull ( getImmediateSubNodes ) ; var nodes = new Stack < TNode > ( ) ; nodes . Push ( root ) ; while ( nodes . Count > 0 ) { var current = nodes . Pop ( ) ; if ( isLeaf ( current ) ) { yield return current ; } else { var childNodes = new List < TNode > ( getImmediateSubNodes ( current ) ) ; for ( var idx = childNodes . Count - 1 ; idx > - 1 ; idx -- ) { nodes . Push ( childNodes [ idx ] ) ; } } } }
Uses a stack to non-recursively traverse a given tree structure and retrieve the leaf nodes.
Uses a stack to non - recursively traverse a given tree structure and retrieve the leaf nodes .
An enumerable containing the leaf nodes (as determined by  ) retrieved by traversing the tree from

using  .
An enumerable containing the leaf nodes ( as determined by ) retrieved by traversing the tree from using .
root
TNode
The node that represents the root of the tree
The node that represents the root of the tree
isLeaf
Func<TNode, bool>
A function that determines whether or not a given node should be considered a leaf node
A function that determines whether or not a given node should be considered a leaf node
getImmediateSubNodes
Func<TNode, IEnumerable<TNode>>
A function that traverses the tree by retrieving the immediate
A function that traverses the tree by retrieving the immediate
typeparam
The type of each node in the tree structure
The type of each node in the tree structure