org.vizzini.util.tree
Class AbstractTreeNode

java.lang.Object
  extended by org.vizzini.util.tree.AbstractTreeNode
All Implemented Interfaces:
Cloneable, ITreeNode, IVisitable
Direct Known Subclasses:
AbstractNode, DefaultTreeNode

public abstract class AbstractTreeNode
extends Object
implements ITreeNode

Provides base functionality for a tree node.

Since:
v0.3
Version:
v0.3
Author:
Jeffrey M. Thompson

Constructor Summary
AbstractTreeNode()
          Construct this object.
AbstractTreeNode(Object userObject)
          Construct this object with the given parameter.
AbstractTreeNode(Object userObject, boolean isChildAllowed)
          Construct this object with the given parameter.
 
Method Summary
 void acceptBreadthFirst(IVisitor visitor)
          Accept the given visitor in a breadth first manner.
 void acceptDepthFirst(IVisitor visitor)
          Accept the given visitor in a depth first manner.
 void add(ITreeNode treeNode)
          Removes newChild from its parent and makes it a child of this node by adding it to the end of this node's child array.
 Iterator breadthFirstIterator()
          Creates and returns an enumeration that traverses the subtree rooted at this node in breadth-first order.
 Iterator children()
          Creates and returns a forward-order iterator of this node's children.
 Object clone()
          Return a clone of this instance.
protected  ITreeNodeCollection createTreeNodeCollection()
          Create a new tree node collection of the appropriate type.
 Iterator depthFirstIterator()
          Creates and returns an enumeration that traverses the subtree rooted at this node in depth-first order.
protected  String dotDesc()
          Return a dot description of this node.
protected  String dotLabel()
          Return a dot label of this node.
protected  String dotLinkDesc()
          Return a dot description of this node.
 boolean equals(Object object)
          Return true if the given object is equal to this.
protected  ITreeNodeCollection fillCollectionBreadthFirst(ITreeNodeCollection list)
          Fill the given collection with nodes in a breadth first manner.
protected  ITreeNodeCollection fillCollectionChildren(ITreeNodeCollection list)
          Fill the given collection with the children of this.
protected  ITreeNodeCollection fillCollectionDepthFirst(ITreeNodeCollection list)
          Fill the given collection with nodes in a depth first manner.
 ITreeNodeCollection getBreadthFirstList()
          Creates and returns a list containing the subtree rooted at this node in breadth-first order.
 ITreeNode getChildAt(int index)
          Returns the child at the specified index in this node's child array.
 int getChildCount()
          Returns the number of children of this node.
 ITreeNodeCollection getChildren()
          Return the tree node collection of children.
 int getDepth()
          Returns the depth of the tree rooted at this node -- the longest distance from this node to a leaf.
 ITreeNodeCollection getDepthFirstList()
          Creates and returns a list containing the subtree rooted at this node in depth-first order.
 ITreeNode getFirstChild()
          Returns this node's first child.
 ITreeNode getFirstLeaf()
          Finds and returns the first leaf that is a descendant of this node -- either this node or its first child's first leaf.
 int getIndex(ITreeNode treeNode)
          Returns the index of the specified child in this node's child array.
 ITreeNode getLastChild()
          Returns this node's last child.
 ITreeNode getLastLeaf()
          Finds and returns the last leaf that is a descendant of this node -- either this node or its last child's last leaf.
 int getLevel()
          Returns the number of levels above this node -- the distance from the root to this node.
 ITreeNode getNextSibling()
          Return the sibling after this.
 int getNodeCount()
          Return the count of all nodes in the subtree rooted at this node.
 ITreeNode getParent()
          Returns this node's parent or null if this node has no parent.
 ITreeNode[] getPath()
          Returns the path from the root, to get to this node.
 ITreeNode getRoot()
          Returns the root of the tree that contains this node.
static Class getTreeNodeCollectionClass()
           
 Object getUserObject()
          Returns this node's user object.
 int hashCode()
          Returns a hash code value for the object.
 void insert(ITreeNode treeNode, int index)
          Removes treeNode from its present parent (if it has a parent), sets the child's parent to this node, and then adds the child to this node's child array at index childIndex.
 boolean isChildAllowed()
          Returns true if this node is allowed to have children.
 boolean isLeaf()
          Returns true if this node has no children.
 boolean isRoot()
          Returns true if this node is the root of the tree.
 void remove(int index)
          Removes the child at the specified index from this node's children and sets that node's parent to null.
 void remove(ITreeNode treeNode)
          Removes treeNode from this node's child array, giving it a null parent.
 void removeAllChildren()
          Removes all of this node's children, setting their parents to null.
 void removeFromParent()
          Removes the subtree rooted at this node from the tree, giving this node a null parent.
 void setChildAllowed(boolean isChildAllowed)
          Determines whether or not this node is allowed to have children.
 void setChildren(ITreeNodeCollection children)
           
 void setParent(ITreeNode parent)
          Sets this node's parent to parent but does not change the parent's child array.
