Subversion Repositories programming

Rev

Rev 37 | Go to most recent revision | Details | Compare with Previous | 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 {
36 irasnyd 8
        BinaryTree tree1 = createTreeOne();
9
        BinaryTree tree2 = createTreeTwo();
10
        BinaryTree tree3 = createTreeThree();
37 irasnyd 11
        BinaryTree tree4 = createTreeOne();
12
        BinaryTree tree5 = createTreeThree(); //make a copy for later
36 irasnyd 13
 
37 irasnyd 14
        // ---- check getter methods ----
36 irasnyd 15
        System.out.println("Testing getter methods...");
16
        System.out.println("root: " + tree3.getRoot());
17
        System.out.println("left: " + tree3.getLeft());
18
        System.out.println("right: " + tree3.getRight());
37 irasnyd 19
        System.out.println();
36 irasnyd 20
 
37 irasnyd 21
        // ---- check setter and toString methods ----
36 irasnyd 22
        System.out.println("Testing setter and toString methods...");
37 irasnyd 23
        tree4.setRoot("A");
24
        tree4.setLeft( new BinaryTree("B"));
25
        tree4.setRight( new BinaryTree("C"));
26
        System.out.println("tree4: " + tree4 ); //should print ((A),B,(C))
27
        System.out.println();
28
 
29
        // ---- check misc methods ----
36 irasnyd 30
        System.out.println("Testing misc methods...");
37 irasnyd 31
        System.out.println("tree4.isLeaf(): " + tree4.isLeaf());
32
        System.out.println("tree4.left.isLeaf(): " + tree4.getLeft().isLeaf());
33
        System.out.println();
34
 
36 irasnyd 35
        System.out.println("tree3.size(): " + tree3.size());
37 irasnyd 36
        System.out.println();
37
 
36 irasnyd 38
        System.out.println("tree3.height(): " + tree3.height());
37 irasnyd 39
        System.out.println();
40
 
36 irasnyd 41
        System.out.println("tree3.contains(\"2\"): " + tree3.contains("2"));
42
        System.out.println("tree3.contains(\"zZ\"): " + tree3.contains("zZ"));
37 irasnyd 43
        System.out.println();
36 irasnyd 44
 
37 irasnyd 45
        System.out.println("tree3.numLeaves(): " + tree3.numLeaves());
46
        System.out.println("tree1.numLeaves(): " + tree1.numLeaves());
47
        System.out.println();
36 irasnyd 48
 
37 irasnyd 49
        System.out.println("tree3.count(\"s\"): " + tree3.count("s"));
50
        System.out.println("tree3.count(\"zZ\"): " + tree3.count("zZ"));
51
        System.out.println();
52
 
53
        System.out.println("tree3.isFull(): " + tree3.isFull());
54
        System.out.println("tree4.isFull(): " + tree4.isFull());
55
        System.out.println();
56
 
57
        System.out.println("tree3.isBalanced(): " + tree3.isBalanced());
58
        System.out.println("tree4.isBalanced(): " + tree4.isBalanced());
59
        System.out.println();
60
 
61
        System.out.println("tree3.pathLength(): " + tree3.pathLength());
62
        System.out.println("tree4.pathLength(): " + tree4.pathLength());
63
        System.out.println();
64
 
65
        System.out.println("tree4 = " + tree4);
66
        System.out.println("tree4.reverse() = " + tree4.reverse());
67
        System.out.println("tree2 = " + tree2);
68
        System.out.println("tree2.reverse() = " + tree2.reverse());
69
        System.out.println();
70
 
71
        System.out.println("tree1.level(\"H\"): " + tree1.level("H"));
72
        System.out.println("tree2.level(\"O\"): " + tree2.level("O"));
73
        System.out.println();
74
 
75
        System.out.println("tree4.isDisjointFrom(tree1): " 
76
            + tree4.isDisjointFrom(tree1));
77
        System.out.println("tree3.isDisjointFrom(new BinaryTree(\"s\"): "
78
            + tree3.isDisjointFrom(new BinaryTree("s")));
79
        System.out.println();
80
 
81
        System.out.println("tree1.isValid(): " + tree1.isValid());
82
        System.out.println("tree2.isValid(): " + tree2.isValid());
83
        System.out.println("tree3.isValid(): " + tree3.isValid());
84
        System.out.println();
85
 
86
        System.out.println("tree1.equals(tree3): " + tree1.equals(tree3));
87
        System.out.println("tree3.equals(tree5): " + tree3.equals(tree5));
88
        System.out.println();
89
 
90
        // ---- check print methods ----
91
        System.out.println("Testing Printing Methods...");
92
 
93
        // for tree1
94
        System.out.print("BinaryTree.preOrderPrint(tree1): ");
95
            BinaryTree.preOrderPrint(tree1); System.out.println();
96
        System.out.print("BinaryTree.inOrderPrint(tree1): ");
97
            BinaryTree.inOrderPrint(tree1); System.out.println();
98
        System.out.print("BinaryTree.postOrderPrint(tree1): ");
99
            BinaryTree.postOrderPrint(tree1); System.out.println();
100
        System.out.print("BinaryTree.levelOrderPrint(tree1): ");
101
            BinaryTree.levelOrderPrint(tree1); System.out.println();
102
        System.out.println();
103
 
104
        //for tree2
105
        System.out.print("BinaryTree.preOrderPrint(tree2): ");
106
            BinaryTree.preOrderPrint(tree2); System.out.println();
107
        System.out.print("BinaryTree.inOrderPrint(tree2): ");
108
            BinaryTree.inOrderPrint(tree2); System.out.println();
109
        System.out.print("BinaryTree.postOrderPrint(tree2): ");
110
            BinaryTree.postOrderPrint(tree2); System.out.println();
111
        System.out.print("BinaryTree.levelOrderPrint(tree2): ");
112
            BinaryTree.levelOrderPrint(tree2); System.out.println();
113
        System.out.println();
114
 
115
        //for tree3
116
        System.out.print("BinaryTree.preOrderPrint(tree3): ");
117
            BinaryTree.preOrderPrint(tree3); System.out.println();
118
        System.out.print("BinaryTree.inOrderPrint(tree3): ");
119
            BinaryTree.inOrderPrint(tree3); System.out.println();
120
        System.out.print("BinaryTree.postOrderPrint(tree3): ");
121
            BinaryTree.postOrderPrint(tree3); System.out.println();
122
        System.out.print("BinaryTree.levelOrderPrint(tree3): ");
123
            BinaryTree.levelOrderPrint(tree3); System.out.println();
124
        System.out.println();
36 irasnyd 125
    }
