// CSE 143, winter 2012 // An IntTree object represents an entire binary tree of ints. public class IntTree { IntTreeNode overallRoot; // Constructs an empty binary tree public IntTree() { overallRoot = null; } // Constructs a binary tree with the given node as its root. public IntTree(IntTreeNode overallRoot) { this.overallRoot = overallRoot; } // Prints all elements of this tree in left to right order. public void print() { print(overallRoot); } // Prints a portion of the overall tree private void print(IntTreeNode root) { // implicit base case: if null, do nothing if (root != null) { print(root.left); // print my left sub-tree System.out.print(root.data + " "); // print myself print(root.right); // print my right sub-tree } } // Returns true if the overall tree contains the given target value, // false otherwise public boolean contains(int target) { return contains(overallRoot, target); } // Returns true if a portion of the overall tree contains the given // target value, false otherwise. private boolean contains(IntTreeNode root, int target) { if (root == null) { return false; } else if (root.data == target) { return true; } else { return contains(root.left, target) || contains(root.right, target); } } }