示例#1
0
        private static void LevelOrder(
            ITraversing node, Visitor visitor, ICollection <ITraversing> visited)
        {
            if (node == null || visitor == null)
            {
                return;
            }
            var queue = new Queue <ITraversing>(new[] { node });

            while (queue.Count > 0)
            {
                var next = queue.Dequeue();
                TraversingHelper.CheckCircularity(visited, next);
                if (visitor(next))
                {
                    return;
                }
                next.Traverse(child =>
                {
                    if (child != null)
                    {
                        queue.Enqueue(child);
                    }
                    return(false);
                });
            }
        }
示例#2
0
 private static bool PostOrder(
     ITraversing node, Visitor visitor, ICollection <ITraversing> visited)
 {
     TraversingHelper.CheckCircularity(visited, node);
     if (node == null || visitor == null)
     {
         return(true);
     }
     node.Traverse(child => PostOrder(child, visitor, visited));
     return(visitor(node));
 }
示例#3
0
        private static void ReverseLevelOrder(
            ITraversing node, Visitor visitor, ICollection <ITraversing> visited)
        {
            if (node == null || visitor == null)
            {
                return;
            }
            var queue = new Queue <ITraversing>(new[] { node });
            var stack = new Stack <ITraversing>();

            while (queue.Count > 0)
            {
                var next = queue.Dequeue();
                TraversingHelper.CheckCircularity(visited, next);
                stack.Push(next);
                var level = new List <ITraversing>();
                next.Traverse(child =>
                {
                    if (child != null)
                    {
                        level.Insert(0, child);
                    }
                    return(false);
                });
                foreach (var l in level)
                {
                    queue.Enqueue(l);
                }
            }
            while (stack.Count > 0)
            {
                if (visitor(stack.Pop()))
                {
                    return;
                }
            }
        }