MaxTreeFinder.java
Created with JBuilder

package brs.visitor;

import brs.*;
import ordering.*;

/**
 * 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 {
    public static final MaxTreeFinder Singleton = new MaxTreeFinder ();

    private MaxTreeFinder ()  {
    }

    /**
    * The host tree is empty: the tree containing the max is the host itself.
    * @param host satisfies the binary search tree property.
    * @param input == null, not used.
    * @return host.
    */
    public Object emptyCase(BiTree host, Object input) {
        return host;
    }

    /**
    * Asks the right subtree of the host to find the max.
    * Uses anynonymous class as helper.
    * @param host satisfies the binary search tree property.
    * @param input == null, not used.
    * @return subtree with maximum root.
    */
    public Object nonEmptyCase (BiTree host, Object input)  {
        return host.getRightSubTree().execute (new IVisitor () {
            public Object emptyCase (BiTree h, Object inp) {
               return inp;
            }

            public Object nonEmptyCase (BiTree h, Object inp) {
               return h.getRightSubTree ().execute (this, h);
            }
        }, host);
    }
}



MaxTreeFinder.java
Created with JBuilder