// BTNodeCount - count the number of nodes in the tree public class BTNodeCount { public static void main(String[] args) { BTNode root; root = beginningTree(); root.preorderPrint(); System.out.println(); root.inorderPrint(); System.out.println(); root.postorderPrint(); root.print(0); int n = countNodes(root); System.out.println("# of nodes = " + n); } public static BTNode beginningTree() { BTNode root; BTNode child; root = new BTNode(14, null, null); child = new BTNode(17, null, null); root.setLeft(child); child.setLeft(new BTNode(9, null, null)); child.setRight(new BTNode(53, null, null)); child = child.getLeft(); child.setLeft(new BTNode(13, null, null)); child = child.getLeft(); child.setRight(new BTNode(21, null, null)); child = new BTNode(11, null, null); root.setRight(child); child.setLeft(new BTNode(4, null, null)); child = child.getLeft(); child.setLeft(new BTNode(19, null, null)); child.setRight(new BTNode(7, null, null)); return root; } public static int countNodes(BTNode t) { int lMax; int rMax; if (t == null) return 0; else { if (t.isLeaf()) return 1; else return 1 + countNodes(t.getLeft()) + countNodes(t.getRight()); } } }