static void setTreeNodeCollectionClass(Class treeNodeCollectionClass)
           
 void setUserObject(Object userObject)
          Sets the user object for this node to userObject.
 String toString()
          Return a string representation of this object.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractTreeNode

public AbstractTreeNode()
Construct this object.

Since:
v0.3

AbstractTreeNode

public AbstractTreeNode(Object userObject)
Construct this object with the given parameter.

Parameters:
userObject - User object.
Since:
v0.3

AbstractTreeNode

public AbstractTreeNode(Object userObject,
                        boolean isChildAllowed)
Construct this object with the given parameter.

Parameters:
userObject - User object.
isChildAllowed - Flag indicating whether children are allowed.
Since:
v0.3
Method Detail

getTreeNodeCollectionClass

public static Class getTreeNodeCollectionClass()
Returns:
Return treeNodeCollectionClass.
Since:
v0.3

setTreeNodeCollectionClass

public static void setTreeNodeCollectionClass(Class treeNodeCollectionClass)
Parameters:
treeNodeCollectionClass - the treeNodeCollectionClass to set
Since:
v0.3

acceptBreadthFirst

public void acceptBreadthFirst(IVisitor visitor)
Accept the given visitor in a breadth first manner.

Specified by:
acceptBreadthFirst in interface IVisitable
Parameters:
visitor - Visitor.
Since:
v0.3

acceptDepthFirst

public void acceptDepthFirst(IVisitor visitor)
Accept the given visitor in a depth first manner.

Specified by:
acceptDepthFirst in interface IVisitable
Parameters:
visitor - Visitor.
Since:
v0.3

add

public void add(ITreeNode treeNode)
Removes newChild from its parent and makes it a child of this node by adding it to the end of this node's child array.

Specified by:
add in interface ITreeNode
Since:
v0.3

breadthFirstIterator

public Iterator breadthFirstIterator()
Creates and returns an enumeration that traverses the subtree rooted at this node in breadth-first order.

Specified by:
breadthFirstIterator in interface ITreeNode
Since:
v0.3

children

public Iterator children()
Creates and returns a forward-order iterator of this node's children.

Specified by:
children in interface ITreeNode
Since:
v0.3

clone

public Object clone()
Return a clone of this instance.

Specified by:
clone in interface ITreeNode
Overrides:
clone in class Object
Since:
v0.3

depthFirstIterator

public Iterator depthFirstIterator()
Creates and returns an enumeration that traverses the subtree rooted at this node in depth-first order.

Specified by:
depthFirstIterator in interface ITreeNode
Since:
v0.3

equals

public boolean equals(Object object)
Return true if the given object is equal to this.

Overrides:
equals in class Object
Parameters:
object - The object to compare.
Since:
v0.3

getBreadthFirstList

public ITreeNodeCollection getBreadthFirstList()
Creates and returns a list containing the subtree rooted at this node in breadth-first order.

Specified by:
getBreadthFirstList in interface ITreeNode
Since:
v0.3

getChildAt

public ITreeNode getChildAt(int index)
Returns the child at the specified index in this node's child array.

Specified by:
getChildAt in interface ITreeNode
Since:
v0.3

getChildCount

public int getChildCount()
Returns the number of children of this node.

Specified by:
getChildCount in interface ITreeNode
Since:
v0.3

getChildren

public ITreeNodeCollection getChildren()
Return the tree node collection of children.

Since:
v0.3

getDepth

public int getDepth()
Returns the depth of the tree rooted at this node -- the longest distance from this node to a leaf.

Specified by:
getDepth in interface ITreeNode
Since:
v0.3

getDepthFirstList

public ITreeNodeCollection getDepthFirstList()
Creates and returns a list containing the subtree rooted at this node in depth-first order.

Specified by:
getDepthFirstList in interface ITreeNode
Since:
v0.3

getFirstChild

public ITreeNode getFirstChild()
Returns this node's first child.

Specified by:
getFirstChild in interface ITreeNode
Since:
v0.3

getFirstLeaf

public ITreeNode getFirstLeaf()
Finds and returns the first leaf that is a descendant of this node -- either this node or its first child's first leaf.

Specified by:
getFirstLeaf in interface ITreeNode
Since:
v0.3

getIndex

public int getIndex(ITreeNode treeNode)
Returns the index of the specified child in this node's child array.

Specified by:
getIndex in interface ITreeNode
Since:
v0.3

getLastChild

public ITreeNode getLastChild()
Returns this node's last child.

Specified by:
getLastChild in interface ITreeNode
Since:
v0.3

getLastLeaf

public ITreeNode getLastLeaf()
Finds and returns the last leaf that is a descendant of this node -- either this node or its last child's last leaf.

Specified by:
getLastLeaf in interface ITreeNode
Since:
v0.3

