/* * Kyle Pierce * CSE 143 * * This class represents a binary tree of integers */ public class IntTree { private IntTreeNode overallRoot; // post: constructs an empty tree public IntTree() { overallRoot = null; } // pre: tree is a binary search tree // post: value added to overall tree to preserve // binary search tree property public void add(int value) { overallRoot = add(overallRoot, value); } // pre: tree is a binary search tree // post: value added to tree with given root to preserve // binary search tree property public IntTreeNode add(IntTreeNode root, int value) { if (root == null) { root = new IntTreeNode(value); } else if (value <= root.data) { root.left = add(root.left, value); } else { root.right = add(root.right, value); } return root; } // 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); } } /* * Here is another (bad) version of the add method that * uses stopping one early / looking ahead instead of the * x = change(x) pattern (notice there is no return from the * private helper method). This is a stylistically worse * and more complicated solution, so we would greatly prefer * you do not write your code this way for the assignments. * Style points will be lost if you write code like this. */ public void badAdd(int value) { /* * Notice the extra case needed for reassigning overallRoot */ if (overallRoot == null) { overallRoot = new IntTreeNode(value); } else { badAdd(overallRoot, value); } } private void badAdd(IntTreeNode root, int value) { /* * Notice the redundancy introduced for dealing with the left * and right subtrees... the code in both is nearly identical, * but we had to write it twice just because we stopped when * root.left == null or root.right == null rather than going on * until root == null */ if (value <= root.data) { if (root.left == null) { root.left = new IntTreeNode(value); } else { badAdd(root.left, value); } } else if (value > root.data) { if (root.right == null) { root.right = new IntTreeNode(value); } else { badAdd(root.right, value); } } } }