Subversion Repositories programming

Rev

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