Пример #1
0
        public void TaverseListInorderNonRecursive(BinaryTreeNode root)
        {
            StackUsingLL <BinaryTreeNode> stage = new StackUsingLL <BinaryTreeNode>();

            while (true)
            {
                while (root != null)
                {
                    stage.Push(root);
                    root = root.left;
                }
                if (stage.IsEmpty())
                {
                    break;
                }

                root = stage.Pop();
                resultListInOrderNonRecursive.InsertAtEnd(root.data);
                root = root.right;
            }
        }
Пример #2
0
        public void TraverseListPostOrderNonRecursive(BinaryTreeNode root)
        {
            StackUsingLL <BinaryTreeNode> stage = new StackUsingLL <BinaryTreeNode>();

            while (true)
            {
                if (root != null)
                {
                    stage.Push(root);
                    root = root.left;
                }
                else
                {
                    if (stage.IsEmpty())
                    {
                        break;
                    }
                    else if (stage.PeekTop().right == null)
                    {
                        root = stage.Pop();
                        resultListPostOrderNonRecursive.InsertAtEnd(root.data);
                        if (root == stage.PeekTop().right)
                        {
                            resultListPostOrderNonRecursive.InsertAtEnd(stage.PeekTop().data);
                            stage.Pop();
                        }
                    }
                    if (!stage.IsEmpty())
                    {
                        root = stage.PeekTop().right;
                    }
                    else
                    {
                        root = null;
                    }
                }
            }
        }