Accord.MachineLearning.DecisionTrees.DecisionTreeTraversal.PostOrder C# (CSharp) Method

PostOrder() public static method

Post-order tree traversal method.
Adapted from John Cowan (1998) recommendation.
public static PostOrder ( Accord.MachineLearning.DecisionTrees.DecisionNode tree ) : IEnumerator
tree Accord.MachineLearning.DecisionTrees.DecisionNode
return IEnumerator
        public static IEnumerator<DecisionNode> PostOrder(DecisionNode tree)
        {
            var cursor = new Dictionary<DecisionNode, int>();

            DecisionNode currentNode = tree;

            while (currentNode != null)
            {
                // Move down to first child
                DecisionNode nextNode = null;
                if (currentNode.Branches != null)
                    nextNode = currentNode.Branches.FirstOrDefault();

                if (nextNode != null)
                {
                    currentNode = nextNode;
                    continue;
                }

                // No child nodes, so walk tree
                while (currentNode != null)
                {
                    yield return currentNode; // post order

                    // Move to sibling if possible.
                    nextNode = GetNextSibling(cursor, currentNode);

                    if (nextNode != null)
                    {
                        currentNode = nextNode;
                        break;
                    }

                    // Move up
                    if (currentNode == nextNode)
                        currentNode = null;
                    else
                        currentNode = currentNode.Parent;
                }
            }
        }
    }