| 28 |
irasnyd |
1 |
// Written by Ira Snyder
|
|
|
2 |
// Due Date: 11-15-2004
|
|
|
3 |
// Project #3
|
|
|
4 |
import java.io.*;
|
|
|
5 |
|
|
|
6 |
class Driver {
|
| 29 |
irasnyd |
7 |
public static void main ( String [] args ) throws Exception {
|
|
|
8 |
|
|
|
9 |
System.out.println( test_12_2() );
|
| 31 |
irasnyd |
10 |
System.out.println( test_12_3() );
|
|
|
11 |
System.out.println( test_12_4() );
|
|
|
12 |
System.out.println( test_12_5() );
|
| 32 |
irasnyd |
13 |
System.out.println( test_12_6() );
|
|
|
14 |
System.out.println( test_12_7() );
|
|
|
15 |
System.out.println( test_12_8() );
|
|
|
16 |
|
| 29 |
irasnyd |
17 |
} //end main
|
| 28 |
irasnyd |
18 |
|
| 30 |
irasnyd |
19 |
public static BinaryTree createTestTree() {
|
| 29 |
irasnyd |
20 |
BinaryTree treeB = new BinaryTree("B");
|
|
|
21 |
BinaryTree treeD = new BinaryTree("D");
|
|
|
22 |
BinaryTree treeE = new BinaryTree("E");
|
|
|
23 |
BinaryTree treeC = new BinaryTree("C",treeD,treeE);
|
|
|
24 |
BinaryTree treeA = new BinaryTree("A",treeB,treeC);
|
|
|
25 |
|
| 30 |
irasnyd |
26 |
return treeA;
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
public static String test_12_2() {
|
|
|
30 |
BinaryTree treeA = createTestTree();
|
|
|
31 |
|
| 29 |
irasnyd |
32 |
String experimentalResult = treeA.toString();
|
|
|
33 |
String correctResult = "((B),A,((D),C,(E)))";
|
|
|
34 |
|
| 30 |
irasnyd |
35 |
if( correctResult.equals(experimentalResult) )
|
|
|
36 |
return "test_12_2: PASSED";
|
|
|
37 |
|
|
|
38 |
return "test_12_2: *** FAILED ***";
|
| 29 |
irasnyd |
39 |
}
|
| 31 |
irasnyd |
40 |
|
|
|
41 |
public static String test_12_3() {
|
|
|
42 |
BinaryTree treeA = createTestTree();
|
|
|
43 |
BinaryTree treeAleft = treeA.getLeft();
|
| 29 |
irasnyd |
44 |
|
| 31 |
irasnyd |
45 |
if( treeA.isLeaf() == false && treeAleft.isLeaf() == true )
|
|
|
46 |
return "test_12_3: PASSED";
|
| 29 |
irasnyd |
47 |
|
| 31 |
irasnyd |
48 |
return "test_12_3: *** FAILED ***";
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public static String test_12_4() {
|
|
|
52 |
BinaryTree treeA = createTestTree();
|
|
|
53 |
int correctResult = 5;
|
|
|
54 |
int experimentalResult = treeA.size();
|
|
|
55 |
|
|
|
56 |
if( correctResult == experimentalResult )
|
|
|
57 |
return "test_12_4: PASSED";
|
|
|
58 |
|
|
|
59 |
return "test_12_4: *** FAILED ***";
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public static String test_12_5() {
|
|
|
63 |
BinaryTree treeA = createTestTree();
|
|
|
64 |
int correctResult = 2;
|
|
|
65 |
int experimentalResult = treeA.height();
|
|
|
66 |
|
|
|
67 |
if( correctResult == experimentalResult )
|
|
|
68 |
return "test_12_5: PASSED";
|
|
|
69 |
|
|
|
70 |
return "test_12_5: *** FAILED ***";
|
|
|
71 |
}
|
|
|
72 |
|
| 32 |
irasnyd |
73 |
public static String test_12_6() {
|
|
|
74 |
BinaryTree treeA = createTestTree();
|
|
|
75 |
|
|
|
76 |
if( treeA.contains("B") &&
|
|
|
77 |
treeA.contains("A") &&
|
|
|
78 |
treeA.contains("D") &&
|
|
|
79 |
!treeA.contains("Z") ) return "test_12_6: PASSED";
|
|
|
80 |
|
|
|
81 |
return "test_12_6: *** FAILED ***";
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public static String test_12_7() {
|
|
|
85 |
BinaryTree treeA = createTestTree();
|
|
|
86 |
int correctResult = 3;
|
|
|
87 |
|
|
|
88 |
if( correctResult == treeA.numLeaves() )
|
|
|
89 |
return "test_12_7: PASSED";
|
|
|
90 |
|
|
|
91 |
return "test_12_7: *** FAILED ***";
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
public static String test_12_8() {
|
|
|
95 |
BinaryTree treeA = createTestTree();
|
|
|
96 |
BinaryTree tree = new BinaryTree("E",treeA,new BinaryTree("E"));
|
|
|
97 |
|
|
|
98 |
int correctResult = 3;
|
|
|
99 |
|
|
|
100 |
if( correctResult == tree.count("E") )
|
|
|
101 |
return "test_12_8: PASSED";
|
|
|
102 |
|
|
|
103 |
return "test_12_8: *** FAILED ***";
|
|
|
104 |
}
|
|
|
105 |
|
| 28 |
irasnyd |
106 |
} //end class Driver
|
|
|
107 |
|
|
|
108 |
/*
|
|
|
109 |
BufferedReader kb = new BufferedReader(
|
|
|
110 |
new InputStreamReader(System.in));
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
BufferedReader br = new BufferedReader(
|
|
|
114 |
new InputStreamReader(
|
|
|
115 |
new FileInputStream(filename)));
|
|
|
116 |
|
|
|
117 |
PrintStream ps = new PrintStream(
|
|
|
118 |
new FileOutputStream(
|
|
|
119 |
new File(filename)));
|
|
|
120 |
|
|
|
121 |
*/
|
|
|
122 |
|