Subversion Repositories programming

Rev

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