public class BinTree { /* This is a class of binary tree, such that each leaf of a tree one integer * element. */ private BinTree _left; // the left child private BinTree _right; // the right child private int _element; // the element only have effect if the tree is a leaf private BinTree(int element){ _left = null; _right = null; _element = element; } private BinTree(BinTree left, BinTree right){ /* this method constructs a BinTree from two BinTree * Input: * left is the left child of the root the constructed BinTree; * right is the right child of the root of the constructed BinTree; * at most one of left and right can be null. * Output: * A BinTree whose left and right children are the input BinTree's * Exception: * java.lang.IllegalArgumentException is thrown if both left and right * inputs are null; */ if ((left==null)&& (right==null) ) { throw new java.lang.IllegalArgumentException("node with both children null"); } _left = left; _right = right; } static public BinTree BinTreeLeaf(int element){ return new BinTree(element); } static public BinTree BinTreeNode(BinTree left, BinTree right){ return new BinTree(left, right); } public int getMax() { /* this method returns the maximum integer element contain in the leaves * of the tree. * Hint of algorithm: * A way to find the maximum leaf element is to find it with a * recurision. If you want to review how recurisions look like, the * method BinTree.toString() can give you some idea. */ if ((_left!=null)&&(_right!=null)){ return java.lang.Math.max(_left.getMax(), _right.getMax()); } else if ((_left==null)&&(_right==null)) { return _element; } else if ((_left!=null)&&(_right==null)) { return _left.getMax(); } else { return _right.getMax(); } } public String toString(){ if ((_left!=null)&&(_right!=null)){ return "("+_left.toString()+","+_right.toString()+")"; } else if ((_left==null)&&(_right==null)) { return Integer.toString(_element); } else if ((_left!=null)&&(_right==null)) { return "("+_left.toString()+","+"[]"+")"; } else { return "("+"[]"+","+_right.toString()+")"; } } }