Subversion Repositories programming

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
35 irasnyd 9
    }
10
 
11
    public static void run_BinaryTree_tests() {
29 irasnyd 12
        System.out.println( test_12_2() );
31 irasnyd 13
        System.out.println( test_12_3() );
14
        System.out.println( test_12_4() );
15
        System.out.println( test_12_5() );
32 irasnyd 16
        System.out.println( test_12_6() );
17
        System.out.println( test_12_7() );
18
        System.out.println( test_12_8() );
33 irasnyd 19
        System.out.println( test_12_9() );
34 irasnyd 20
        System.out.println( test_12_10());
35 irasnyd 21
        System.out.println( test_12_11());
22
        System.out.println( test_12_12());
23
        System.out.println( test_12_13());
24
        System.out.println( test_12_14());
25
        System.out.println( test_12_15());
26
        System.out.println( test_12_16());
28 irasnyd 27
 
35 irasnyd 28
        System.out.println(); //print a blank line
29
        test_traversals();
30
 
31
    } //end main test method
32
 
30 irasnyd 33
    public static BinaryTree createTestTree() {
29 irasnyd 34
        BinaryTree treeB = new BinaryTree("B");
35
        BinaryTree treeD = new BinaryTree("D");
36
        BinaryTree treeE = new BinaryTree("E");
37
        BinaryTree treeC = new BinaryTree("C",treeD,treeE);
38
        BinaryTree treeA = new BinaryTree("A",treeB,treeC);
39
 
30 irasnyd 40
        return treeA;
41
    }
42
 
43
    public static String test_12_2() {
44
        BinaryTree treeA = createTestTree();
45
 
29 irasnyd 46
        String experimentalResult = treeA.toString();
47
        String correctResult = "((B),A,((D),C,(E)))";
48
 
30 irasnyd 49
        if( correctResult.equals(experimentalResult) )
50
            return "test_12_2: PASSED";
51
 
52
        return "test_12_2: *** FAILED ***";
29 irasnyd 53
    }
31 irasnyd 54
 
55
    public static String test_12_3() {
56
        BinaryTree treeA = createTestTree();
57
        BinaryTree treeAleft = treeA.getLeft();
29 irasnyd 58
 
31 irasnyd 59
        if( treeA.isLeaf() == false && treeAleft.isLeaf() == true )
60
            return "test_12_3: PASSED";
29 irasnyd 61
 
31 irasnyd 62
        return "test_12_3: *** FAILED ***";
63
    }
64
 
65
    public static String test_12_4() {
66
        BinaryTree treeA = createTestTree();
67
        int correctResult = 5;
68
        int experimentalResult = treeA.size();
69
 
70
        if( correctResult == experimentalResult )
71
            return "test_12_4: PASSED";
72
 
73
        return "test_12_4: *** FAILED ***";
74
    }
75
 
76
    public static String test_12_5() {
77
        BinaryTree treeA = createTestTree();
78
        int correctResult = 2;
79
        int experimentalResult = treeA.height();
80
 
81
        if( correctResult == experimentalResult )
82
            return "test_12_5: PASSED";
83
 
84
        return "test_12_5: *** FAILED ***";
85
    }
86
 
32 irasnyd 87
    public static String test_12_6() {
88
        BinaryTree treeA = createTestTree();
89
 
90
        if( treeA.contains("B") &&
91
            treeA.contains("A") &&
92
            treeA.contains("D") &&
93
           !treeA.contains("Z") ) return "test_12_6: PASSED";
94
 
95
        return "test_12_6: *** FAILED ***";
96
    }
97
 
98
    public static String test_12_7() {
99
        BinaryTree treeA = createTestTree();
100
        int correctResult = 3;
101
 
102
        if( correctResult == treeA.numLeaves() )
103
            return "test_12_7: PASSED";
104
 
105
        return "test_12_7: *** FAILED ***";
106
    }
107
 
