Example #1
0
        private AStack <int> InitStack()
        {
            AStack <int> stack = new AStack <int>();

            stack.Push(1);
            stack.Push(2);
            return(stack);
        }
Example #2
0
        /// <summary>
        /// A non-recursive method using a stack to remove recursion
        /// Will be called by GetEnumerator
        /// </summary>
        /// <remarks>
        /// This method took me a while to get my head around it.
        /// Stepping through the code with the debugger helped make it click.
        /// </remarks>
        /// <returns>An IEnumerator<T></returns>
        private IEnumerator <T> InOrderTraversal()
        {
            if (_head != null)
            {
                // Used to store nodes we skip
                AStack <BinaryTreeNode <T> > stack = new AStack <BinaryTreeNode <T> >();

                BinaryTreeNode <T> current = _head;

                // When not using recursion, we need to keep track of whether we
                // should be processing left or right nodes
                bool goLeftNext = true;

                // Push the current node on the stack
                stack.Push(current);

                while (stack.Count > 0)
                {
                    // If heading left
                    if (goLeftNext)
                    {
                        // Push all but the left-most node on the stack
                        // yield the left-most after this block
                        while (current.Left != null)
                        {
                            stack.Push(current);
                            current = current.Left;
                        }
                    }

                    // InOrder is left->yield->right
                    yield return(current.Value);

                    // If we can go right, do it
                    if (current.Right != null)
                    {
                        current = current.Right;

                        // Once we've gone right once, start going left again
                        goLeftNext = true;
                    }
                    else
                    {
                        // If can't go right, pop off parent node
                        // so it can be processed and then go to its right side
                        current    = stack.Pop();
                        goLeftNext = false;
                    }
                }
            }
        }
 public void isEmptyTest()
 {
     Assert.IsTrue(intStack.IsEmpty());
     intStack.Push(2);
     Assert.IsFalse(intStack.IsEmpty());
 }