Rev 34 | Rev 36 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
// Written by Ira Snyder
// Due Date: 11-15-2004
// Project #3
import java.io.*;
class Driver {
public static void main ( String [] args ) throws Exception {
}
public static void run_BinaryTree_tests() {
System.out.println( test_12_2() );
System.out.println( test_12_3() );
System.out.println( test_12_4() );
System.out.println( test_12_5() );
System.out.println( test_12_6() );
System.out.println( test_12_7() );
System.out.println( test_12_8() );
System.out.println( test_12_9() );
System.out.println( test_12_10());
System.out.println( test_12_11());
System.out.println( test_12_12());
System.out.println( test_12_13());
System.out.println( test_12_14());
System.out.println( test_12_15());
System.out.println( test_12_16());
System.out.println(); //print a blank line
test_traversals();
} //end main test method
public static BinaryTree createTestTree() {
BinaryTree treeB = new BinaryTree("B");
BinaryTree treeD = new BinaryTree("D");
BinaryTree treeE = new BinaryTree("E");
BinaryTree treeC = new BinaryTree("C",treeD,treeE);
BinaryTree treeA = new BinaryTree("A",treeB,treeC);
return treeA;
}
public static String test_12_2() {
BinaryTree treeA = createTestTree();
String experimentalResult = treeA.toString();
String correctResult = "((B),A,((D),C,(E)))";
if( correctResult.equals(experimentalResult) )
return "test_12_2: PASSED";
return "test_12_2: *** FAILED ***";
}
public static String test_12_3() {
BinaryTree treeA = createTestTree();
BinaryTree treeAleft = treeA.getLeft();
if( treeA.isLeaf() == false && treeAleft.isLeaf() == true )
return "test_12_3: PASSED";
return "test_12_3: *** FAILED ***";
}
public static String test_12_4() {
BinaryTree treeA = createTestTree();
int correctResult = 5;
int experimentalResult = treeA.size();
if( correctResult == experimentalResult )
return "test_12_4: PASSED";
return "test_12_4: *** FAILED ***";
}
public static String test_12_5() {
BinaryTree treeA = createTestTree();
int correctResult = 2;
int experimentalResult = treeA.height();
if( correctResult == experimentalResult )
return "test_12_5: PASSED";
return "test_12_5: *** FAILED ***";
}
public static String test_12_6() {
BinaryTree treeA = createTestTree();
if( treeA.contains("B") &&
treeA.contains("A") &&
treeA.contains("D") &&
!treeA.contains("Z") ) return "test_12_6: PASSED";
return "test_12_6: *** FAILED ***";
}
public static String test_12_7() {
BinaryTree treeA = createTestTree();
int correctResult = 3;
if( correctResult == treeA.numLeaves() )
return "test_12_7: PASSED";
return "test_12_7: *** FAILED ***";
}
public static String test_12_8() {
BinaryTree treeA = createTestTree();
BinaryTree tree = new BinaryTree("E",treeA,new BinaryTree("E"));
int correctResult = 3;
if( correctResult == tree.count("E") )
return "test_12_8: PASSED";
return "test_12_8: *** FAILED ***";
}
public static String test_12_9() {
BinaryTree treeA = createTestTree();
if( treeA.isFull() == false ) { return "test_12_9: PASSED"; }
return "test_12_9: *** FAILED ***";
}
public static String test_12_10() {
BinaryTree treeA = createTestTree();
if( treeA.isBalanced() == true ) { return "test_12_10: PASSED"; }
return "test_12_10: *** FAILED ***";
}
public static String test_12_11() {
BinaryTree treeA = createTestTree();
if( treeA.pathLength() == 6 ) { return "test_12_11: PASSED"; }
return "test_12_11: *** FAILED *** val:" + treeA.pathLength();
}
public static String test_12_12() {
BinaryTree treeA = createTestTree();
String correctAnswer = "(((E),C,(D)),A,(B))";
if( treeA.reverse().toString().equals(correctAnswer) ) {
return "test_12_12: PASSED";
}
return "test_12_12: *** FAILED *** " + treeA.reverse();
}
public static String test_12_13() {
BinaryTree treeA = createTestTree();
if( treeA.level("E") == 2 ) { return "test_12_13: PASSED"; }
return "test_12_13: *** FAILED ***";
}
public static String test_12_14() {
BinaryTree treeA = createTestTree();
BinaryTree treeB = new BinaryTree("B");
if( treeA.isDisjointFrom(treeB) ) {
return "test_12_14: *** FAILED ***";
}
return "test_12_14: PASSED";
}
public static String test_12_15() {
BinaryTree treeA = createTestTree();
if( treeA.isValid() ) { return "test_12_15: PASSED"; }
return "test_12_15: *** FAILED ***";
}
public static String test_12_16() {
BinaryTree tree1 = createTestTree();
BinaryTree tree2 = createTestTree();
if( tree1.equals(tree2) ) { return "test_12_16: PASSED"; }
return "test_12_16: *** FAILED ***";
}
public static void test_traversals() {
BinaryTree treeA = createTestTree();
//print the correct answers
System.out.println("Should Be:");
System.out.println("PreOrder: A B C D E");
System.out.println("PostOrder: B D E C A");
System.out.println("LevelOrder: A B C D E");
System.out.println("InOrder: B A D C E");
//print the actual answers
System.out.println();
System.out.println("Actually is:");
System.out.print("PreOrder: "); BinaryTree.preOrderPrint(treeA);
System.out.println();
System.out.print("PostOrder: "); BinaryTree.postOrderPrint(treeA);
System.out.println();
System.out.print("LevelOrder: "); BinaryTree.levelOrderPrint(treeA);
System.out.println();
System.out.print("InOrder: "); BinaryTree.inOrderPrint(treeA);
System.out.println();
}
} //end class Driver