getLevel

public int getLevel()
Returns the number of levels above this node -- the distance from the root to this node.

Specified by:
getLevel in interface ITreeNode
Since:
v0.3

getNextSibling

public ITreeNode getNextSibling()
Return the sibling after this.

Specified by:
getNextSibling in interface ITreeNode
Since:
v0.3

getNodeCount

public int getNodeCount()
Return the count of all nodes in the subtree rooted at this node.

Specified by:
getNodeCount in interface ITreeNode
Since:
v0.3

getParent

public ITreeNode getParent()
Returns this node's parent or null if this node has no parent.

Specified by:
getParent in interface ITreeNode
Since:
v0.3

getPath

public ITreeNode[] getPath()
Returns the path from the root, to get to this node.

Specified by:
getPath in interface ITreeNode
Since:
v0.3

getRoot

public ITreeNode getRoot()
Returns the root of the tree that contains this node.

Specified by:
getRoot in interface ITreeNode
Since:
v0.3

getUserObject

public Object getUserObject()
Description copied from interface: ITreeNode
Returns this node's user object.

Specified by:
getUserObject in interface ITreeNode
Returns:
Return userObject.
Since:
v0.3

hashCode

public int hashCode()
Returns a hash code value for the object.

Overrides:
hashCode in class Object
Since:
v0.3

insert

public void insert(ITreeNode treeNode,
                   int index)
Removes treeNode from its present parent (if it has a parent), sets the child's parent to this node, and then adds the child to this node's child array at index childIndex.

Specified by:
insert in interface ITreeNode
Since:
v0.3

isChildAllowed

public boolean isChildAllowed()
Returns true if this node is allowed to have children.

Specified by:
isChildAllowed in interface ITreeNode
Since:
v0.3

isLeaf

public boolean isLeaf()
Returns true if this node has no children.

Specified by:
isLeaf in interface ITreeNode
Since:
v0.3

isRoot

public boolean isRoot()
Returns true if this node is the root of the tree.

Specified by:
isRoot in interface ITreeNode
Since:
v0.3

remove

public void remove(int index)
Removes the child at the specified index from this node's children and sets that node's parent to null.

Specified by:
remove in interface ITreeNode
Since:
v0.3

remove

public void remove(ITreeNode treeNode)
Removes treeNode from this node's child array, giving it a null parent.

Specified by:
remove in interface ITreeNode
Since:
v0.3

removeAllChildren

public void removeAllChildren()
Removes all of this node's children, setting their parents to null.

Specified by:
removeAllChildren in interface ITreeNode
Since:
v0.3

removeFromParent

public void removeFromParent()
Removes the subtree rooted at this node from the tree, giving this node a null parent.

Specified by:
removeFromParent in interface ITreeNode
Since:
v0.3

setChildAllowed

public void setChildAllowed(boolean isChildAllowed)
Determines whether or not this node is allowed to have children.

Specified by:
setChildAllowed in interface ITreeNode
Since:
v0.3

setChildren

public void setChildren(ITreeNodeCollection children)
Parameters:
children - the children to set

setParent

public void setParent(ITreeNode parent)
Sets this node's parent to parent but does not change the parent's child array.

Specified by:
setParent in interface ITreeNode
Since:
v0.3

setUserObject

public void setUserObject(Object userObject)
Description copied from interface: ITreeNode
Sets the user object for this node to userObject.

Specified by:
setUserObject in interface ITreeNode
Parameters:
userObject - the userObject to set
Since:
v0.3

toString

public String toString()
Return a string representation of this object.

Specified by:
toString in interface ITreeNode
Overrides:
toString in class Object
Since:
v0.3

createTreeNodeCollection

protected ITreeNodeCollection createTreeNodeCollection()
Create a new tree node collection of the appropriate type.

Since:
v0.3

dotDesc

protected String dotDesc()
Return a dot description of this node.

Since:
v0.3

dotLabel

protected String dotLabel()
Return a dot label of this node.

Since:
v0.3

dotLinkDesc

protected String dotLinkDesc()
Return a dot description of this node.

Since:
v0.3

fillCollectionBreadthFirst

protected ITreeNodeCollection fillCollectionBreadthFirst(ITreeNodeCollection list)
Fill the given collection with nodes in a breadth first manner.

Parameters:
list - Collection to fill.
Since:
v0.3

fillCollectionChildren

protected ITreeNodeCollection fillCollectionChildren(ITreeNodeCollection list)
Fill the given collection with the children of this.

Parameters:
list - Collection to fill.
Since:
v0.3

fillCollectionDepthFirst

protected ITreeNodeCollection fillCollectionDepthFirst(ITreeNodeCollection list)
Fill the given collection with nodes in a depth first manner.

Parameters:
list - Collection to fill.
Since:
v0.3


Copyright 2007 Vizzini.org. All Rights Reserved. 2007.12.25.03.00.02