// Erika Wolfe, 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; } // 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); } } /////////////////////////////////////////////// // post: prints the tree contents using an inorder traversal public void printInorder() { printInorder(overallRoot); System.out.println(); } // post: prints in inorder the tree with given root private void printInorder(IntTreeNode root) { if (root != null) { printInorder(root.left); System.out.print(" " + root.data); printInorder(root.right); } } // prints the numbers in this tree in a pre-order fashion. public void printPreorder() { printPreorder(overallRoot); System.out.println(); } // prints the numbers of the subtree beginning at root // using a pre-order traversal. private void printPreorder(IntTreeNode root) { if (root != null) { // root, left, right System.out.print(root.data + " "); print(root.left); 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; } } }