package binaryTree.visitor; import binaryTree.IVisitor; import binaryTree.BiTree; /** * An object-oriented leaf counter. Uses anonymous inner classes to "help" count * the leaves of a BiTree host. *@author Dung X. Nguyen - Copyright 2000 - All rights reserved. */ public class OOLeafCounter implements IVisitor { public final static OOLeafCounter Singleton = new OOLeafCounter (); private OOLeafCounter() { } /** * Returns 0 - empty tree has no leaf. * @param host * @param inputNotUsed not needed. * @return Integer (0). */ public Object emptyCase(BiTree host, Object inputNotUsed) { return new Integer (0); } /** * Asks the subtrees to "help" count the leaves. * @param host * @param inputNotUsed not needed. * @return */ public Object nonEmptyCase(BiTree host, Object inputNotUsed) { // passes the right subtree to the left subtree // and ask the left subtree to help count the leaves. return host.getLeftSubTree().execute ( new IVisitor () { // hst, one of the host's subtree, is empty: // host may or may not be a leaf depending on the sibling subtree, sib! // asks the sibling subtree, sib, to "help" count. public Object emptyCase (BiTree hst, Object sib) { return ((BiTree)sib).execute ( new IVisitor () { // both host's subtrees, hst and sib, are empty: // host is a leaf, and the leaf count is 1. public Object emptyCase (BiTree h, Object inpNotUsed) { return new Integer (1); } // Case when sib is not empty and the other subtree is empty: // the leaf count is just the leaf count of sib (i.e. h). public Object nonEmptyCase (BiTree h, Object inpNotUsed) { return h.execute (OOLeafCounter.Singleton, null); } } , null); } // hst, one of the host's subtree, is not empty: host is not a leaf! // host's leaf count is the sum of the subtrees' leaf count. public Object nonEmptyCase (BiTree hst, Object sib) { Integer hstCount = (Integer)hst.execute (OOLeafCounter.Singleton, null); Integer sibCount = (Integer)((BiTree)sib).execute (OOLeafCounter.Singleton, null); return new Integer (hstCount.intValue () + sibCount.intValue ()); } } , host.getRightSubTree()); } /** * @param args == null - not used. */ public static void main(String[] args) { Integer level0 = new Integer (0); BiTree t0 = new BiTree (); System.out.println ("t0 is "); t0.execute (VerticalPrinter.Singleton, level0); System.out.println("\nt0 leaf count is: " + t0.execute(OOLeafCounter.Singleton, null)); t0.insertRoot (new Integer (5)); BiTree t1 = new BiTree (); t1.insertRoot (new Integer (-7)); BiTree t2 = new BiTree (); t2.insertRoot (new Integer (3)); System.out.println ("\nt2 is "); t2.execute (VerticalPrinter.Singleton, level0); System.out.println("\nt2 leaf count is: " + t2.execute(OOLeafCounter.Singleton, null)); t1.setLeftSubTree(t0); System.out.println ("\nt1 is "); t1.execute (VerticalPrinter.Singleton, level0); System.out.println("\nt1 leaf count is: " + t1.execute(OOLeafCounter.Singleton, null)); t1.setRightSubTree (t2); System.out.println ("\nt1 is "); t1.execute (VerticalPrinter.Singleton, level0); System.out.println("\nt1 leaf count is: " + t1.execute(OOLeafCounter.Singleton, null)); } }