/* * Jeremy Lipschutz * * A class representing a tree of integers */ public class IntTree { private IntTreeNode overallRoot; // Constructs an empty tree public IntTree() { overallRoot = null; } // Prints the preorder traversal of the current tree public void printPreorder() { System.out.print("preorder: "); printPreorder(overallRoot); System.out.println(); } // Prints the preorder traversal of the tree represented by the // given root of it private void printPreorder(IntTreeNode root) { if(root != null) { System.out.print(root.data + " "); printPreorder(root.left); printPreorder(root.right); } } // Prints the preorder traversal of the current tree public void printInOrder() { System.out.print("inorder: "); printInOrder(overallRoot); System.out.println(); } // Prints the in order traversal of the tree represented by the // given root of it private void printInOrder(IntTreeNode root) { if(root != null) { printInOrder(root.left); System.out.print(root.data + " "); printInOrder(root.right); } } // Prints the preorder traversal of the current tree public void printPostOrder() { System.out.print("postorder: "); printPostOrder(overallRoot); System.out.println(); } // Prints the post order traversal of the tree represented by the // given root of it 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; prints "empty" for an empty tree public void printSideways() { if(overallRoot == null) { System.out.println("empty tree"); } else { 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); } } // pre: the tree is a binary search tree // Returns whether or not a value is contained within the current // tree public boolean contains(int value) { return contains(overallRoot, value); } // Returns whether or not a value is contained within the // tree represented by the given root of it private boolean contains(IntTreeNode root, int value) { if(root == null) { // The empty tree holds no values, so the value can't be // in this tree return false; } else if (root.data == value) { return true; } else if (root.data > value) { return contains(root.left, value); } else { // root.data < value return contains(root.right, value); } } // Adds the given value into the current tree, maintaining // the BST property public void add(int value) { overallRoot = add(overallRoot, value); } // Add in the given value into the tree represented by the // given root, maintaining the BST property private IntTreeNode add(IntTreeNode root, int value) { if(root == null) { root = new IntTreeNode(value); } else if (root.data > value) { root.left = add(root.left, value); } else if (root.data < value) { root.right = add(root.right, value); } return root; } // DO NOT DO THIS CODE OF "STOPPING ONE EARLY" // Trust in x=change(x)!! You can get to a null part // of the tree and still be fine! // Adds the given value into the current tree public void badAdd(int value) { if (overallRoot == null) { overallRoot = new IntTreeNode(value); } else { badAdd(value, overallRoot); } } // DO NOT DO THIS CODE OF "STOPPING ONE EARLY" // Trust in x=change(x)!! You can get to a null part // of the tree and still be fine! // Adds the given value into the tree represented by the given root private void badAdd(int value, IntTreeNode root) { if (value < root.data) { if (root.left == null) { root.left = new IntTreeNode(value); } else { badAdd(value, root.left); } } else if (value > root.data) { if (root.right == null) { root.right = new IntTreeNode(value); } else { badAdd(value, root.right); } } } }