com.arsdigita.util
Class Tree

java.lang.Object
  extended bycom.arsdigita.util.Tree

public class Tree
extends Object

This class a represents the tree abstraction. This implementation takes a recursive definition where a tree is a root node connected to other (sub)trees.

This implementation allows the same node to be used in more than position in the tree. For example, you can do something like this:

  Tree aa = new Tree("a");
  Tree bb = aa.addChild("b");
  bb.addChild("a");
  aa.addChild("c");
 

This can be visualized as follows:

    a
   / \
  /   \
 b     c
  \
   \
    a
 

The only ways to add node to the tree is through the constructor and the addChild(Object) addChild(Object, Object) methods.

Since:
2003-01-23
Version:
$Date: 2004/04/07 $
Author:
Vadim Nasardinov (vadimn@redhat.com)

Nested Class Summary
static class Tree.EdgeTreePair
          Nodes in a tree are connected with edges.
 
Constructor Summary
Tree(Object root)
           
 
Method Summary
 Tree addChild(Object child)
          A shortcut for addChild(child, null).
 Tree addChild(Object child, Object edge)
          Adds a child element to the root of this tree.
 void addSubtree(Tree subtree)
          A shortcut for addSubtree(subtree,null).
 void addSubtree(Tree subtree, Object edge)
          Adds subtree to the root node of this tree.
 Tree copy()
          Returns a copy of this tree.
 int depth()
          Returns the maximum depth of the tree.
 List getAncestors()
          Returns the list of trees such that each of the returned trees is rooted at the ancestor nodes of this tree or an empty list, if the root of this tree has no ancestors.
 String getLabel()
           
 Tree getParent()
          Returns the tree rooted at the parent node of the root of this tree or null, if the root of this tree has no parent node.
 Object getRoot()
          Returns the root of this tree.
 List getSubtrees()
          Returns the list of edge-tree pairs parented to the root node of this tree in the order in which they were initially added.
 int nodeCount()
           
 void setLabel(String label)
           
static List treesToNodes(List trees)
          Takes a list of trees and returns a new list where each tree from the passed in list is replaced with its root node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Tree

public Tree(Object root)
Parameters:
root - the root node of the tree
Method Detail

setLabel

public void setLabel(String label)

getLabel

public String getLabel()

getRoot

public Object getRoot()
Returns the root of this tree.


addChild

public Tree addChild(Object child,
                     Object edge)
Adds a child element to the root of this tree. Returns the subtree rooted at the newly created node.


addChild

public Tree addChild(Object child)
A shortcut for addChild(child, null).

See Also:
addChild(Object, Object)

addSubtree

public void addSubtree(Tree subtree,
                       Object edge)
Adds subtree to the root node of this tree.

Note: This doesn't check for cycles. If you do something like,

 tree.addSubtree(tree);
 

you're on your own. I'll add a check for cycles like this when I have the time. (This probably means never.)


addSubtree

public void addSubtree(Tree subtree)
A shortcut for addSubtree(subtree,null).

See Also:
addSubtree(Tree, Object)

getParent

public Tree getParent()
Returns the tree rooted at the parent node of the root of this tree or null, if the root of this tree has no parent node.


getSubtrees

public List getSubtrees()
Returns the list of edge-tree pairs parented to the root node of this tree in the order in which they were initially added. Manipulating the returned list does not affect this tree. For example, if you remove an element from the list, no changes are made to this tree.


copy

public Tree copy()
Returns a copy of this tree. The returned copy does not have a parent tree. In other words, the returned tree is no longer a part of a bigger tree, even if this tree was.


nodeCount

public int nodeCount()

depth

public int depth()
Returns the maximum depth of the tree.


getAncestors

public List getAncestors()
Returns the list of trees such that each of the returned trees is rooted at the ancestor nodes of this tree or an empty list, if the root of this tree has no ancestors. The closer ancestors appear first in the list.


treesToNodes

public static List treesToNodes(List trees)
Takes a list of trees and returns a new list where each tree from the passed in list is replaced with its root node.



Copyright (c) 2004 Red Hat, Inc. Corporation. All Rights Reserved. Generated at July 21 2004:2337 UTC