Beispiel #1
0
        /// <summary>
        /// Returns an iterator that traverses the descendant nodes breadth first, top down.
        /// </summary>
        /// <value>The descendant node iterator.</value>
        public IEnumerable <ITreeNode <T> > DescendantNodesBreadthFirst(bool includeSelf)
        {
            Queue <ITreeNode <T> > q = new Queue <ITreeNode <T> >();

            #region Prime the queue
            if (includeSelf)
            {
                q.Enqueue(this);
            }
            else
            {
                foreach (ITreeNode <T> child in Children)
                {
                    q.Enqueue(child);
                }
            }
            #endregion

            // For every node in the queue, dequeue it, enqueue all of its children, and yield-return it.
            while (q.Count > 0)
            {
                ITreeNode <T> node = q.Dequeue();
                node.ForEachChild(delegate(ITreeNode <T> tn) { q.Enqueue(tn); });
                yield return(node);
            }
        }