Please send questions to
st10@humboldt.edu .
/* Cs132BinaryTreeTest.java 1.0 */
/* by Sharon Tuttle */
/* last modified: 4-3-03 */
public class Cs132BinaryTreeTest extends JPFalt
{
public static void main(String[] args)
{
new Cs132BinaryTreeTest();
}
/** Test suite for the class Cs132BinaryTree */
/*--------------------------------------------------------------------
Define several Cs132BinaryTree objects and print the member data values,
and test accessor and modifier methods
--------------------------------------------------------------------*/
void TestCs132BinaryTree()
{
println("\nDefine Cs132BinaryTree objects, print member data values");
// cannot simply create an empty binary tree, given
// this implementation of a binary tree...
// testing 1-argument constructor
String stringToAdd = new String("Root Tree Element");
Cs132BinaryTree myTree = new Cs132BinaryTree(stringToAdd);
expected("binary tree with 1 element, its root, Root Tree Element:");
actual(myTree);
expected("Root Tree Element");
actual(myTree.getValue());
println("");
// its left and right subtrees should be empty --- are they?
println("are its left and right subtrees empty?");
expected("" + null);
actual("" + myTree.getLeft());
expected("" + null);
actual("" + myTree.getRight());
println("");
// testing setLeft() and getLeft()
Integer intToAdd = new Integer(13);
Cs132BinaryTree left1 = new Cs132BinaryTree(intToAdd);
myTree.setLeft(left1);
println("binary tree with root and left subtree:");
expected("root: node with value Root Tree Element");
actual("root: " + myTree);
expected("left child: node with value 13");
actual("left child: " + myTree.getLeft());
println("");
// testing setRight() and getRight()
Integer intToAdd2 = new Integer(14);
Cs132BinaryTree right1 = new Cs132BinaryTree(intToAdd2);
myTree.setRight(right1);
println("binary tree with root,left, and right subtree:");
expected("root: node with value Root Tree Element");
actual("root: " + myTree);
expected("left child: node with value 13");
actual("left child: " + myTree.getLeft());
expected("right child: node with value 14");
actual("right child: " + myTree.getRight());
println("");
// test 3-argument constructor...
Cs132BinaryTree operand1 = new Cs132BinaryTree(new Integer(20));
Cs132BinaryTree operand2 = new Cs132BinaryTree(new Integer(30));
Cs132BinaryTree operation =
new Cs132BinaryTree(new Character('+'),
operand1,
operand2);
expected("+");
actual(operation.getValue());
expected(20);
actual(operation.getLeft().getValue());
expected(30);
actual(operation.getRight().getValue());
}
/*---------------------------------------------------------------
Test method for isEmpty()
-----------------------------------------------------------------*/
void isEmptyTest()
{
// remember: there is not a binary tree constructor that
// returns an empty tree in our particular implementation
testHeader("isEmpty");
String myEntry = new String("Hello");
Cs132BinaryTree myTree = new Cs132BinaryTree(myEntry);
expected(false);
actual(myTree.isEmpty());
// I cannot think how to test a true case of isEmpty()
// "publically" --- there are no publically accessible
// empty binary trees under this implementation...!
}
/*--------------------------------------------------------
Test method for valuesInOrder()
------------------------------------------------------------*/
public void valuesInOrderTest()
{
testHeader("valuesInOrder()");
Cs132BinaryTree operand1 = new Cs132BinaryTree(new Integer(30));
Cs132BinaryTree operand2 = new Cs132BinaryTree(new Integer(20));
Cs132BinaryTree operation1 = new Cs132BinaryTree(
new Character('+'), operand1, operand2);
Cs132BinaryTree operand3 = new Cs132BinaryTree(new Integer(4));
Cs132BinaryTree operand4 = new Cs132BinaryTree(new Integer(5));
Cs132BinaryTree operation2 = new Cs132BinaryTree(
new Character('*'), operand3, operand4);
Cs132BinaryTree expression = new Cs132BinaryTree(
new Character('-'), operation1, operation2);
expected("30 + 20 - 4 * 5");
actual(expression.valuesInOrder());
}
/*--------------------------------------------------------
Test method for valuesPreOrder()
------------------------------------------------------------*/
public void valuesPreOrderTest()
{
testHeader("valuesPreOrder()");
Cs132BinaryTree operand1 = new Cs132BinaryTree(new Integer(30));
Cs132BinaryTree operand2 = new Cs132BinaryTree(new Integer(20));
Cs132BinaryTree operation1 = new Cs132BinaryTree(
new Character('+'), operand1, operand2);
Cs132BinaryTree operand3 = new Cs132BinaryTree(new Integer(4));
Cs132BinaryTree operand4 = new Cs132BinaryTree(new Integer(5));
Cs132BinaryTree operation2 = new Cs132BinaryTree(
new Character('*'), operand3, operand4);
Cs132BinaryTree expression = new Cs132BinaryTree(
new Character('-'), operation1, operation2);
expected("- + 30 20 * 4 5");
actual(expression.valuesPreOrder());
}
/*--------------------------------------------------------
Test method for valuesPostOrder()
------------------------------------------------------------*/
public void valuesPostOrderTest()
{
testHeader("valuesPostOrder()");
Cs132BinaryTree operand1 = new Cs132BinaryTree(new Integer(30));
Cs132BinaryTree operand2 = new Cs132BinaryTree(new Integer(20));
Cs132BinaryTree operation1 = new Cs132BinaryTree(
new Character('+'), operand1, operand2);
Cs132BinaryTree operand3 = new Cs132BinaryTree(new Integer(4));
Cs132BinaryTree operand4 = new Cs132BinaryTree(new Integer(5));
Cs132BinaryTree operation2 = new Cs132BinaryTree(
new Character('*'), operand3, operand4);
Cs132BinaryTree expression = new Cs132BinaryTree(
new Character('-'), operation1, operation2);
expected("30 20 + 4 5 * -");
actual(expression.valuesPostOrder());
}
} // end of Cs132BinaryTreeTest