29 irasnyd 126
 
36 irasnyd 127
    public static BinaryTree createTreeOne() {
128
        return new BinaryTree("H");
35 irasnyd 129
    }
130
 
36 irasnyd 131
    public static BinaryTree createTreeTwo() {
132
        BinaryTree treeA = new BinaryTree("A");
133
        BinaryTree treeC = new BinaryTree("C");
134
        BinaryTree treeE = new BinaryTree("E");
135
        BinaryTree treeG = new BinaryTree("G");
136
        BinaryTree treeI = new BinaryTree("I");
137
        BinaryTree treeK = new BinaryTree("K");
138
        BinaryTree treeM = new BinaryTree("M");
139
        BinaryTree treeO = new BinaryTree("O");
140
 
141
        BinaryTree treeB = new BinaryTree("B",treeA,treeC);
142
        BinaryTree treeF = new BinaryTree("F",treeE,treeG);
143
        BinaryTree treeJ = new BinaryTree("J",treeI,treeK);
144
        BinaryTree treeN = new BinaryTree("N",treeM,treeO);
145
 
146
        BinaryTree treeD = new BinaryTree("D",treeB,treeF);
147
        BinaryTree treeL = new BinaryTree("L",treeJ,treeN);
148
 
149
        BinaryTree treeH = new BinaryTree("H",treeD,treeL);
150
 
151
        return treeH;
152
    }
153
 
154
    public static BinaryTree createTreeThree() {
155
        BinaryTree tree_2 = new BinaryTree("2");
156
        BinaryTree tree_a1 = new BinaryTree("a");
157
        BinaryTree tree_t1 = new BinaryTree("t");
158
        BinaryTree tree_c1 = new BinaryTree("c");
159
        BinaryTree tree_a2 = new BinaryTree("a");
160
 
161
        BinaryTree tree_s1 = new BinaryTree("s",null,tree_2);
162
        BinaryTree tree_n = new BinaryTree("n");
163
        BinaryTree tree_s2 = new BinaryTree("s",tree_a1,tree_t1);
164
        BinaryTree tree_a3 = new BinaryTree("a");
165
        BinaryTree tree_l1 = new BinaryTree("l",tree_c1,tree_a2);
166
 
167
        BinaryTree tree_c2 = new BinaryTree("c",null,tree_s1);
168
        BinaryTree tree_i1 = new BinaryTree("i");
169
        BinaryTree tree_t2 = new BinaryTree("t",tree_n,tree_s2);
170
        BinaryTree tree_l2 = new BinaryTree("l");
171
        BinaryTree tree_t3 = new BinaryTree("t",tree_a3,tree_l1);
172
        BinaryTree tree_s3 = new BinaryTree("s");
173
 
174
        BinaryTree tree_4 = new BinaryTree("4",tree_c2,null);
175
        BinaryTree tree_s4 = new BinaryTree("s",tree_i1,null);
176
        BinaryTree tree_f = new BinaryTree("f");
177
        BinaryTree tree_i2 = new BinaryTree("i",tree_t2,null);
178
        BinaryTree tree_a4 = new BinaryTree("a");
179
        BinaryTree tree_y = new BinaryTree("y",tree_l2,null);
180
        BinaryTree tree_r = new BinaryTree("r");
181
        BinaryTree tree_s5 = new BinaryTree("s",tree_t3,tree_s3);
182
 
183
        BinaryTree tree_1 = new BinaryTree("1",tree_4,tree_s4);
184
        BinaryTree tree_a5 = new BinaryTree("a",tree_f,tree_i2);
185
        BinaryTree tree_l3 = new BinaryTree("l",tree_a4,tree_y);
186
        BinaryTree tree_e = new BinaryTree("e",tree_r,tree_s5);
187
 
188
        BinaryTree tree_a6 = new BinaryTree("a",tree_1,tree_a5);
189
        BinaryTree tree_g = new BinaryTree("g",tree_l3,tree_e);
190
 
191
        BinaryTree tree_c3 = new BinaryTree("c",tree_a6,tree_g);
192
 
193
        return tree_c3;
194
    }
