// Zorah Fung, CSE 143 // An IntTree object represents an entire binary tree of ints. public class IntTree { private IntTreeNode overallRoot; // Constructs an empty binary tree. public IntTree() { overallRoot = null; } // Constructs a binary tree with the given node as its root. // Note: this is hacky and just useful for testing public IntTree(IntTreeNode root) { overallRoot = root; } // Prints a pre-order traversal of this tree. public void printPreorder() { printPreorder(overallRoot); } // Prints a pre-order traversal of the tree starting at the given root private void printPreorder(IntTreeNode root) { // implicit base case: do nothing when we reach a null root if (root != null) { System.out.print(root.data + " "); printPreorder(root.left); printPreorder(root.right); } } // Prints an in-order traversal of this tree. public void printInorder() { printInorder(overallRoot); } // Prints an in-order traversal of the tree starting at the given root private void printInorder(IntTreeNode root) { if (root != null) { printInorder(root.left); System.out.print(root.data + " "); printInorder(root.right); } } // Prints a post-order traversal of this tree. public void printPostorder() { printPostorder(overallRoot); } // Prints a post-order traversal of the tree starting at the given root private void printPostorder(IntTreeNode root) { if (root != null) { printPostorder(root.left); printPostorder(root.right); System.out.print(root.data + " "); } } // Returns the number of elements in this tree. public int size() { return size(overallRoot); } // Returns the number of elements in the tree at the given root. private int size(IntTreeNode root) { if (root == null) { return 0; } else { return size(root.left) + size(root.right) + 1; } } }