Beispiel #1
0
        /// <summary>
        /// Starting from a binary tree node, traverse depth-first by pre-order and perform an action.
        /// </summary>
        /// <param name="node">The node to start a traversal.</param>
        /// <param name="doFuncVisit">The action to perform on the node.</param>
        public void DepthFirstPreOrderIterative(TNode node, Action <TNode> doFuncVisit)
        {
            if (node == null || doFuncVisit == null)
            {
                return;
            }

            StackGeneric <TNode> stackNodes = new StackGeneric <TNode>();

            while (node != null || !stackNodes.IsEmpty)
            {
                doFuncVisit(node);

                if (node.Right != null)
                {
                    stackNodes.Push(node.Right as TNode);
                }

                if (node.Left != null)
                {
                    stackNodes.Push(node.Left as TNode);
                }

                node = stackNodes.Pop();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Starting from a binary tree node, traverse depth-first by post-order and perform an action.
        /// </summary>
        /// <param name="node">The node to start a traversal.</param>
        /// <param name="doFuncVisit">The action to perform on the node.</param>
        public void DepthFirstPostOrderIterative(TNode node, Action <TNode> doFuncVisit)
        {
            if (node == null || doFuncVisit == null)
            {
                return;
            }

            TNode nodeLastVisted            = null;
            StackGeneric <TNode> stackNodes = new StackGeneric <TNode>();

            while (node != null || !stackNodes.IsEmpty)
            {
                if (node != null)
                {
                    stackNodes.Push(node);
                    node = node.Left as TNode;
                }
                else //// traverse to right
                {
                    TNode peekNode = stackNodes.Peek();

                    if (peekNode.Right != null && peekNode.Right != nodeLastVisted)
                    {
                        node = peekNode.Right as TNode;
                    }
                    else
                    {
                        stackNodes.Pop();
                        doFuncVisit(peekNode);
                        nodeLastVisted = peekNode;
                    }
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Starting from a binary tree node, traverse depth-first by in-order and add to a sequential list.
        /// </summary>
        /// <param name="node">The node to start a traversal.</param>
        /// <param name="sequentializedTraverseList">The sequential list.</param>
        public void DepthFirstInOrderIterative(TNode node, List <TNode> sequentializedTraverseList)
        {
            if (sequentializedTraverseList == null)
            {
                sequentializedTraverseList = new List <TNode>();
            }

            StackGeneric <TNode> stackNodes = new StackGeneric <TNode>();

            while (node != null || !stackNodes.IsEmpty)
            {
                if (node != null)
                {
                    stackNodes.Push(node);
                    node = node.Left as TNode;
                }
                else //// LIFO vist from the stack
                {
                    node = stackNodes.Pop();
                    sequentializedTraverseList.Add(node);
                    node = node.Right as TNode;
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Starting from a binary tree node, traverse depth-first by in-order and perform an action.
        /// </summary>
        /// <param name="node">The node to start a traversal.</param>
        /// <param name="doFuncVisit">The action to perform on the node.</param>
        public void DepthFirstInOrderIterative(TNode node, Action <TNode> doFuncVisit)
        {
            if (node == null || doFuncVisit == null)
            {
                return;
            }

            StackGeneric <TNode> stackNodes = new StackGeneric <TNode>();

            while (node != null || !stackNodes.IsEmpty)
            {
                if (node != null)
                {
                    stackNodes.Push(node);
                    node = node.Left as TNode;
                }
                else //// LIFO vist from the stack
                {
                    node = stackNodes.Pop();
                    doFuncVisit(node);
                    node = node.Right as TNode;
                }
            }
        }