/****************************************************************
 * WNode.java
 * 
 * Code to represent a node in an M-ary tree.
 */

import java.awt.Point;
import java.util.Vector;

/**
 * Represents a node (or tree) in the WalTree.
 * This is part of the WalTree package, created by Walter Korman to enable
 * the visually appealing display of M-ary trees. More documentation is 
 * available at his web site; follow the link below to get to it.
 * <P>
 * <A HREF="http://www.go2net.com/people/paulp/deep/1997/05/07/">Documentation</A>
 * <P>
 * Added by Naomi Novik:<BR>
 * The boolean field "visited" and the "visit" and "leave" methods were added 
 * for use in displaying a search as it progresses through a tree. The 
 * methods expand(), getNodeDepth(), and getTreeDepth() were added to help 
 * with searching functions,
 * so that calling classes don't need to know what the member fields of the WNode
 * are. The toString() method was added for the same reason.
 * <P>
 * @author Walter Korman
 * @author Naomi Novik
 */
public class WNode {
    String  label;
    WNode   parent, child, sibling;
    int     width, height, border;
    boolean visited;
    Point   pos, offset;
    Polygon contour;

    public WNode(String l, WNode p, WNode c, WNode s, int w, int h, int b) {
	label = l;
	parent = p;
	child = c;
	sibling = s;
	width = w;
	height = h;
	border = b;
	visited = false;
	pos = new Point(0, 0);
	offset = new Point(0, 0);
	contour = new Polygon();
    }
    
    /**
     * Return the label of the node.
     */
    public String toString() {
    	return label;
    }
    
    /**
     * Change the visited status of the node to true.
     */
    public void visit() {
    	visited = true;
    }
    
    /** 
     * Change the visited status of the node to false.
     */
    public void leave() {
    	visited = false;
    }
    
    /** 
     * Expand a node, returning a Vector containing its
     * children.
     */
    public Vector expand() {
    	Vector allChildren = new Vector();
    	WNode currentChild;
    	
	currentChild = this.child;
	while (currentChild != null) {
	    allChildren.addElement(currentChild);
	    currentChild = currentChild.sibling;
	}
	
	return allChildren;
    }
    
   /**
    * Determine depth of node within the tree.
    * The number of parents the node has represents its depth,
    * assuming the root has depth 0.
    */
   public int getNodeDepth() {
   	WNode p;
   	int depth = 0;
   	
   	p = this.parent;
   	while (p != null) {
   	    depth++;
   	    p = p.parent;
   	}
   	
   	return depth;
   }
   
   /**
    * Determine maximum depth of tree rooted at this node.
    * Assuming an even distribution, this is until there
    * are no more children in a straight line from the root.
    */
   public int getTreeDepth() {
   	WNode p;
   	int depth = 0;
   	
   	p = this;
   	while (p != null) {
   	    depth++;
   	    p = p.child;
   	}
   	
   	return depth;
    }
   	
    
};

