public class bst // for Integer { private TreeNode root; public bst() {root = null; } public void add(Object obj) {TreeNode n = new TreeNode(obj,null,null); if (root == null) root = n; else insert(root,n); } private void insert(TreeNode current, TreeNode t) {Integer i1 = (Integer)t.getValue(); Integer i2 = (Integer)current.getValue(); if (i1.compareTo(i2) < 0 && current.getLeft() == null) {current.setLeft(t); return; } else if (i1.compareTo(i2) >= 0 && current.getRight() == null) {current.setRight(t); return; } else if (i1.compareTo(i2) < 0 ) insert(current.getLeft(),t); else insert(current.getRight(),t); } public void print() {printer(root); } private void printer(TreeNode current) {if (current != null) {printer(current.getLeft()); System.out.println(current.getValue()); // uses toString() of data object printer(current.getRight()); } } public int countNodes() {return ctNodes(root); } private int ctNodes(TreeNode current) {if (current == null) return 0; return 1 + ctNodes(current.getLeft()) + ctNodes(current.getRight()); } public void printLeaves() {printLeavesHelper(root); } public void printLeavesHelper(TreeNode current) {if (current == null) return; if (current.getLeft() == null && current.getRight() ==null) System.out.println(current.getValue() ); //uses toString of data object else {printLeavesHelper(current.getLeft()); printLeavesHelper(current.getRight()); } } public void deleteLeaves() {deleteLeavesHelper(root); } public void deleteLeavesHelper(TreeNode current) {if (current != null) { if (current.getLeft() != null && (current.getLeft().getLeft()==null && current.getLeft().getRight() == null)) current.setLeft(null); if (current.getRight() != null && (current.getRight().getLeft()==null && current.getRight().getRight() == null)) current.setRight(null); deleteLeavesHelper(current.getLeft()); deleteLeavesHelper(current.getRight()); } } }___________________________________________________________________________________ import java.io.*; public class treetest2 { public static void main(String[] args) throws IOException {input in = new input(); bst b = new bst(); int n; System.out.println("Enter a number or -1 to end"); n = in.getInt(); while (n != -1) {b.add(new Integer(n)); System.out.println("Enter a number or -1 to end"); n = in.getInt(); } b.print(); System.out.println("Leaves are "); b.printLeaves(); int ct = b.countNodes(); // no parameters required System.out.println("There are " + ct + " nodes"); } }