org.j4me.collections
Class TreeNode

java.lang.Object
  extended by org.j4me.collections.TreeNode

public class TreeNode
extends java.lang.Object

TreeNode objects can be combined into a tree. The tree is not a special tree such as a balanced tree. It can have any number of levels and each node can have any number of children.

Each tree node may have at most one parent and 0 or more children. TreeNode provides operations for examining and modifying a node's parent and children. A node's tree is the set of all nodes that can be reached by starting at the node and following all the possible links to parents and children. A node with no parent is the root of its tree; a node with no children is a leaf. A tree may consist of many subtrees, each node acting as the root for its own subtree.

Every TreeNode can hold a reference to one user object. It is up to the developer to decide what the object is and how to use it. For example in maintaining the golf course tree the user object is an ICourseElement.

This is not a thread safe class. If used from multiple threads it must be manually sychronized by your code.


Constructor Summary
TreeNode()
          Constructs a tree node object.
TreeNode(java.lang.Object userObject)
          Constructs a tree node object.
 
Method Summary
 void add(TreeNode child)
          Adds the child node to this container making this its parent.
 void add(TreeNode child, int index)
          Adds the child node to this container making this its parent.
 TreeNode[] children()
          Gets a list of all the child nodes of this node.
 int depth()
          Gets this node's depth in the tree.
 TreeNode getParent()
          Gets the parent node of this one.
 java.lang.Object getUserObject()
          Gets the user defined object attached to this node.
 boolean hasChildren()
          Returns if this node has children or if it is a leaf node.
 int index()
          Gets the position of this node in the list of siblings managed by the parent node.
 boolean isRoot()
          Returns if this node is the root node in the tree or not.
 TreeNode remove(int index)
          Removes the child at position index from the tree.
 void removeFromParent()
          Removes this node from its parent.
 void setUserObject(java.lang.Object userObject)
          Attaches a user defined object to this node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TreeNode

public TreeNode()
Constructs a tree node object. It can become the root of a tree. Or it can become a child of another node by calling the other node's add method.

There is no user object attached to the node. Call setUserObject to attach one.


TreeNode

public TreeNode(java.lang.Object userObject)
Constructs a tree node object. It can become the root of a tree. Or it can become a child of another node by calling the other node's add method.

Parameters:
userObject - is an object this node encapsulates. It is up to the developer to maintain its type. To get the object back out call getUserObject.
Method Detail

add

public void add(TreeNode child,
                int index)
Adds the child node to this container making this its parent.

Parameters:
child - is the node to add to the tree as a child of this
index - is the position within the children list to add the child. It must be between 0 (the first child) and the total number of current children (the last child). If it is negative the child will become the last child.

add

public void add(TreeNode child)
Adds the child node to this container making this its parent. The child is appended to the list of children as the last child.


remove

public TreeNode remove(int index)
Removes the child at position index from the tree.

Parameters:
index - is the position of the child. It should be between 0 (the first child) and the total number of children minus 1 (the last child).
Returns:
The removed child node. This will be null if no child exists at the specified index.

removeFromParent

public void removeFromParent()
Removes this node from its parent. This node becomes the root of a subtree where all of its children become first level nodes.

Calling this on the root node has no effect.


getParent

public TreeNode getParent()
Gets the parent node of this one.

Returns:
The parent of this node. This will return null if this node is the root node in the tree.

isRoot

public boolean isRoot()
Returns if this node is the root node in the tree or not.

Returns:
true if this node is the root of the tree; false if it has a parent.

children

public TreeNode[] children()
Gets a list of all the child nodes of this node.

Returns:
An array of all the child nodes. The array will be the size of the number of children. A leaf node will return an empty array, not null.

hasChildren

public boolean hasChildren()
Returns if this node has children or if it is a leaf node.

Returns:
true if this node has children; false if it does not have any children.

index

public int index()
Gets the position of this node in the list of siblings managed by the parent node. This node can be obtained by this = parent.children[this.index()].

Returns:
The index of the child array of this node's parent. If this is the root node it will return -1.

depth

public int depth()
Gets this node's depth in the tree. The root node will have a depth of 0, first-level nodes will have a depth of 1, and so on.

Returns:
The depth of this node in the tree.

setUserObject

public void setUserObject(java.lang.Object userObject)
Attaches a user defined object to this node. Only one object can be attached to a node.

Parameters:
userObject - is the programmer defined object to attach to this node in the tree. Set it to null to clear any objects.

getUserObject

public java.lang.Object getUserObject()
Gets the user defined object attached to this node. It must be cast back to what it was inserted as. It is up to the developer to make this cast.

Returns:
The programmer defined object attached to this node in the tree. Returns null if no object is attached.