Ejemplo n.º 1
0
        //------------------- HELPER METHODS ---------------------

        //gets the next item in an in-order search.
        private T inOrderNext()
        {
            BinaryNode <T> last = my_element_stack.pop();

            if (last.stack_turn == 0) //first time off the stack
            {
                //put last back on the stack and mark as visited
                my_element_stack.push(last);
                last.stack_turn++;

                if (last.left != null)
                {
                    my_element_stack.push(last.left);
                }
                return(inOrderNext());
            }
            else //stack_turn == 1
            {
                if (last.right != null)
                {
                    my_element_stack.push(last.right);
                }

                last.stack_turn = 0;
                my_previous     = my_current;
                my_current      = last;
                return(last.value);
            }
        }
Ejemplo n.º 2
0
        //------------------- HELPER METHODS ----------------------

        private void addStackItems(BasicStack <DSString> the_stack)
        {
            for (int i = 0; i < 10; i++)
            {
                the_stack.push(new DSString(i.ToString()));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sets up the iterate with a traversal method, an initial node to traverse from and a link
        /// to the parent binary search tree.
        /// </summary>
        /// <param name="the_traversal_method">the method of traversing the tree.</param>
        /// <param name="the_parent_root">the root of the tree.</param>
        /// <param name="the_parent">link to the parent tree itself.</param>
        public BSTIterator(GraphTraversals the_traversal_method, BinaryNode <T> the_source_node, BinarySearchTree <T> the_parent)
        {
            my_traversal_method = the_traversal_method;
            my_parent           = the_parent;

            //prepare stack for traversal
            my_element_stack = new Stack <BinaryNode <T> >(my_parent.size());
            my_element_queue = new Queue <BinaryNode <T> >();

            //add the first element if it is there
            if (my_parent.root != null)
            {
                my_element_stack.push(the_source_node);
                my_element_queue.enqueue(the_source_node);
            }
        }
Ejemplo n.º 4
0
        private void testClear(BasicStack <DSString> the_stack)
        {
            //make sure size() and isEmpty() are correct before clear
            Assert.AreEqual(false, the_stack.isEmpty());
            Assert.AreEqual(10, the_stack.size());

            //after clearing
            the_stack.clear();
            Assert.AreEqual(true, the_stack.isEmpty());
            Assert.AreEqual(0, the_stack.size());

            //after readding and clearing
            the_stack.push(new DSString("13"));
            Assert.AreEqual(false, the_stack.isEmpty());
            Assert.AreEqual(1, the_stack.size());
        }
Ejemplo n.º 5
0
        private void testSize(BasicStack <DSString> the_stack)
        {
            //check after initial insertion
            Assert.AreEqual(10, the_stack.size());

            //check after removal
            the_stack.pop();
            Assert.AreEqual(9, the_stack.size());

            //check after addition
            the_stack.push(new DSString("13"));
            Assert.AreEqual(10, the_stack.size());

            //check after removing all
            the_stack.clear();
            Assert.AreEqual(0, the_stack.size());
        }
Ejemplo n.º 6
0
        private void testIsEmpty(BasicStack <DSString> the_stack)
        {
            //check while not empty
            Assert.IsFalse(the_stack.isEmpty());

            //after empty
            the_stack.clear();
            Assert.IsTrue(the_stack.isEmpty());

            //after re-addition
            the_stack.push(new DSString("1"));
            Assert.IsFalse(the_stack.isEmpty());

            //after empty again
            the_stack.clear();
            Assert.IsTrue(the_stack.isEmpty());
        }
Ejemplo n.º 7
0
        private void testPop(BasicStack <DSString> the_stack)
        {
            //make sure the ordering is secure
            Assert.AreEqual("9", the_stack.pop().value);
            Assert.AreEqual("8", the_stack.pop().value);
            Assert.AreEqual("7", the_stack.pop().value);

            //add an item after removing
            the_stack.push(new DSString("13"));

            //check ordering
            Assert.AreEqual("13", the_stack.pop().value);
            Assert.AreEqual("6", the_stack.pop().value);
            Assert.AreEqual("5", the_stack.pop().value);
            Assert.AreEqual("4", the_stack.pop().value);
            Assert.AreEqual("3", the_stack.pop().value);
            Assert.AreEqual("2", the_stack.pop().value);
            Assert.AreEqual("1", the_stack.pop().value);
            Assert.AreEqual("0", the_stack.pop().value);
        }
Ejemplo n.º 8
0
        private void testContains(BasicStack <DSString> the_stack)
        {
            //make sure all items are matched inside
            Assert.AreEqual(true, the_stack.contains(new DSString("0")));
            Assert.AreEqual(true, the_stack.contains(new DSString("1")));
            Assert.AreEqual(true, the_stack.contains(new DSString("2")));
            Assert.AreEqual(true, the_stack.contains(new DSString("3")));
            Assert.AreEqual(true, the_stack.contains(new DSString("4")));
            Assert.AreEqual(true, the_stack.contains(new DSString("5")));
            Assert.AreEqual(true, the_stack.contains(new DSString("6")));
            Assert.AreEqual(true, the_stack.contains(new DSString("7")));
            Assert.AreEqual(true, the_stack.contains(new DSString("8")));
            Assert.AreEqual(true, the_stack.contains(new DSString("9")));

            //check after removal
            the_stack.pop();
            Assert.AreEqual(false, the_stack.contains(new DSString("9")));

            //check after adding
            the_stack.push(new DSString("13"));
            Assert.AreEqual(true, the_stack.contains(new DSString("13")));

            //check after removing all
            the_stack.clear();
            Assert.AreEqual(false, the_stack.contains(new DSString("13")));
            Assert.AreEqual(false, the_stack.contains(new DSString("0")));
            Assert.AreEqual(false, the_stack.contains(new DSString("1")));
            Assert.AreEqual(false, the_stack.contains(new DSString("2")));
            Assert.AreEqual(false, the_stack.contains(new DSString("3")));
            Assert.AreEqual(false, the_stack.contains(new DSString("4")));
            Assert.AreEqual(false, the_stack.contains(new DSString("5")));
            Assert.AreEqual(false, the_stack.contains(new DSString("6")));
            Assert.AreEqual(false, the_stack.contains(new DSString("7")));
            Assert.AreEqual(false, the_stack.contains(new DSString("8")));
            Assert.AreEqual(false, the_stack.contains(new DSString("9")));
        }