108
    public static String test_12_8() {
109
        BinaryTree treeA = createTestTree();
110
        BinaryTree tree = new BinaryTree("E",treeA,new BinaryTree("E"));
111
 
112
        int correctResult = 3;
113
 
114
        if( correctResult == tree.count("E") )
115
            return "test_12_8: PASSED";
116
 
117
        return "test_12_8: *** FAILED ***";
118
    }
119
 
33 irasnyd 120
    public static String test_12_9() {
121
        BinaryTree treeA = createTestTree();
122
 
123
        if( treeA.isFull() == false ) { return "test_12_9: PASSED"; }
124
        return "test_12_9: *** FAILED ***";
125
    }
126
 
34 irasnyd 127
    public static String test_12_10() {
128
        BinaryTree treeA = createTestTree();
129
 
130
        if( treeA.isBalanced() == true ) { return "test_12_10: PASSED"; }
131
        return "test_12_10: *** FAILED ***";
132
    }
35 irasnyd 133
 
134
    public static String test_12_11() {
135
        BinaryTree treeA = createTestTree();
136
 
137
        if( treeA.pathLength() == 6 ) { return "test_12_11: PASSED"; }
138
        return "test_12_11: *** FAILED *** val:" + treeA.pathLength();
139
    }
140
 
141
    public static String test_12_12() {
142
        BinaryTree treeA = createTestTree();
143
 
144
        String correctAnswer = "(((E),C,(D)),A,(B))";
145
 
146
        if( treeA.reverse().toString().equals(correctAnswer) ) {
147
            return "test_12_12: PASSED";
148
        }
34 irasnyd 149
 
35 irasnyd 150
        return "test_12_12: *** FAILED *** " + treeA.reverse();
151
    }
152
 
153
    public static String test_12_13() {
154
        BinaryTree treeA = createTestTree();
34 irasnyd 155
 
35 irasnyd 156
        if( treeA.level("E") == 2 ) { return "test_12_13: PASSED"; }
157
        return "test_12_13: *** FAILED ***";
158
    }
159
 
160
    public static String test_12_14() {
161
        BinaryTree treeA = createTestTree();
162
        BinaryTree treeB = new BinaryTree("B");
163
 
164
        if( treeA.isDisjointFrom(treeB) ) {
165
            return "test_12_14: *** FAILED ***";
166
        }
28 irasnyd 167
 
35 irasnyd 168
        return "test_12_14: PASSED";
169
    }
28 irasnyd 170
 
35 irasnyd 171
    public static String test_12_15() {
172
        BinaryTree treeA = createTestTree();
28 irasnyd 173
 
35 irasnyd 174
        if( treeA.isValid() ) { return "test_12_15: PASSED"; }
175
        return "test_12_15: *** FAILED ***";
176
    }
177
 
178
    public static String test_12_16() {
179
        BinaryTree tree1 = createTestTree();
180
        BinaryTree tree2 = createTestTree();
28 irasnyd 181
 
35 irasnyd 182
        if( tree1.equals(tree2) ) { return "test_12_16: PASSED"; }
183
        return "test_12_16: *** FAILED ***";
184
    }
185
 
186
    public static void test_traversals() {
187
        BinaryTree treeA = createTestTree();
188
 
189
        //print the correct answers
190
        System.out.println("Should Be:");
191
        System.out.println("PreOrder: A B C D E");
192
        System.out.println("PostOrder: B D E C A");
193
        System.out.println("LevelOrder: A B C D E");
194
        System.out.println("InOrder: B A D C E");
28 irasnyd 195
 
35 irasnyd 196
        //print the actual answers
197
        System.out.println();
198
        System.out.println("Actually is:");
199
        System.out.print("PreOrder: "); BinaryTree.preOrderPrint(treeA); 
200
        System.out.println();
201
        System.out.print("PostOrder: "); BinaryTree.postOrderPrint(treeA);
202
        System.out.println();
203
        System.out.print("LevelOrder: "); BinaryTree.levelOrderPrint(treeA);
204
        System.out.println();
205
        System.out.print("InOrder: "); BinaryTree.inOrderPrint(treeA);
206
        System.out.println();
207
    }
28 irasnyd 208
 
35 irasnyd 209
} //end class Driver
210