Subversion Repositories programming

Rev

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

Rev 37 Rev 38
Line 1... Line 1...
1
// Written by Ira Snyder
1
// Written by Ira Snyder
2
// Due Date: 11-15-2004
2
// Due Date: 11-15-2004
3
// Project #3
3
// Project #3
-
 
4
// People Helped: Allen Oliver
4
 
5
 
5
import java.io.*;
6
import java.io.*;
6
import java.util.*;
7
import java.util.*;
7
class BinaryTree {
8
class BinaryTree {
8
    private Object root;
9
    private Object root;
Line 338... Line 339...
338
        return false; //return false if any condition was not met
339
        return false; //return false if any condition was not met
339
    }
340
    }
340
    
341
    
341
    
342
    
342
    //printing methods ------------------------------------------------------
343
    //printing methods ------------------------------------------------------
-
 
344
 
-
 
345
    // method to print the tree in the following order: root,left,right
-
 
346
    // Precondition:  none
-
 
347
    // Postcondition: the tree will be printed to System.out in preOrder
343
    public static void preOrderPrint( BinaryTree tree ) { 
348
    public static void preOrderPrint( BinaryTree tree ) { 
344
        System.out.print( tree.root + " " );
349
        System.out.print( tree.root + " " );
345
        if( tree.left != null ) { preOrderPrint( tree.left ); }
350
        if( tree.left != null ) { preOrderPrint( tree.left ); }
346
        if( tree.right != null ) { preOrderPrint( tree.right ); }
351
        if( tree.right != null ) { preOrderPrint( tree.right ); }
347
    }
352
    }
348
    
353
    
-
 
354
    // method to print the tree in the following order: left,right,root
-
 
355
    // Precondition:  none
-
 
356
    // Postcondition: the tree will be printed to System.out in postOrder
349
    public static void postOrderPrint( BinaryTree tree ) { 
357
    public static void postOrderPrint( BinaryTree tree ) { 
350
        if( tree.left != null ) { postOrderPrint( tree.left ); }
358
        if( tree.left != null ) { postOrderPrint( tree.left ); }
351
        if( tree.right != null ) { postOrderPrint( tree.right ); }
359
        if( tree.right != null ) { postOrderPrint( tree.right ); }
352
        System.out.print( tree.root + " " );
360
        System.out.print( tree.root + " " );
353
    }
361
    }
354
    
362
    
-
 
363
    // method to print the tree by level
-
 
364
    // Precondition:  none
-
 
365
    // Postcondition: the tree will be printed to System.out by level
355
    public static void levelOrderPrint( BinaryTree tree ) { 
366
    public static void levelOrderPrint( BinaryTree tree ) { 
356
        Queue queue = new Queue();
367
        Queue queue = new Queue();
357
        queue.enqueue(tree);
368
        queue.enqueue(tree);
358
 
369
 
359
        while( !queue.isEmpty() ) {
370
        while( !queue.isEmpty() ) {
Line 362... Line 373...
362
            if( temp.left != null ) { queue.enqueue(temp.left); }
373
            if( temp.left != null ) { queue.enqueue(temp.left); }
363
            if( temp.right != null ) { queue.enqueue(temp.right); }
374
            if( temp.right != null ) { queue.enqueue(temp.right); }
364
        }   
375
        }   
365
    }
376
    }
366
    
377
    
-
 
378
    // method to print the tree in the following order: left,root,right
-
 
379
    // Precondition:  none
-
 
380
    // Postcondition: the tree will be printed to System.out in inOrder
367
    public static void inOrderPrint( BinaryTree tree ) { 
381
    public static void inOrderPrint( BinaryTree tree ) { 
368
        if( tree.left != null ) { inOrderPrint(tree.left); }
382
        if( tree.left != null ) { inOrderPrint(tree.left); }
369
        System.out.print( tree.root + " " );
383
        System.out.print( tree.root + " " );
370
        if( tree.right != null ) { inOrderPrint(tree.right); }
384
        if( tree.right != null ) { inOrderPrint(tree.right); }
371
    }
385
    }