// Hunter Schafer, CSE 143 // This class represents a tree of integers public class IntTree { private IntTreeNode overallRoot; // Constructs a tree with default numbers public IntTree() { // overallRoot = null; IntTreeNode overall = 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); } // Returns true if the given value is stored in this tree, false otherwise public boolean contains(int value) { return contains(overallRoot, value); } // Returns true if the given value is stored in the subtree // rooted at the given root, false otherwise private boolean contains(IntTreeNode root, int value) { if (root == null) { return false; } else if (root.data == value) { return true; } else { // not null, and root.data != value return contains(root.left, value) || contains(root.right, value); } } // 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 the number of numbers in this tree. public int size() { return size(overallRoot); } // Returns the number of nodes in the tree starting at root. private int size(IntTreeNode root) { if (root == null) { return 0; } else { int leftSize = size(root.left); int rightSize = size(root.right); return 1 + leftSize + rightSize; } } }