// BinaryNode class; stores a node in a tree. // Original source: http://www.cs.fiu.edu/~weiss/dsj2/code/BinaryNode.java // Modifications to Weiss's code and comments by DIS // *******************PUBLIC OPERATIONS********************** // void printPostOrder( ) --> Print a postorder tree traversal // void printInOrder( ) --> Print an inorder tree traversal // void printPreOrder( ) --> Print a preorder tree traversal public class BinaryNode { private Object element; private BinaryNode left; private BinaryNode right; public BinaryNode( ) { this( null, null, null ); } public BinaryNode(Object theElement) { this( theElement, null, null ); } public BinaryNode( Object theElement, BinaryNode lt, BinaryNode rt ) { element = theElement; left = lt; right = rt; } // Print tree rooted at current node using preorder traversal. public void printPreOrder( ) { System.out.println( element ); // Node if( left != null ) left.printPreOrder( ); // Left if( right != null ) right.printPreOrder( ); // Right } // Print tree rooted at current node using postorder traversal. public void printPostOrder( ) { if( left != null ) left.printPostOrder( ); // Left if( right != null ) right.printPostOrder( ); // Right System.out.println( element ); // Node } // Print tree rooted at current node using inorder traversal. public void printInOrder( ) { if( left != null ) left.printInOrder( ); // Left System.out.println( element ); // Node if( right != null ) right.printInOrder( ); // Right } public Object getElement( ) { return element; } public BinaryNode getLeft( ) { return left; } public BinaryNode getRight( ) { return right; } public void setElement( Object x ) { element = x; } public void setLeft( BinaryNode t ) { left = t; } public void setRight( BinaryNode t ) { right = t; } } // Class BinaryNode