//------------------- 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); } }
private void testPeek(BasicStack <DSString> the_stack) { //check multiple peeks Assert.AreEqual("9", the_stack.peek().value); Assert.AreEqual("9", the_stack.peek().value); //peek after removal the_stack.pop(); Assert.AreEqual("8", the_stack.peek().value); //peek with nothing on the stack while (!the_stack.isEmpty()) { the_stack.pop(); } Assert.AreEqual(null, the_stack.peek()); }
private void testPush(BasicStack <DSString> the_stack) { //items already added in initialization //make sure first size is right Assert.AreEqual(10, the_stack.size()); //make sure first removal matches FIFO ordering Assert.AreEqual("9", the_stack.pop().value); //remove all elements and check last item to make sure removals match FIFO ordering string element = ""; while (!the_stack.isEmpty()) { element = the_stack.pop().value; } Assert.AreEqual("0", element); }
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()); }
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"))); }
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); }