195
 
35 irasnyd 196
    public static void run_BinaryTree_tests() {
29 irasnyd 197
        System.out.println( test_12_2() );
31 irasnyd 198
        System.out.println( test_12_3() );
199
        System.out.println( test_12_4() );
200
        System.out.println( test_12_5() );
32 irasnyd 201
        System.out.println( test_12_6() );
202
        System.out.println( test_12_7() );
203
        System.out.println( test_12_8() );
33 irasnyd 204
        System.out.println( test_12_9() );
34 irasnyd 205
        System.out.println( test_12_10());
35 irasnyd 206
        System.out.println( test_12_11());
207
        System.out.println( test_12_12());
208
        System.out.println( test_12_13());
209
        System.out.println( test_12_14());
210
        System.out.println( test_12_15());
211
        System.out.println( test_12_16());
28 irasnyd 212
 
35 irasnyd 213
        System.out.println(); //print a blank line
214
        test_traversals();
215
 
216
    } //end main test method
217
 
30 irasnyd 218
    public static BinaryTree createTestTree() {
29 irasnyd 219
        BinaryTree treeB = new BinaryTree("B");
220
        BinaryTree treeD = new BinaryTree("D");
221
        BinaryTree treeE = new BinaryTree("E");
222
        BinaryTree treeC = new BinaryTree("C",treeD,treeE);
223
        BinaryTree treeA = new BinaryTree("A",treeB,treeC);
224
 
30 irasnyd 225
        return treeA;
226
    }
227
 
228
    public static String test_12_2() {
229
        BinaryTree treeA = createTestTree();
230
 
29 irasnyd 231
        String experimentalResult = treeA.toString();
232
        String correctResult = "((B),A,((D),C,(E)))";
233
 
30 irasnyd 234
        if( correctResult.equals(experimentalResult) )
235
            return "test_12_2: PASSED";
236
 
237
        return "test_12_2: *** FAILED ***";
29 irasnyd 238
    }
31 irasnyd 239
 
240
    public static String test_12_3() {
241
        BinaryTree treeA = createTestTree();
242
        BinaryTree treeAleft = treeA.getLeft();
29 irasnyd 243
 
31 irasnyd 244
        if( treeA.isLeaf() == false && treeAleft.isLeaf() == true )
245
            return "test_12_3: PASSED";
29 irasnyd 246
 
31 irasnyd 247
        return "test_12_3: *** FAILED ***";
248
    }
249
 
250
    public static String test_12_4() {
251
        BinaryTree treeA = createTestTree();
252
        int correctResult = 5;
253
        int experimentalResult = treeA.size();
254
 
255
        if( correctResult == experimentalResult )
256
            return "test_12_4: PASSED";
257
 
258
        return "test_12_4: *** FAILED ***";
259
    }
260
 
261
    public static String test_12_5() {
262
        BinaryTree treeA = createTestTree();
263
        int correctResult = 2;
264
        int experimentalResult = treeA.height();
265
 
266
        if( correctResult == experimentalResult )
267
            return "test_12_5: PASSED";
268
 
269
        return "test_12_5: *** FAILED ***";
270
    }
271
 
32 irasnyd 272
    public static String test_12_6() {
273
        BinaryTree treeA = createTestTree();
274
 
275
        if( treeA.contains("B") &&
276
            treeA.contains("A") &&
277
            treeA.contains("D") &&
278
           !treeA.contains("Z") ) return "test_12_6: PASSED";
279
 
280
        return "test_12_6: *** FAILED ***";
281
    }
