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; }
|
34 |
irasnyd |
186 |
if( left.isFull() && right.isFull() ) { return true; }
|
|
|
187 |
return false;
|
33 |
irasnyd |
188 |
}
|
|
|
189 |
|
|
|
190 |
// method to check if the tree is balanced
|
|
|
191 |
// Precondition: none
|
|
|
192 |
// Postcondition: returns true if the tree is balanced, false otherwise
|
|
|
193 |
public boolean isBalanced() {
|
34 |
irasnyd |
194 |
if( this.isLeaf() ) { return true; }
|
|
|
195 |
if( Math.abs(left.height() - right.height()) < 2 ) { return true; }
|
|
|
196 |
if( left.isBalanced() && right.isBalanced() ) { return true; }
|
|
|
197 |
return false;
|
|
|
198 |
}
|
35 |
irasnyd |
199 |
|
|
|
200 |
public int pathLength() {
|
|
|
201 |
int answer=0;
|
|
|
202 |
Queue queue = new Queue();
|
|
|
203 |
queue.enqueue(this);
|
|
|
204 |
|
|
|
205 |
while( !queue.isEmpty() ) {
|
|
|
206 |
BinaryTree temp = (BinaryTree)queue.dequeue();
|
|
|
207 |
answer += level(temp.root);
|
|
|
208 |
if( temp.left != null ) { queue.enqueue(temp.left); }
|
|
|
209 |
if( temp.right != null ) { queue.enqueue(temp.right); }
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
return answer;
|
34 |
irasnyd |
213 |
}
|
33 |
irasnyd |
214 |
|
35 |
irasnyd |
215 |
public BinaryTree reverse() {
|
|
|
216 |
//both left and right are null
|
|
|
217 |
if( this.isLeaf() ) { return new BinaryTree(root); }
|
|
|
218 |
|
|
|
219 |
//left must be null, right must not be null
|
|
|
220 |
if( this.left == null )
|
|
|
221 |
return new BinaryTree(root,right.reverse(),null);
|
|
|
222 |
|
|
|
223 |
//right must be null, left must not be null
|
|
|
224 |
if( this.right == null )
|
|
|
225 |
return new BinaryTree(root,null,left.reverse());
|
|
|
226 |
|
|
|
227 |
//both sides are not null
|
|
|
228 |
return new BinaryTree(root,right.reverse(),left.reverse());
|
|
|
229 |
}
|
|
|
230 |
|
34 |
irasnyd |
231 |
public int level( Object x ) {
|
|
|
232 |
if( !this.contains(x) ) { return -1; } //not found
|
|
|
233 |
if( this.root.equals(x) ) { return 0; } //found here
|
|
|
234 |
int ansLeft = left.level(x);
|
|
|
235 |
int ansRight = right.level(x);
|
|
|
236 |
|
|
|
237 |
int answer = Math.max(ansLeft,ansRight);
|
|
|
238 |
if( answer >= 0 ) { return answer + 1; }
|
|
|
239 |
return answer;
|
33 |
irasnyd |
240 |
}
|
35 |
irasnyd |
241 |
|
|
|
242 |
public boolean isDisjointFrom( BinaryTree that ) {
|
|
|
243 |
if( that == null ) { return true; }
|
|
|
244 |
if( this.contains(that.root) ) { return false; }
|
|
|
245 |
|
|
|
246 |
return isDisjointFrom(that.left) && isDisjointFrom(that.right);
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
public boolean isValid() {
|
|
|
250 |
if( this.isLeaf() ) { return true; }
|
|
|
251 |
boolean answer;
|
|
|
252 |
|
|
|
253 |
if( left != null ) { answer = left.isDisjointFrom(right); }
|
|
|
254 |
else { answer = right.isDisjointFrom(left); }
|
|
|
255 |
|
|
|
256 |
return answer;
|
|
|
257 |
}
|
|
|
258 |
|
34 |
irasnyd |
259 |
public boolean equals( Object object ) {
|
|
|
260 |
//if we are not a BinaryTree, return false
|
|
|
261 |
if( !(object instanceof BinaryTree) ) { return false; }
|
|
|
262 |
BinaryTree x = (BinaryTree)object; //typed instance of Object
|
|
|
263 |
|
|
|
264 |
//temporary answer holding variables
|
|
|
265 |
boolean ansLeft=false, ansRight=false;
|
|
|
266 |
|
|
|
267 |
//check for the root data equality
|
|
|
268 |
if( !this.root.equals(x.root) ) { return false; }
|
|
|
269 |
|
|
|
270 |
//need this check to make sure that the recursive operations are ok
|
|
|
271 |
if( left != null && right != null ) {
|
|
|
272 |
//check the left
|
|
|
273 |
if( left.equals(x.left) ) { ansLeft = true; }
|
|
|
274 |
//check the right
|
|
|
275 |
if( right.equals(x.right) ) { ansRight = true; }
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
//if both are null, we are still okay
|
|
|
279 |
if( left == null && x.left == null ) { ansLeft = true; }
|
|
|
280 |
if( right == null && x.right == null ) { ansRight = true; }
|
|
|
281 |
|
|
|
282 |
//check that both left and right are okay
|
|
|
283 |
if( ansLeft == true && ansRight == true ) { return true; }
|
|
|
284 |
return false; //return false if any condition was not met
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
//printing methods ------------------------------------------------------
|
|
|
289 |
public static void preOrderPrint( BinaryTree tree ) {
|
|
|
290 |
System.out.print( tree.root + " " );
|
|
|
291 |
if( tree.left != null ) { preOrderPrint( tree.left ); }
|
|
|
292 |
if( tree.right != null ) { preOrderPrint( tree.right ); }
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
public static void postOrderPrint( BinaryTree tree ) {
|
|
|
296 |
if( tree.left != null ) { postOrderPrint( tree.left ); }
|
|
|
297 |
if( tree.right != null ) { postOrderPrint( tree.right ); }
|
|
|
298 |
System.out.print( tree.root + " " );
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
public static void levelOrderPrint( BinaryTree tree ) {
|
|
|
302 |
Queue queue = new Queue();
|
|
|
303 |
queue.enqueue(tree);
|
28 |
irasnyd |
304 |
|
34 |
irasnyd |
305 |
while( !queue.isEmpty() ) {
|
|
|
306 |
BinaryTree temp = (BinaryTree)queue.dequeue();
|
|
|
307 |
System.out.print( temp.root + " " );
|
|
|
308 |
if( temp.left != null ) { queue.enqueue(temp.left); }
|
|
|
309 |
if( temp.right != null ) { queue.enqueue(temp.right); }
|
|
|
310 |
}
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
public static void inOrderPrint( BinaryTree tree ) {
|
|
|
314 |
if( tree.left != null ) { inOrderPrint(tree.left); }
|
|
|
315 |
System.out.print( tree.root + " " );
|
|
|
316 |
if( tree.right != null ) { inOrderPrint(tree.right); }
|
|
|
317 |
}
|
28 |
irasnyd |
318 |
}
|
|
|
319 |
|
|
|
320 |
/*
|
|
|
321 |
BufferedReader kb = new BufferedReader(
|
|
|
322 |
new InputStreamReader(System.in));
|
|
|
323 |
|
|
|
324 |
|
|
|
325 |
BufferedReader br = new BufferedReader(
|
|
|
326 |
new InputStreamReader(
|
|
|
327 |
new FileInputStream(filename)));
|
|
|
328 |
|
|
|
329 |
PrintStream ps = new PrintStream(
|
|
|
330 |
new FileOutputStream(
|
|
|
331 |
new File(filename)));
|
|
|
332 |
|
|
|
333 |
*/
|
|
|
334 |
|