Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

TreeNode.java

00001 package gov.nasa.arc.ase.util;
00002 
00003 //#ifdef JDK11
00004 
00005 
00006 
00007 //#else JDK11
00008 import java.util.Collection;
00009 import java.util.ArrayList;
00010 import java.util.Iterator;
00011 //#endif JDK11
00012 
00013 /**
00014  * This is a node in a tree.
00015  */
00016 public class TreeNode {
00017   /**
00018    * The tree the node belongs to.
00019    */
00020   private Tree tree;
00021 
00022   /**
00023    * The parent of this node.
00024    */
00025   private TreeNode parent;
00026 
00027   /**
00028    * The children of this node.
00029    */
00030   private Collection children;
00031 
00032   /**
00033    * Creates a new node as the root of a tree.
00034    */
00035   public TreeNode(Tree t) {
00036     tree = t;
00037     parent = null;
00038     children = new ArrayList();
00039 
00040     t.setRoot(this);
00041   }  
00042   /**
00043    * Creates a new node under a given node.
00044    */
00045   public TreeNode(TreeNode p) {
00046     tree = p.tree;
00047     parent = p;
00048     children = new ArrayList();
00049 
00050     p.addChild(this);
00051   }  
00052   /**
00053    * Returns the set of nodes above this one.
00054    */
00055   public Collection above() {
00056     Collection a = new ArrayList();
00057 
00058     if(parent != null) {
00059       a.add(parent);
00060       a.addAll(parent.above());
00061     }
00062 
00063     return a;
00064   }  
00065   /**
00066    * Adds a children.
00067    */
00068   private void addChild(TreeNode n) {
00069     children.add(n);
00070   }  
00071   /**
00072    * Returns the set of nodes below this one.
00073    */
00074   public Collection below() {
00075     Collection b = new ArrayList();
00076 
00077     for(Iterator i = children.iterator(); i.hasNext(); ) {
00078       TreeNode n = (TreeNode)i.next();
00079 
00080       b.add(n);
00081       b.addAll(n.below());
00082     }
00083 
00084     return b;
00085   }  
00086   /**
00087    * Returns the children of a node.
00088    */
00089   public Collection children() {
00090     return children;
00091   }  
00092   /**
00093    * Gets all the node under this one that are
00094    * equal to a given one.
00095    */
00096   public Collection get(TreeNode n) {
00097     Collection nodes = new ArrayList();
00098 
00099     if(equals(n)) nodes.add(this);
00100 
00101     for(Iterator i = children.iterator(); i.hasNext(); ) {
00102       TreeNode child = (TreeNode)i.next();
00103 
00104       nodes.addAll(child.get(n));
00105     }
00106 
00107     return nodes;
00108   }  
00109   /**
00110    * Returns the parent of this node.
00111    */
00112   public TreeNode getParent() {
00113     return parent;
00114   }  
00115   /**
00116    * Returns the tree the node belongs to.
00117    */
00118   public Tree getTree() {
00119     return tree;
00120   }  
00121   /**
00122    * Returns a string rapresentation of the node and the nodes below.
00123    */
00124   public String toString(String prefix) {
00125     StringBuffer sb = new StringBuffer();
00126 
00127     sb.append(' ');
00128     sb.append(this);
00129     sb.append('\n');
00130 
00131     for(Iterator i = children.iterator(); i.hasNext(); ) {
00132       TreeNode child = (TreeNode)i.next();
00133 
00134       sb.append(prefix);
00135       if(i.hasNext()) {
00136     sb.append('+');
00137     sb.append(child.toString(prefix + "|  "));
00138       } else {
00139     sb.append('\\');
00140     sb.append(child.toString(prefix + "   "));
00141       }
00142     }
00143 
00144     return sb.toString();
00145   }  
00146 }

Generated at Thu Feb 7 06:58:06 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001