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 >
Wrap
Text File
|
1997-01-02
|
2KB
|
103 lines
/* file : Tree.java
* purpose : represent a Tree structure by it's root node
* offer some usefull operations like add/remove
* author : Thomas Koch
* copyright : 1995 , GMD
* (Forschungszentrum Informationstechnik GmbH, FIT.CSCW,
* GMD Schloss Birlinghoven, 53754 Sankt Augustin, Germany).
*/
package ui.tree;
import java.lang.*;
import java.util.*;
import java.awt.*;
import ui.tree.*;
public class Tree
{
TreeNode root;
public Tree(TreeNode nr) {
root = nr;
}
public void setRoot(TreeNode nr) {
root = nr;
}
public TreeNode findNode(TreeNode start, TreeNode search) {
if (start.equals(search))
return start;
else {
if (start.isLeaf())
return null;
else
{
int num = start.numberChilds();
int i=0;
TreeNode found = null;
while ((found==null) && (i<num)) {
found = findNode(start.getChild(i),search);
i++;
}
return found;
}
}
}
public TreeNode findParent(TreeNode start,TreeNode search) {
if (start.equals(search))
return null;
if (start.isLeaf())
return null;
if (start.isChild(search))
return start;
int num = start.numberChilds();
int i=0;
TreeNode found = null;
while ((found==null) && (i<num)) {
found = findParent(start.getChild(i),search);
i++;
}
return found;
}
public void addNode(TreeNode where,TreeNode n) {
TreeNode pos = findNode(root,where);
if (pos==null)
System.out.println("Tree::addNode() Error - node not found");
else pos.addChild(n);
}
public void removeNode(TreeNode n) { // includes subtree removal !
// System.out.println("Tree::removeNode("+n+")");
if(root == n)
root.removeSubtree();
else
{
TreeNode par = findParent(root,n);
if (par!=null)
par.removeChild(n);
}
}
public void removeNode(TreeNode n,TreeTool t) { // includes subtree removal !
// System.out.println("Tree::removeNode("+n+",TreeTool)");
if(root.equals(n))
root.removeSubtree(t);
else
{
TreeNode par = findParent(root,n);
if (par!=null)
par.removeChild(n,t);
}
}
}