### 2015-04-23 LAB

posted Apr 23, 2015, 7:43 AM by Samuel Konstantinovich   [ updated Apr 25, 2015, 6:01 AM ]
 `/*========== BTree.java ========== `` Lab: Complete`` 1. TreeNode.java`` 2. add()`` 3. pre/post/in Order()`` 4. getHeight`` 5. getLevel`` 6. toString``  ```` Basic binary tree. Uses TreeNode```` Stolen from DW.````=========================*/ import java.io.*; import java.util.*; public class BTree { public static final int PRE_ORDER = 0; public static final int IN_ORDER = 1; public static final int POST_ORDER = 2; private TreeNode root; public BTree() { root = null; } /*======== public void add() ========== Inputs: E d Returns: Wrapper method for the recursive add() ====================*/ public void add( E d ) { } /*======== public void add() ========== Inputs: TreeNode curr, TreeNode bn Returns: Adds bn to the tree rooted at curr. If curr has an available child space, then attach bn there. Otherwise, try to add at the subtree rooted at one of curr's children. Choose the child to be added to randomly. ====================*/ private void add( TreeNode curr, TreeNode bn ) { } public void traverse( int mode) { if ( mode == PRE_ORDER ) preOrder( root ); else if ( mode == IN_ORDER ) inOrder( root ); else postOrder( root ); System.out.println(); }`````` /*======== public void preOrder() ========== Inputs: TreeNode curr Returns: Prints out the elements in the tree by doing an pre-order Traversal ====================*/ public void preOrder( TreeNode curr ) { } /*======== public void inOrder() ========== Inputs: TreeNode curr Returns: Prints out the elements in the tree by doing an in-order Traversal ====================*/ public void inOrder( TreeNode curr ) { } /*======== public void postOrder() ========== Inputs: TreeNode curr Returns: Prints out the elements in the tree by doing a post-order Traversal `````` ====================*/ public void postOrder( TreeNode curr ) { } /*======== public int getHeight()) ========== Inputs: Returns: The height of the tree Wrapper for the recursive getHeight method ====================*/ public int getHeight() { return getHeight( root ); } /*======== public int getHeight() ========== Inputs: TreeNode curr Returns: The height of the tree rooted at node curr ====================*/ public int getHeight( TreeNode curr ) { return -1; /*======== private String getLevel() ========== Inputs: TreeNode curr int level int currLevel Returns: A string containing all the elements on the given level with respect to the curr treenode ordered left -> right ====================*/ private String getLevel( TreeNode curr, int level) { return ""; } `````` /*======== public String toString()) ========== Inputs: Returns: A string representation of the tree This string should display each level as a separate line. A simple version might look something like this: 0 1 2 3 4 5 Note that you cannot tell exactly where 3, 4 and 5 lie. That is ok, but if you want a CHALLENGE, you can try to get the output to look nicer, something like this:          0```` 1 2`` 3 4 5```` ====================*/ public String toString() { return ""; } public static void main( String[] args ) { BTree t = new BTree(); for ( int i=0; i < 8; i++ ) t.add( i ); System.out.println( "Pre-order: "); t.traverse( PRE_ORDER ); System.out.println( "In-order: "); t.traverse( IN_ORDER ); System.out.println( "Post-order: "); t.traverse( POST_ORDER ); System.out.println( "Height: " + t.getHeight() ); System.out.println( t ); } }```