Subversion Repositories programming

Rev

Rev 33 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 33 Rev 34
Line 181... Line 181...
181
    // Postcondition: returns true if the tree is a full tree, 
181
    // Postcondition: returns true if the tree is a full tree, 
182
    //                and false if the tree is not a full tree
182
    //                and false if the tree is not a full tree
183
    public boolean isFull() { 
183
    public boolean isFull() { 
184
        if( this.isLeaf() ) { return true; }
184
        if( this.isLeaf() ) { return true; }
185
        if( !(left.height() == right.height()) ) { return false; }
185
        if( !(left.height() == right.height()) ) { return false; }
186
        if( left.isFull() == false || right.isFull() == false )
186
        if( left.isFull() && right.isFull() ) { return true; }
187
            return false;
187
        return false;
188
        return true;
-
 
189
    }
188
    }
190
    
189
    
191
    // method to check if the tree is balanced
190
    // method to check if the tree is balanced
192
    // Precondition:  none
191
    // Precondition:  none
193
    // Postcondition: returns true if the tree is balanced, false otherwise
192
    // Postcondition: returns true if the tree is balanced, false otherwise
194
    public boolean isBalanced() { 
193
    public boolean isBalanced() { 
-
 
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; }
195
        
197
        return false;
196
    }
198
    }
197
    
199
    /*
198
    public int pathLength() { }
200
    public int pathLength() { 
-
 
201
    }
-
 
202
        
199
    public BinaryTree reverse() { }
203
    public BinaryTree reverse() { }
-
 
204
    */
-
 
205
    
200
    public int level( Object x ) { }
206
    public int level( Object x ) { 
-
 
207
        if( !this.contains(x) ) { return -1; }  //not found
-
 
208
        if( this.root.equals(x) ) { return 0; } //found here
-
 
209
        int ansLeft = left.level(x);
-
 
210
        int ansRight = right.level(x);
-
 
211
        
-
 
212
        int answer = Math.max(ansLeft,ansRight);
-
 
213
        if( answer >= 0 ) { return answer + 1; }
-
 
214
        return answer;
-
 
215
    }
-
 
216
    /*
201
    public boolean isDisjointFrom( BinaryTree that ) { }
217
    public boolean isDisjointFrom( BinaryTree that ) { }
202
    public boolean isValid() { }
218
    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 ) { }
-
 
210
    */
219
    */
-
 
220
    public boolean equals( Object object ) {
-
 
221
        //if we are not a BinaryTree, return false
-
 
222
        if( !(object instanceof BinaryTree) ) { return false; } 
-
 
223
        BinaryTree x = (BinaryTree)object; //typed instance of Object
-
 
224
        
-
 
225
        //temporary answer holding variables
-
 
226
        boolean ansLeft=false, ansRight=false;
-
 
227
        
-
 
228
        //check for the root data equality
-
 
229
        if( !this.root.equals(x.root) ) { return false; }
-
 
230
        
-
 
231
        //need this check to make sure that the recursive operations are ok
-
 
232
        if( left != null && right != null ) {
-
 
233
            //check the left
-
 
234
            if( left.equals(x.left) ) { ansLeft = true; }
-
 
235
            //check the right
-
 
236
            if( right.equals(x.right) ) { ansRight = true; }
-
 
237
        }
-
 
238
        
-
 
239
        //if both are null, we are still okay
-
 
240
        if( left == null && x.left == null ) { ansLeft = true; }
-
 
241
        if( right == null && x.right == null ) { ansRight = true; }
-
 
242
        
-
 
243
        //check that both left and right are okay
-
 
244
        if( ansLeft == true && ansRight == true ) { return true; }
-
 
245
        return false; //return false if any condition was not met
-
 
246
    }
-
 
247
    
-
 
248
    
-
 
249
    //printing methods ------------------------------------------------------
-
 
250
    public static void preOrderPrint( BinaryTree tree ) { 
-
 
251
        System.out.print( tree.root + " " );
-
 
252
        if( tree.left != null ) { preOrderPrint( tree.left ); }
-
 
253
        if( tree.right != null ) { preOrderPrint( tree.right ); }
-
 
254
    }
-
 
255
    
-
 
256
    public static void postOrderPrint( BinaryTree tree ) { 
-
 
257
        if( tree.left != null ) { postOrderPrint( tree.left ); }
-
 
258
        if( tree.right != null ) { postOrderPrint( tree.right ); }
-
 
259
        System.out.print( tree.root + " " );
-
 
260
    }
-
 
261
    
-
 
262
    public static void levelOrderPrint( BinaryTree tree ) { 
-
 
263
        Queue queue = new Queue();
-
 
264
        queue.enqueue(tree);
-
 
265
 
-
 
266
        while( !queue.isEmpty() ) {
-
 
267
            BinaryTree temp = (BinaryTree)queue.dequeue();
-
 
268
            System.out.print( temp.root + " " );
-
 
269
            if( temp.left != null ) { queue.enqueue(temp.left); }
-
 
270
            if( temp.right != null ) { queue.enqueue(temp.right); }
-
 
271
        }   
-
 
272
    }
-
 
273
    
-
 
274
    public static void inOrderPrint( BinaryTree tree ) { 
-
 
275
        if( tree.left != null ) { inOrderPrint(tree.left); }
-
 
276
        System.out.print( tree.root + " " );
-
 
277
        if( tree.right != null ) { inOrderPrint(tree.right); }
-
 
278
    }
211
}
279
}
212
 
280
 
213
/*
281
/*
214
      BufferedReader kb = new BufferedReader(
282
      BufferedReader kb = new BufferedReader(
215
                              new InputStreamReader(System.in));
283
                              new InputStreamReader(System.in));