MinTreeFinder.java
Created with JBuilder

package brs.visitor;

import brs.*;

/**
 * Returns the subtree of the host with the min value in the root
 * if the tree is not empty, otherwise returns the host itself.
 */
public class MinTreeFinder implements IVisitor {
    public final static MinTreeFinder Singleton = new MinTreeFinder ();

    private MinTreeFinder() {
    }

    /**
    * The host tree is empty: the tree containing the min 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 left subtree of the host to help find the min.  Use anonymous class as helper.
    * @param host satisfies the binary search tree property.
    * @param input not used.
    * @return subtree with minimum root.
    */
    public Object nonEmptyCase (BiTree host, Object input) {
        return host.getLeftSubTree().execute(new IVisitor () {
            public Object emptyCase (BiTree h, Object inp) {
               return inp;
            }

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



MinTreeFinder.java
Created with JBuilder