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 {
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() );
33 irasnyd 16
        System.out.println( test_12_9() );
34 irasnyd 17
        System.out.println( test_12_10());
18
        //System.out.println( test_12_11());
19
 
20
        BinaryTree treeA = createTestTree();
21
        //System.out.println("level E: " + treeA.level("E"));
22
        //System.out.println("treeA == treeB: " + 
23
        //    treeA.equals( new BinaryTree("Q",createTestTree(),createTestTree())));
24
 
25
        System.out.print("PreOrder: "); BinaryTree.preOrderPrint(treeA); 
26
        System.out.println();
27
 
28
        System.out.print("PostOrder: "); BinaryTree.postOrderPrint(treeA);
29
        System.out.println();
30
 
31
        System.out.print("LevelOrder: "); BinaryTree.levelOrderPrint(treeA);
32
        System.out.println();
33
 
34
        System.out.print("InOrder: "); BinaryTree.inOrderPrint(treeA);
35
        System.out.println();
36
 
37
 
29 irasnyd 38
    } //end main
28 irasnyd 39
 
30 irasnyd 40
    public static BinaryTree createTestTree() {
29 irasnyd 41
        BinaryTree treeB = new BinaryTree("B");
42
        BinaryTree treeD = new BinaryTree("D");
43
        BinaryTree treeE = new BinaryTree("E");
44
        BinaryTree treeC = new BinaryTree("C",treeD,treeE);
45
        BinaryTree treeA = new BinaryTree("A",treeB,treeC);
46
 
30 irasnyd 47
        return treeA;
48
    }
49
 
50
    public static String test_12_2() {
51
        BinaryTree treeA = createTestTree();
52
 
29 irasnyd 53
        String experimentalResult = treeA.toString();
54
        String correctResult = "((B),A,((D),C,(E)))";
55
 
30 irasnyd 56
        if( correctResult.equals(experimentalResult) )
57
            return "test_12_2: PASSED";
58
 
59
        return "test_12_2: *** FAILED ***";
29 irasnyd 60
    }
31 irasnyd 61
 
62
    public static String test_12_3() {
63
        BinaryTree treeA = createTestTree();
64
        BinaryTree treeAleft = treeA.getLeft();
29 irasnyd 65
 
31 irasnyd 66
        if( treeA.isLeaf() == false && treeAleft.isLeaf() == true )
67
            return "test_12_3: PASSED";
29 irasnyd 68
 
31 irasnyd 69
        return "test_12_3: *** FAILED ***";
70
    }
71
 
72
    public static String test_12_4() {
73
        BinaryTree treeA = createTestTree();
74
        int correctResult = 5;
75
        int experimentalResult = treeA.size();
76
 
77
        if( correctResult == experimentalResult )
78
            return "test_12_4: PASSED";
79
 
80
        return "test_12_4: *** FAILED ***";
81
    }
82
 
83
    public static String test_12_5() {
84
        BinaryTree treeA = createTestTree();
85
        int correctResult = 2;
86
        int experimentalResult = treeA.height();
87
 
88
        if( correctResult == experimentalResult )
89
            return "test_12_5: PASSED";
90
 
91
        return "test_12_5: *** FAILED ***";
92
    }
93
 
32 irasnyd 94
    public static String test_12_6() {
95
        BinaryTree treeA = createTestTree();
96
 
97
        if( treeA.contains("B") &&
98
            treeA.contains("A") &&
99
            treeA.contains("D") &&
100
           !treeA.contains("Z") ) return "test_12_6: PASSED";
101
 
102
        return "test_12_6: *** FAILED ***";
103
    }
104
 
105
    public static String test_12_7() {
106
        BinaryTree treeA = createTestTree();
107
        int correctResult = 3;
108
 
109
        if( correctResult == treeA.numLeaves() )
110
            return "test_12_7: PASSED";
111
 
112
        return "test_12_7: *** FAILED ***";
113
    }
114
 
115
    public static String test_12_8() {
116
        BinaryTree treeA = createTestTree();
117
        BinaryTree tree = new BinaryTree("E",treeA,new BinaryTree("E"));
118
 
119
        int correctResult = 3;
120
 
121
        if( correctResult == tree.count("E") )
122
            return "test_12_8: PASSED";
123
 
124
        return "test_12_8: *** FAILED ***";
125
    }
126
 
33 irasnyd 127
    public static String test_12_9() {
128
        BinaryTree treeA = createTestTree();
129
 
130
        if( treeA.isFull() == false ) { return "test_12_9: PASSED"; }
131
        return "test_12_9: *** FAILED ***";
132
    }
133
 
34 irasnyd 134
    public static String test_12_10() {
135
        BinaryTree treeA = createTestTree();
136
 
137
        if( treeA.isBalanced() == true ) { return "test_12_10: PASSED"; }
138
        return "test_12_10: *** FAILED ***";
139
    }
140
 
141
    //public static String test_12_11() {
142
    //    BinaryTree treeA = createTestTree();
143
    //
144
    //    if( treeA.pathLength() == 6 ) { return "test_12_11: PASSED"; }
145
    //    return "test_12_11: *** FAILED *** val:" + treeA.pathLength();
146
    //}
147
 
28 irasnyd 148
} //end class Driver
149
 
150
/*
151
      BufferedReader kb = new BufferedReader(
152
                              new InputStreamReader(System.in));
153
 
154
 
155
      BufferedReader br = new BufferedReader(
156
                              new InputStreamReader(
157
                                  new FileInputStream(filename)));
158
 
159
      PrintStream ps = new PrintStream(
160
                           new FileOutputStream(
161
                               new File(filename)));
162
 
163
*/
164