// Allison Obourn // CSE 143 - lecture 20 // 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); System.out.println(); } // Prints a pre-order traversal of the tree starting at the specified root private void printPreorder(IntTreeNode 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); System.out.println(); } // Prints an in-order traversal of the tree starting at the specified 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); System.out.println(); } // Prints a post-order traversal of the tree starting at the specified root private void printPostorder(IntTreeNode root) { if(root != null) { printPostorder(root.left); printPostorder(root.right); System.out.print(root.data + " "); } } // Returns true if the value is in this tree, false otherwise public boolean contains(int value) { return contains(value, overallRoot); } // Returns true if the value is in the tree starting at the // specified root. private boolean contains(int value, IntTreeNode root) { if(root == null) { return false; } else if (root.data == value) { return true; } else { return contains(value, root.left) || contains(value, root.right); } } }