HOME

com.ireasoning.util
Class CTreeNode

java.lang.Object
  extended bycom.ireasoning.util.CTreeNode
All Implemented Interfaces:
Comparable, Serializable
Direct Known Subclasses:
MibTreeNode, com.ireasoning.util.OrderedTreeNode

public class CTreeNode
extends Object
implements Serializable, Comparable

This class is a tree data structure. It can be converted to a JTree node to be displayed in a UI program.

See Also:
Serialized Form

Constructor Summary
CTreeNode(Comparable name)
          Construtor for creating root node.
CTreeNode(Comparable name, CTreeNode root)
          Constructs a non-root node if passed root is not null.
 
Method Summary
 CTreeNode addChild(Comparable name)
          Not applicable, only supported in ordered tree node classes
 CTreeNode addChild(CTreeNode node)
          Not applicable, only supported in ordered tree node classes
 CTreeNode appendChild(Comparable name)
          Appends a new node with the passed name
 CTreeNode appendChild(CTreeNode newChild)
          Adds the node newChild to the end of the list of children of this node.
 CTreeNode appendChild(CTreeNode newChild, int mode)
          Adds the node newChild to the end of the list of children of this node.
 int compareTo(Comparable name)
          Compares this Object with the specified Object for order.
 int compareTo(CTreeNode node)
          Compares this Object with the specified Object for order.
 int compareTo(Object o)
          Compares this Object with the specified Object for order.
 CTreeNode copy()
          Deep copy of itself, including its subtree.
 boolean equals(Object obj)
          Checks if passed object is equal to this object
 CTreeNode getChildNode(int index)
          Returns child node at specified index.
 int getChildNodeCount()
          Returns the number of immidiate child nodes
 int getChildNodeIndex(CTreeNode node)
          Returns the passed child node's position.
 ArrayList getChildNodes()
          Returns all the immidiate child nodes
 CTreeNode getFirstChild()
          Returns the first immediate child of this node.
 CTreeNode getLastChild()
          Returns the last child of this node
static CTreeNode getLeftMost(CTreeNode start)
          Gets the left most child node (could be multiple levels below starting node) starting from the passed start.
 Comparable getName()
          Gets the name object
 CTreeNode getNextSibling()
          The node immediately following this node.
 CTreeNode getParent()
          Returns the parent node.
 CTreeNode getPrevSibling()
          Return the node immediately preceding this node.
 Comparable getRawName()
           
static CTreeNode getRightMost(CTreeNode start)
          Gets the right most child node (could be multiple levels below starting node) starting from the passed start.
 CTreeNode getRoot()
          Gets the root node
 int hashCode()
           
 CTreeNode insertBefore(CTreeNode newChild, CTreeNode refChild)
          Inserts the node newChild before the existing child node refChild .
 boolean isChildNode(CTreeNode child)
          Checks the passed child is one of this node 's immediate child nodes
 boolean isInSameTree(CTreeNode node)
          Checks if the passed node is in the same tree as this object
 boolean isLeaf()
          Returns true if this node has no child node
 boolean isRoot()
          Returns true if this object itself is a root node
 boolean removeChild(Comparable oldChildName)
          Removes the first encounter of its immediate child node with oldChildName (only remove 1 node).
 boolean removeChild(CTreeNode oldChild)
          Removes the immediate child node indicated by oldChild from the list of children
 boolean removeChild(int index)
          Removes the immediate child node at specified index
static boolean removeNode(CTreeNode node)
          Removes the passed node from its tree.
 CTreeNode replaceChild(CTreeNode oldChild, CTreeNode newChild)
          Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node.
 CTreeNode search(Comparable name)
          Searches for the node with passed name within this node's subtree, starting from itself.
 void setName(Comparable name)
          Sets a new name
 void setParent(CTreeNode parent)
          Sets parent node
 void setRoot(CTreeNode r)
          Sets a new root node
 DefaultMutableTreeNode toJTreeNode()
          Convert this node (including all subtrees) to JTree node, so it can be displayed in a JTree.
 String toString()
           
static void traverse(CTreeNode start, TraverseListener listener)
          Traverse the tree starting from the passed start node.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CTreeNode

public CTreeNode(Comparable name)
Construtor for creating root node.

Parameters:
name - an instance of a class that implements Comparable interface, usually represents the identity of the node.

CTreeNode

public CTreeNode(Comparable name,
                 CTreeNode root)
Constructs a non-root node if passed root is not null.

Parameters:
name - an instance of a class that implements Comparable interface, usually represents the identity of the node.
root - root node. If it's null, then this object become a new root node.
Method Detail

copy

public CTreeNode copy()
Deep copy of itself, including its subtree.

Returns:
a new instance of whole/sub tree

isRoot

public boolean isRoot()
Returns true if this object itself is a root node


getRoot

public CTreeNode getRoot()
Gets the root node


setRoot

public void setRoot(CTreeNode r)
Sets a new root node


getName

public Comparable getName()
Gets the name object


setName

public void setName(Comparable name)
Sets a new name


isLeaf

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


toString

public String toString()

getChildNode

