// Variation of the IntTree class that includes a zero-argument constructor // that constructs an empty tree and an add method that can be used to // construct a binary search tree of ints. As in the original version of // IntTree, it includes methods to construct a "sequential tree" of ints, to // print the structure, and to print the data using a preorder, inorder or // postorder traversal. public class IntTree { private IntTreeNode overallRoot; // post: constructs an empty tree public IntTree() { overallRoot = null; } // pre : tree is a binary search tree // post: value is added to overall tree so as to preserve the binary search // tree property public void add(int value) { overallRoot = add(value, overallRoot); } // pre : root is a binary search tree // post: value is added to root in a sorted fashion. private IntTreeNode add(int value, IntTreeNode root) { // "x = change x" pattern if (root == null) { root = new IntTreeNode(value); } else if (value < root.data) { root.left = add(value, root.left); } else if (value > root.data) { root.right = add(value, root.right); } return root; } // pre : this is a binary search tree // post : returns true iff the value is contained in the tree. public boolean contains(int value) { return contains(value, overallRoot); } // post : returns true if the value is in root, false otherwise. private boolean contains(int value, IntTreeNode root) { if (root == null) { return false; } else if (root.data == value) { return true; } else { if (value < root.data) { return contains(value, root.left); } else if (value > data) { return contains(value, root.right); } else { return false; } } } // post: prints the tree contents using a preorder traversal public void printPreorder() { System.out.print("preorder:"); printPreorder(overallRoot); System.out.println(); } // post: prints in preorder the tree with given root private void printPreorder(IntTreeNode root) { if (root != null) { System.out.print(" " + root.data); printPreorder(root.left); printPreorder(root.right); } } // post: prints the tree contents using an inorder traversal public void printInorder() { System.out.print("inorder:"); 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); } } // post: prints the tree contents using a postorder traversal public void printPostorder() { System.out.print("postorder:"); printPostorder(overallRoot); System.out.println(); } // post: prints in postorder the tree with given root private void printPostorder(IntTreeNode root) { if (root != null) { printPostorder(root.left); printPostorder(root.right); System.out.print(" " + root.data); } } // post: prints the tree contents, one per line, following an // inorder traversal and using indentation to indicate // node depth; prints right to left so that it looks // correct when the output is rotated. public void printSideways() { printSideways(overallRoot, 0); } // post: prints in reversed preorder the tree with given // root, indenting each line to the given level private void printSideways(IntTreeNode root, int level) { if (root != null) { printSideways(root.right, level + 1); for (int i = 0; i < level; i++) { System.out.print(" "); } System.out.println(root.data); printSideways(root.left, level + 1); } } }