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
 
5
import java.io.*;
6
import java.util.*;
7
class BinaryTree {
8
    private Object root;
9
    private BinaryTree left, right;
10
 
11
    //constructors ----------------------------------------------------------
29 irasnyd 12
 
13
    // constructor to create a singleton tree
14
    // Precondition:  root is a non-null Object
15
    // Postcondition: returns a BinaryTree with the Object given as the root
16
    //                data, and null left and right subtrees
17
    public BinaryTree( Object root ) { 
18
        this.root = root;
19
        this.left = null;
20
        this.right = null;
21
    }
22
 
23
    // constructor to create a BinaryTree with the given Object as the root
24
    // data, and the given BinaryTrees as the left and right subtrees
25
    // Precondition:  root is a non-null Object (right and left CAN be null
26
    // Postcondition: returns a BinaryTree with the given root data and
27
    //                the given left and right subtrees
28
    public BinaryTree( Object root, BinaryTree left, BinaryTree right ) {
29
        this.root = root;
30
        this.left = left;
31
        this.right = right;
32
    }
28 irasnyd 33
 
29 irasnyd 34
    // copy constructor, creates a tree which has the same structure and
35
    // whose nodes reference the same objects as the _that_ tree
36
    public BinaryTree( BinaryTree that ) { 
37
        System.out.println("Don't use me yet");
38
    }
39
 
28 irasnyd 40
    //getter methods --------------------------------------------------------
41
 
29 irasnyd 42
    // method which returns the root data
43
    // Precondition:  none
44
    // Postcondition: return the root data
45
    public Object getRoot() { return root; }
46
 
47
    // method which will return a reference to the left subtree
48
    // Precondition:  none
49
    // Postcondition: returns a reference to the left subtree
50
    public BinaryTree getLeft() { return left; }
51
 
52
    // method which will return a reference to the right subtree
53
    // Precondition:  none
54
    // Postcondition: returns a reference to the right subtree
55
    public BinaryTree getRight() { return right; }
56
 
28 irasnyd 57
    //setter methods --------------------------------------------------------
58
 
29 irasnyd 59
    // method which updates the root data
60
    // Precondition:  root is non-null
61
    // Postcondition: sets this.root to the new data, returns the old data
62
    public Object setRoot( Object root ) { 
63
        Object temp = this.root;
64
        this.root = root;
65
        return temp;
66
    }
67
 
68
    // method which updates the left subtree
69
    // Precondition:  none ( left CAN be null )
70
    // Postcondition: sets this.left to the new subtree,
71
    //                returns a reference to the old left subtree
72
    public BinaryTree setLeft( BinaryTree left ) { 
73
        BinaryTree temp = this.left;
74
        this.left = left;
75
        return temp;
76
    }
77
 
78
    // method which update the right subtree
79
    // Precondition:  none ( right CAN be null )
80
    // Postcondition: sets this.right to the new subtree,
81
    //                returns a reference to the old right subtree
82
    public BinaryTree setRight( BinaryTree right ) {
83
        BinaryTree temp = this.right;
84
        this.right = right;
85
        return temp;
86
    }
87
 
28 irasnyd 88
    //toString method -------------------------------------------------------
89
 
29 irasnyd 90
    // returns a String representation of the BinaryTree
30 irasnyd 91
    // Precondition:  none
92
    // Postcondition: returns a string representation of the BinaryTree
29 irasnyd 93
    public String toString() { 
94
        String sLeft = "";
95
        String sRight = "";
96
        String answer = new String();
97
 
30 irasnyd 98
        //get the left tree's string representation (if it exists)
29 irasnyd 99
        if( !(left == null) ) { sLeft = left.toString(); }
100
 
30 irasnyd 101
        //get the right tree's string representation (if it exists)
29 irasnyd 102
        if( !(right == null) ) { sRight = right.toString(); }
103
 
30 irasnyd 104
        //assemble the string to return
29 irasnyd 105
        answer = "(";
106
        if( !sLeft.equals("") ) { answer += sLeft + ","; }
107
        answer += root.toString();
108
        if( !sRight.equals("") ) { answer += "," + sRight; }
109
        answer += ")";
110
 
30 irasnyd 111
        //return the assembled string
29 irasnyd 112
        return answer;
113
    }
30 irasnyd 114
 
28 irasnyd 115
    //misc methods ----------------------------------------------------------
33 irasnyd 116
 
117
    // method to check if the current node is a leaf
118
    // Precondition:  none
119
    // Postcondition: returns true if the current node is a leaf, and false
120
    //                in any other case
30 irasnyd 121
    public boolean isLeaf() { 
31 irasnyd 122
        if( (left == null) && (right == null) ) { return true; }
30 irasnyd 123
        return false;
124
    }
31 irasnyd 125
 
33 irasnyd 126
    // method to find the size of the tree
127
    // Precondition:  none
128
    // Postcondition: returns the size of the tree
31 irasnyd 129
    public int size() { 
130
        int answer=1; // 1 for the node we are at
131
        if( !(left == null) ) { answer += left.size(); }
132
        if( !(right == null) ) { answer += right.size(); }
133
 
134
        return answer;
135
    }
136
 
33 irasnyd 137
    // method to calculate the height of the tree
138
    // Precondition:  none
139
    // Postcondition: returns the height of the tree
31 irasnyd 140
    public int height() {
141
        if( this.isLeaf() ) { return 0; }
142
        int l=1,r=1;
143
        l += left.height();
144
        r += right.height();
145
 
146
        return Math.max(l,r);
147
    }
32 irasnyd 148
 
33 irasnyd 149
    // method to search the tree for an object
150
    // Precondition:  object is non-null
151
    // Postcondition: returns true if the tree contains the object,
152
    //                and false if the tree doesn't contain the object
32 irasnyd 153
    public boolean contains( Object object ) { 
154
        if( root.equals(object) ) { return true; }
155
        if( this.isLeaf() ) { return false; }
156
        return left.contains(object) || right.contains(object);
157
    }
158
 
33 irasnyd 159
    // method to find the number of leaves in the tree
160
    // Precondition:  none
161
    // Postcondition: returns the number of leaves in the tree
32 irasnyd 162
    public int numLeaves() { 
163
        if( this.isLeaf() ) { return 1; }
164
        return left.numLeaves() + right.numLeaves();
165
    }
166
 
33 irasnyd 167
    // method to find the number of a certain object in the tree
168
    // Precondition:  the object in non-null
169
    // Postcondition: returns the number of the object that
170
    //                are in the tree
32 irasnyd 171
    public int count( Object x ) { 
172
        int answer=0;
173
        if( root.equals(x) ) { answer=1; }
174
        if( !(left == null) ) { answer += left.count(x); }
175
        if( !(right == null) ) { answer += right.count(x); }
176
        return answer;
177
    }
33 irasnyd 178
 
179
    // method to check if the tree is full
180
    // Precondition:  none
181
    // Postcondition: returns true if the tree is a full tree, 
182
    //                and false if the tree is not a full tree
183
    public boolean isFull() { 
184
        if( this.isLeaf() ) { return true; }
185
        if( !(left.height() == right.height()) ) { return false; }
186
        if( left.isFull() == false || right.isFull() == false )
187
            return false;
188
        return true;
189
    }
190
 
191
    // method to check if the tree is balanced
192
    // Precondition:  none
193
    // Postcondition: returns true if the tree is balanced, false otherwise
194
    public boolean isBalanced() { 
195
 
196
    }
197
 
28 irasnyd 198
    public int pathLength() { }
199
    public BinaryTree reverse() { }
200
    public int level( Object x ) { }
201
    public boolean isDisjointFrom( BinaryTree that ) { }
202
    public boolean isValid() { }
203
    public boolean equals( Object object ) { }
204
 
205
    //printing methods ------------------------------------------------------
206
    public static void preOrderPrint( BinaryTree tree ) { }
207
    public static void postOrderPrint( BinaryTree tree ) { }
208
    public static void levelOrderPrint( BinaryTree tree ) { }
209
    public static void inOrderPrint( BinaryTree tree ) { }
29 irasnyd 210
    */
28 irasnyd 211
}
212
 
213
/*
214
      BufferedReader kb = new BufferedReader(
215
                              new InputStreamReader(System.in));
216
 
217
 
218
      BufferedReader br = new BufferedReader(
219
                              new InputStreamReader(
220
                                  new FileInputStream(filename)));
221
 
222
      PrintStream ps = new PrintStream(
223
                           new FileOutputStream(
224
                               new File(filename)));
225
 
226
*/
227