// exercise:  create a TreeNode class, where each
// node has a parent and each node has n children stored as a vector
// the TreeNode class will have the following public interface:
/*  	public class TreeNode{
            
            public TreeNode (); 
            
            public TreeNode (int d);  {
                  
            public void addChild(TreeNode child);  
            
            public Vector getChildren();  

            public TreeNode getParent();  
            
            public void setParent(TreeNode parent); 
            
            public int getData(); 
            
            public void setData(int data); 
            
            public int getNumDescendants(); 
            
            public void setNumDescendants(int n); 
            
            public String toString(); // should print all descendants of the node
             
            // also add a main method to test your code!
             
*/ 
            
import java.util.Vector;
import java.util.Enumeration;

      public class TreeNode{
            private Vector children;
            private TreeNode parent;
            private int data;
            private int descendants;
            
            public TreeNode () {
                  children = new Vector();
                  data = 0;
                  descendants = 0;
            }
            
            public TreeNode (int d) {
                  children = new Vector();
                  data = d;
                  descendants = 0;
            }
                     
            public void addChild(TreeNode child) {
            	child.setParent(this);
                children.add(child);             
            }

            public Vector getChildren() {
                  return children;
            }

            public TreeNode getParent() {
                  return this.parent;
            }
  
            public void setParent(TreeNode parent)  {
            	  this.parent = parent;
            }
            public int getData() {
                  return this.data;
            }
  
            public void setData(int data)  {
            	  this.data = data;
            }
            
            public int getNumDescendants() {
                  return this.descendants;
            }
  
            public void setNumDescendants(int n)  {
            	  this.descendants = n;
            }
           
            // creates a string that outputs the nodes in the tree in
            // preorder 
            public String toString() {
            		String tmp = Integer.toString(data);
            		for (int i = 0; i<children.size(); i++) {
            			TreeNode child = (TreeNode)children.get(i);
            			tmp = tmp + " " + child.toString() ;
            		}
            		return tmp;
            }
            
            
        // try adding a postorder traversal iterator
      	private class PostIterator implements Enumeration {
   			private int curNode;
   			private Vector nodes;
   			private int curSize;

   			public PostIterator(TreeNode root) {
      			nodes = new Vector();
      			curNode = 0;
      			curSize = 0;
      			postTour(root); // populate our vector with the order we want
      			curNode = 0;
   			}
   		
   	   		private void postTour(TreeNode t) {
   				if (t != null) {
      				Vector children = t.getChildren();
      				for (int i=0; i<children.size(); i++) {                           
                  		TreeNode child = (TreeNode)children.elementAt(i);
                  		postTour(child);
            		}
            		nodes.add(t);
            		curNode++; curSize++;
   				}
   	   		}
   	   		
			public boolean hasMoreElements() {
   				return curNode < curSize;
			}
			public Object nextElement() {
   				return nodes.elementAt(curNode++);
			}

	   } // end of private class
	
	   // return a postorder traversal iterator, just like assignment #2
       public Enumeration postIterator() {
        	return new PostIterator(this);
       }   	         
         
              
      public static void main(String args[]) {
      		// test TreeNode first!
      		
      		TreeNode tnode = new TreeNode(1);
      		TreeNode cnode1 = new TreeNode(12);
      		TreeNode cnode2 = new TreeNode(13);
      		tnode.addChild(cnode1);
      		tnode.addChild(cnode2);
      		TreeNode gnode1 = new TreeNode(21);
      		TreeNode gnode2 = new TreeNode(22);
      		cnode1.addChild(gnode1);
      		cnode1.addChild(gnode2);
      		System.out.println("The tree is : " + tnode.toString());
      		
      		
      		//test if the eulertour traversal works properly!
            Enumeration postIt = tnode.postIterator();   
            while (postIt.hasMoreElements()) {
  		          System.out.println("Data for this node is: " +
  		         		((TreeNode)postIt.nextElement()).getData() );
  			}  
  			
     
      }
}

 

 
