// This class represents a tree of integers public class IntTree { private IntTreeNode overallRoot; // Constructs a tree with default numbers public IntTree() { // overallRoot = null; overallRoot = new IntTreeNode(17); overallRoot.left = new IntTreeNode(41); overallRoot.right = new IntTreeNode(9); overallRoot.left.left = new IntTreeNode(29); overallRoot.left.right = new IntTreeNode(6); overallRoot.right.left = new IntTreeNode(81); overallRoot.right.right = new IntTreeNode(40); } // prints the numbers in this tree in a pre-order fashion. public void print() { print(overallRoot); } // prints the numbers of the subtree rooted at root // using a pre-order traversal. private void print(IntTreeNode root) { if (root != null) { System.out.print(root.data + " "); // print the left print(root.left); // print the right print(root.right); } } // Returns true if n is in this tree, false otherwise. public boolean contains(int n) { // Hunter: From reading return contains(overallRoot, n); } // Returns true if n appears as the data for a node in the sub-tree rooted // at root. Returns false if the tree is empty. private boolean contains(IntTreeNode root, int n) { if (root == null) { return false; } else if (root.data == n) { return true; } else { return contains(root.left, n) || contains(root.right, n); } } // Class that represents a single node in the tree private static class IntTreeNode { public int data; // data stored at this node public IntTreeNode left; // reference to left subtree public IntTreeNode right; // reference to right subtree // Constructs a leaf node with the given data. public IntTreeNode(int data) { // Hunter: Added constructors after lecture // Hunter: Remember to reduce redundancy in constructors this(data, null, null); } // Constructs a leaf or branch node with the given data and links. public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) { this.data = data; this.left = left; this.right = right; } } }