import java.util.*; // An IntTree object represents an entire binary tree of ints. public class IntTree { private IntTreeNode overallRoot; // null for an empty tree public static final int MAX_NUM = 100; public IntTree(int height) { Random rand = new Random(); overallRoot = createTree(height, rand); } private IntTreeNode createTree(int height, Random rand) { if (height == 0) { return null; } else { return new IntTreeNode(rand.nextInt(MAX_NUM), createTree(height - 1, rand), createTree(height - 1, rand)); } } public void print() { print(overallRoot); System.out.println(); // end the line of output } private void print(IntTreeNode root) { // (base case is implicitly to do nothing on null) if (root != null) { // recursive case: print left, center, right print(root.left); System.out.print(root.data + " "); print(root.right); } } // Prints the tree in a sideways indented format. public void printSideways() { printSideways(overallRoot, ""); } private void printSideways(IntTreeNode root, String indent) { if (root != null) { printSideways(root.right, indent + " "); System.out.println(indent + root.data); printSideways(root.left, indent + " "); } } }