282
 
283
    public static String test_12_7() {
284
        BinaryTree treeA = createTestTree();
285
        int correctResult = 3;
286
 
287
        if( correctResult == treeA.numLeaves() )
288
            return "test_12_7: PASSED";
289
 
290
        return "test_12_7: *** FAILED ***";
291
    }
292
 
293
    public static String test_12_8() {
294
        BinaryTree treeA = createTestTree();
295
        BinaryTree tree = new BinaryTree("E",treeA,new BinaryTree("E"));
296
 
297
        int correctResult = 3;
298
 
299
        if( correctResult == tree.count("E") )
300
            return "test_12_8: PASSED";
301
 
302
        return "test_12_8: *** FAILED ***";
303
    }
304
 
33 irasnyd 305
    public static String test_12_9() {
306
        BinaryTree treeA = createTestTree();
307
 
308
        if( treeA.isFull() == false ) { return "test_12_9: PASSED"; }
309
        return "test_12_9: *** FAILED ***";
310
    }
311
 
34 irasnyd 312
    public static String test_12_10() {
313
        BinaryTree treeA = createTestTree();
314
 
315
        if( treeA.isBalanced() == true ) { return "test_12_10: PASSED"; }
316
        return "test_12_10: *** FAILED ***";
317
    }
35 irasnyd 318
 
319
    public static String test_12_11() {
320
        BinaryTree treeA = createTestTree();
321
 
322
        if( treeA.pathLength() == 6 ) { return "test_12_11: PASSED"; }
323
        return "test_12_11: *** FAILED *** val:" + treeA.pathLength();
324
    }
325
 
326
    public static String test_12_12() {
327
        BinaryTree treeA = createTestTree();
328
 
329
        String correctAnswer = "(((E),C,(D)),A,(B))";
330
 
331
        if( treeA.reverse().toString().equals(correctAnswer) ) {
332
            return "test_12_12: PASSED";
333
        }
34 irasnyd 334
 
35 irasnyd 335
        return "test_12_12: *** FAILED *** " + treeA.reverse();
336
    }
337
 
338
    public static String test_12_13() {
339
        BinaryTree treeA = createTestTree();
34 irasnyd 340
 
35 irasnyd 341
        if( treeA.level("E") == 2 ) { return "test_12_13: PASSED"; }
342
        return "test_12_13: *** FAILED ***";
343
    }
344
 
345
    public static String test_12_14() {
346
        BinaryTree treeA = createTestTree();
347
        BinaryTree treeB = new BinaryTree("B");
348
 
349
        if( treeA.isDisjointFrom(treeB) ) {
350
            return "test_12_14: *** FAILED ***";
351
        }
28 irasnyd 352
 
35 irasnyd 353
        return "test_12_14: PASSED";
354
    }
28 irasnyd 355
 
35 irasnyd 356
    public static String test_12_15() {
357
        BinaryTree treeA = createTestTree();
28 irasnyd 358
 
35 irasnyd 359
        if( treeA.isValid() ) { return "test_12_15: PASSED"; }
360
        return "test_12_15: *** FAILED ***";
361
    }
362
 
363
    public static String test_12_16() {
364
        BinaryTree tree1 = createTestTree();
365
        BinaryTree tree2 = createTestTree();
28 irasnyd 366
 
35 irasnyd 367
        if( tree1.equals(tree2) ) { return "test_12_16: PASSED"; }
368
        return "test_12_16: *** FAILED ***";
369
    }
370
 
371
    public static void test_traversals() {
372
        BinaryTree treeA = createTestTree();
373
 
374
        //print the correct answers
375
        System.out.println("Should Be:");
376
        System.out.println("PreOrder: A B C D E");
377
        System.out.println("PostOrder: B D E C A");
378
        System.out.println("LevelOrder: A B C D E");
379
        System.out.println("InOrder: B A D C E");
28 irasnyd 380
 
35 irasnyd 381
        //print the actual answers
382
        System.out.println();
383
        System.out.println("Actually is:");
384
        System.out.print("PreOrder: "); BinaryTree.preOrderPrint(treeA); 
385
        System.out.println();
386
        System.out.print("PostOrder: "); BinaryTree.postOrderPrint(treeA);
387
        System.out.println();
388
        System.out.print("LevelOrder: "); BinaryTree.levelOrderPrint(treeA);
389
        System.out.println();
390
        System.out.print("InOrder: "); BinaryTree.inOrderPrint(treeA);
391
        System.out.println();
392
    }
28 irasnyd 393
 
35 irasnyd 394
} //end class Driver
395