package binaryTree.visitor; import binaryTree.BiTree; /** * Returns the subtree of the host with the max value in the root if the tree is not empty, otherwise returns the host itself. */ public class MaxTreeFinder implements IVisitor { /** @SBGen Singleton Variable */ public final static MaxTreeFinder Singleton = new MaxTreeFinder (); /** @SBGen Constructor */ private MaxTreeFinder() { } /** * @param host satisfies the binary search tree property. * @param input == null, not used. * @return host. */ public Object nullCase(BiTree host, Object input) { return host; } /** * @param host satisfies the binary search tree property. * @param input == null, not used. * @return subtree with maximum root. */ public Object nonNullCase(BiTree host, Object input) { Boolean isRightEmpty = (Boolean)host.getRightSubTree().execute (EmptyChecker.Singleton, null); return isRightEmpty.booleanValue()? host: host.getRightSubTree ().execute (this, null); } }