예제 #1
0
        private void PostOrder(Node node, PerformFunction function, bool includeRoot)
        {
            var stack       = new Stack <Node>();
            var outputStack = new Stack <Node>();

            if (includeRoot)
            {
                stack.Push(node);
                while (stack.Count > 0)
                {
                    var currentNode = stack.Pop();
                    outputStack.Push(currentNode);

                    for (int i = 0; i < currentNode.Children.Count; i++)
                    {
                        stack.Push(currentNode.Children[i]);
                    }
                }
            }
            else
            {
                for (int i = 0; i < node.Children.Count; i++)
                {
                    stack.Push(node.Children[i]);
                }

                while (stack.Count > 0)
                {
                    var currentNode = stack.Pop();
                    outputStack.Push(currentNode);

                    for (int i = 0; i < currentNode.Children.Count; i++)
                    {
                        stack.Push(currentNode.Children[i]);
                    }
                }
            }

            while (outputStack.Count > 0)
            {
                _nodelist.Add(outputStack.Pop());
                function(node);
            }
        }
예제 #2
0
        private void LevelOrder(Node node, PerformFunction function, bool includeRoot)
        {
            var queue = new Queue <Node>();

            if (includeRoot)
            {
                _nodelist.Add(node);
            }

            function(node);
            queue.Enqueue(node);
            while (queue.Count > 0)
            {
                foreach (Node currentNode in queue.Dequeue().Children)
                {
                    _nodelist.Add(currentNode);
                    function(node);
                    queue.Enqueue(currentNode);
                }
            }
        }
예제 #3
0
        private void PreOrder(Node node, PerformFunction function, bool includeRoot)
        {
            var stack = new Stack <Node>();

            if (includeRoot)
            {
                stack.Push(node);
                while (stack.Count > 0)
                {
                    var currentNode = stack.Pop();
                    _nodelist.Add(currentNode);
                    function(currentNode);
                    for (int i = currentNode.Children.Count - 1; i >= 0; i--)
                    {
                        stack.Push(currentNode.Children[i]);
                    }
                }
            }
            else
            {
                for (int i = node.Children.Count - 1; i >= 0; i--)
                {
                    stack.Push(node.Children[i]);
                }
                while (stack.Count > 0)
                {
                    var currentNode = stack.Pop();
                    _nodelist.Add(currentNode);
                    function(currentNode);
                    for (int i = currentNode.Children.Count - 1; i >= 0; i--)
                    {
                        stack.Push(currentNode.Children[i]);
                    }
                }
            }
        }
예제 #4
0
 public PreOrderIterator(Node node, PerformFunction function, bool includeRoot = true) : base(node)
 {
     PreOrder(_node, function, includeRoot);
 }