
/**
 * DepVisitor.java
 *
 *
 * Created: Thu Oct 17 17:40:56 2002
 *
 * @author <a href="mailto:bina@cse.buffalo.edu "</a>
 * @version
 */
import java.util.*;
public class DepVisitor implements Visitor{

   public Object operation(Object obt, Object inp)
   {
      int depth = ((Integer)inp).intValue();
      BTree bt = (BTree) obt;
      ArrayList al = new ArrayList();
   
      if ( bt == null) {
	 return null;
      } // end of if ()

      // base case: leaf
       if ( (bt.getLeft()==null)&& (bt.getRight()==null) ) {
	  al.add(new NodeDepth(bt.getData(),depth));
	  return(al.listIterator());
       } // end of if ()
       
       // recursive step for left and right subtrees

      Iterator ll = (Iterator)(bt.getLeft().acceptVisitor(this,new Integer(depth+1))); 
      while (ll.hasNext()) {
	 al.add(ll.next());
      } // end of while ()

      //      System.out.println("depth" + depth);
      al.add(new NodeDepth(bt.getData(),depth));
      Iterator rl = (Iterator)(bt.getRight().acceptVisitor(this,new Integer(depth+1)));
      while (rl.hasNext()) {
	 al.add(rl.next());
      } // end of while ()
      return al.listIterator();

   }
}// DepthVisitor
