コード例 #1
0
        public void NonEmptyStackHasCorrectEmptyFlag()
        {
            var s = new StackViaLinkedList <int>();

            s.Push(34);
            Assert.False(s.IsEmpty());
        }
コード例 #2
0
        public static string InOrderTraversalReturnAsString(BinaryTreeNode <int> treeNode)
        {
            StringBuilder returnString = new StringBuilder();
            StackViaLinkedList <BinaryTreeNode <int> > st = new StackViaLinkedList <BinaryTreeNode <int> >();

            st.Push(treeNode);
            BinaryTreeNode <int> currentNode = treeNode;
            bool shouldCheckLeft             = true;

            while (!st.IsEmpty())
            {
                while (currentNode.Left != null && shouldCheckLeft)
                {
                    st.Push(currentNode.Left);
                    currentNode = currentNode.Left;
                }
                currentNode     = st.Pop().Data;
                shouldCheckLeft = false;
                returnString.Append(currentNode.Data + " ");
                if (currentNode.Right != null)
                {
                    st.Push(currentNode.Right);
                    currentNode     = currentNode.Right;
                    shouldCheckLeft = true;
                }
            }
            return(returnString.ToString());
        }
コード例 #3
0
        public static void InOrderTraversalIterative(BinarySearchTreeNode <int> treeNode)
        {
            StackViaLinkedList <BinarySearchTreeNode <int> > st = new StackViaLinkedList <BinarySearchTreeNode <int> >();

            st.Push(treeNode);
            BinarySearchTreeNode <int> currentNode = treeNode;
            bool shouldCheckLeft = true;

            while (!st.IsEmpty())
            {
                while (currentNode.Left != null && shouldCheckLeft)
                {
                    st.Push(currentNode.Left);
                    currentNode = currentNode.Left;
                }
                currentNode     = st.Pop().Data;
                shouldCheckLeft = false;
                Console.Write(currentNode.Data + " ");
                if (currentNode.Right != null)
                {
                    st.Push(currentNode.Right);
                    currentNode     = currentNode.Right;
                    shouldCheckLeft = true;
                }
            }
        }
コード例 #4
0
        public void EmptyFlagIsCorectAfterStackBecomesEmpty()
        {
            var s = new StackViaLinkedList <int>();

            s.Push(7);
            s.Pop();
            Assert.True(s.IsEmpty());
        }
コード例 #5
0
        public void NewStackHasCorrectEmptyFlag()
        {
            var s = new StackViaLinkedList <int>();

            Assert.True(s.IsEmpty());
        }