public CTreeNode getChildNode(int index)
Returns child node at specified index. Or null if no node at that index


getChildNodeIndex

public int getChildNodeIndex(CTreeNode node)
Returns the passed child node's position. Or -1 if not found.


getChildNodes

public ArrayList getChildNodes()
Returns all the immidiate child nodes


getChildNodeCount

public int getChildNodeCount()
Returns the number of immidiate child nodes


getParent

public CTreeNode getParent()
Returns the parent node.


setParent

public void setParent(CTreeNode parent)
Sets parent node


getNextSibling

public CTreeNode getNextSibling()
The node immediately following this node. If there is no such node, this returns null


getPrevSibling

public CTreeNode getPrevSibling()
Return the node immediately preceding this node. If there is no such node, this returns null


getFirstChild

public CTreeNode getFirstChild()
Returns the first immediate child of this node. If there is no such node, this returns null


getLastChild

public CTreeNode getLastChild()
Returns the last child of this node


appendChild

public CTreeNode appendChild(Comparable name)
Appends a new node with the passed name

See Also:
appendChild(CTreeNode newChild)

appendChild

public CTreeNode appendChild(CTreeNode newChild)
Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed. And if the newChild is a tree itself, all its sub nodes will be preserved.

Parameters:
newChild - - The node to add.
Returns:
the child appended.

appendChild

public CTreeNode appendChild(CTreeNode newChild,
                             int mode)
Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed. And if the newChild is a tree itself, all its sub nodes will be preserved.

Parameters:
newChild - - The node to add.
mode - 0: append anyway; 1: no append if exists 2: merge and replace the existing child node.
Returns:
the child appended.

insertBefore

public CTreeNode insertBefore(CTreeNode newChild,
                              CTreeNode refChild)
Inserts the node newChild before the existing child node refChild . If refChild is null , insert newChild at the end of the list of children. If the newChild is already in the tree, it is first removed.

Parameters:
newChild - - node to insert.
refChild - - reference node, i.e., the node before which the new node must be inserted.
Returns:
null if newChild can't be inserted.

isChildNode

public boolean isChildNode(CTreeNode child)
Checks the passed child is one of this node 's immediate child nodes

Returns:
true if child is this class ' child node.

removeChild

public boolean removeChild(Comparable oldChildName)
Removes the first encounter of its immediate child node with oldChildName (only remove 1 node).


removeChild

public boolean removeChild(int index)
Removes the immediate child node at specified index


removeChild

public boolean removeChild(CTreeNode oldChild)
Removes the immediate child node indicated by oldChild from the list of children

Returns:
true if oldChild node is successfully removed.
See Also:
removeNode(CTreeNode node)

removeNode

public static boolean removeNode(CTreeNode node)
Removes the passed node from its tree. The difference from removeChild method is removeChild only can do the removal if the passed node is this object's child node.

See Also:
removeChild(CTreeNode oldChild)

replaceChild

public CTreeNode replaceChild(CTreeNode oldChild,
                              CTreeNode newChild)
Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node. If the newChild is already in the tree, it is first removed.

Parameters:
newChild - - The new node to put in the child list
oldChild - - The node being replaced in the list

compareTo

public int compareTo(Comparable name)
Compares this Object with the specified Object for order. Returns a negative integer, zero, or a positive integer as this Object is less than, equal to, or greater than the given Object.


compareTo

public int compareTo(CTreeNode node)
Compares this Object with the specified Object for order. Returns a negative integer, zero, or a positive integer as this Object is less than, equal to, or greater than the given Object.


compareTo

public int compareTo(Object o)
Compares this Object with the specified Object for order. Returns a negative integer, zero, or a positive integer as this Object is less than, equal to, or greater than the given Object.

Specified by:
compareTo in interface Comparable

equals

public boolean equals(Object obj)
Checks if passed object is equal to this object


hashCode

public int hashCode()

addChild

public CTreeNode addChild(Comparable name)
Not applicable, only supported in ordered tree node classes


addChild

public CTreeNode addChild(CTreeNode node)
Not applicable, only supported in ordered tree node classes


getRightMost

public static CTreeNode getRightMost(CTreeNode start)
Gets the right most child node (could be multiple levels below starting node) starting from the passed start.


getLeftMost

public static CTreeNode getLeftMost(CTreeNode start)
Gets the left most child node (could be multiple levels below starting node) starting from the passed start.


traverse

public static void traverse(CTreeNode start,
                            TraverseListener listener)
Traverse the tree starting from the passed start node.


isInSameTree

public boolean isInSameTree(CTreeNode node)
Checks if the passed node is in the same tree as this object

Returns:
true if two nodes are in the same tree.

toJTreeNode

public DefaultMutableTreeNode toJTreeNode()
Convert this node (including all subtrees) to JTree node, so it can be displayed in a JTree.
Note: The returned DefaultMutableTreeNode object does not have all the information contained in CTreeNode.


search

public CTreeNode search(Comparable name)
Searches for the node with passed name within this node's subtree, starting from itself.

Returns:
null if nothing found.

getRawName

public final Comparable getRawName()

HOME

Copyright © 2002 iReasoning Inc. All Rights Reserved.