home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / ui / tree / Tree.java < prev    next >
Text File  |  1997-01-02  |  2KB  |  103 lines

  1. /* file      : Tree.java
  2.  * purpose   : represent a Tree structure by it's root node
  3.  *             offer some usefull operations like add/remove
  4.  * author    : Thomas Koch
  5.  * copyright : 1995 , GMD
  6.  *            (Forschungszentrum Informationstechnik GmbH, FIT.CSCW,
  7.  *             GMD Schloss Birlinghoven, 53754 Sankt Augustin, Germany).
  8.  */
  9.  
  10. package ui.tree;
  11.  
  12. import java.lang.*;
  13. import java.util.*;
  14. import java.awt.*;
  15. import ui.tree.*;
  16.  
  17. public class Tree
  18. {
  19.   TreeNode root;
  20.  
  21.   public Tree(TreeNode nr) {
  22.     root = nr;
  23.   }
  24.  
  25.   public void setRoot(TreeNode nr) {
  26.     root = nr;
  27.   }
  28.  
  29.   public TreeNode findNode(TreeNode start, TreeNode search) {
  30.     if (start.equals(search))
  31.       return start;
  32.     else {
  33.       if (start.isLeaf())
  34.     return null;
  35.       else
  36.     {
  37.       int num = start.numberChilds(); 
  38.       int i=0;
  39.       TreeNode found = null;
  40.       while ((found==null) && (i<num)) {
  41.         found = findNode(start.getChild(i),search);
  42.         i++;
  43.       }
  44.       return found;
  45.     }
  46.     }
  47.   }
  48.   
  49.  
  50.   public TreeNode findParent(TreeNode start,TreeNode search) {
  51.     if (start.equals(search))  
  52.       return null;
  53.     if (start.isLeaf())
  54.       return null;
  55.     if (start.isChild(search))
  56.       return start;
  57.     
  58.     int num = start.numberChilds(); 
  59.     int i=0;
  60.     TreeNode found = null;
  61.     while ((found==null) && (i<num)) {
  62.       found = findParent(start.getChild(i),search);
  63.       i++;
  64.     }
  65.     return found;
  66.   }
  67.   
  68.  
  69.   public void addNode(TreeNode where,TreeNode n) {
  70.     TreeNode pos = findNode(root,where);
  71.     if (pos==null)
  72.       System.out.println("Tree::addNode() Error - node not found");
  73.     else pos.addChild(n);
  74.   }
  75.   
  76.  
  77.   public void removeNode(TreeNode n) { // includes subtree removal !
  78.     // System.out.println("Tree::removeNode("+n+")");
  79.     if(root == n)
  80.         root.removeSubtree();
  81.     else
  82.     {
  83.         TreeNode par = findParent(root,n);
  84.         if (par!=null)
  85.             par.removeChild(n);
  86.     }
  87.   }
  88.  
  89.  
  90.   public void removeNode(TreeNode n,TreeTool t) { // includes subtree removal !
  91.     // System.out.println("Tree::removeNode("+n+",TreeTool)");
  92.     if(root.equals(n)) 
  93.         root.removeSubtree(t);
  94.     else
  95.     {
  96.         TreeNode par = findParent(root,n);
  97.         if (par!=null)
  98.             par.removeChild(n,t);
  99.     }
  100.   }
  101.   
  102. }
  103.