
/**
 * PostOrderVisitor.java
 *
 *
 * Created: Thu Oct 17 21:29:54 2002
 *
 * @author <a href="mailto:bina@cse.buffalo.edu "</a>
 * @version
 */
import java.util.*;

public class PostOrderVisitor implements Visitor{
   public PostOrderVisitor (){
      
   }

   public Object operation(Object obt, Object inp)
   {  
      ArrayList tmp = new ArrayList();
      BTree bt = (BTree) obt;

      // base case: empty node
      if ( bt == null) {
	 return null;
      } // end of if ()
      
      // base case: leaf
       if ( (bt.getLeft()==null)&& (bt.getRight()==null) ) {
      	  tmp.add(bt.getData());
	  return (tmp.listIterator());
       } // end of if ()

       // recursive step for left and right subtrees
      Object root = bt.getData();
      Iterator lList = (Iterator)(bt.getLeft().acceptVisitor(this, null));
      Iterator rList =(Iterator)(bt.getRight().acceptVisitor(this,null));

      //  collect the nodes and pass it back in an Iterator
      while (lList.hasNext()) {
	 tmp.add(lList.next());
      } // end of while ()
      while (rList.hasNext()) {
	 tmp.add(rList.next());
      } // end of while ()
      tmp.add(root);      

      return (tmp.listIterator());
   }
}// PostOrderVisitor
