コード例 #1
0
        public void IterativePrintInOrder(MyBinaryTreeNode <T> rootNode)
        {
            var stack       = new MyStack <MyBinaryTreeNode <T> >();
            var currentNode = rootNode;

            while (stack.Count > 0 || currentNode != null)
            {
                if (currentNode != null)
                {
                    stack.Push(currentNode);
                    currentNode = currentNode.Left;
                }
                else
                {
                    var node = stack.Pop();
                    Console.WriteLine(node.Value);
                    currentNode = node.Right;
                }
            }
        }
コード例 #2
0
        public void IterativePrintPreOrder(MyBinaryTreeNode <T> rootNode)
        {
            var stack = new MyStack <MyBinaryTreeNode <T> >();

            stack.Push(rootNode);

            while (stack.Count > 0)
            {
                var node = stack.Pop();
                Console.WriteLine(node.Value);

                if (node.Right != null)
                {
                    stack.Push(node.Right);
                }

                if (node.Left != null)
                {
                    stack.Push(node.Left);
                }
            }
        }