org.rosuda.util
Class Node

java.lang.Object
  extended by org.rosuda.util.Node
Direct Known Subclasses:
SNode

public class Node
extends java.lang.Object

Implements the tree structure consisting of cascaded (unmarked) nodes. Each instance of Node represents a node.

Version:
$Id: Node.java 454 2003-07-30 23:03:55Z starsoft $

Field Summary
protected  java.util.Vector ch
          List of all child nodes (all of object type Node)
protected  int height
          Height of the tree downwards (i.e. assuming this node is the root).
protected  int level
          Depth (level) of this node.
protected  Node par
          Parent node
 
Constructor Summary
Node()
          constructs an empty, lonely node
Node(Node t)
          constructs a new root node as the parent of a subtree
 
Method Summary
 void add(Node n)
          adds a subtree as a child
 Node at(int pos)
          returns the subnode (child) at specified index
 java.util.Enumeration children()
          returns children enumeration
 int count()
          returns # of children
 void getAllNodes(java.util.Vector dstv)
          fills specified vector with all nodes contained in the tree (from this node downward, including) in prefix order (i.e. this node is stored as first)
 Node[] getChildren()
          returns a list of all children
 int getHeight()
          returns the height of the tree from this node downwards
 int getLevel()
          returns the level of this node (i.e. the distance from the root)
 void getNodesAtLevel(int dest, java.util.Vector nodes)
          fills provided vector with all nodes at specified level (works downwards only, so makes sense basically only if used with roots) *experimental*
 int getNumNodes(boolean leavesOnly)
          returns # of nodes in the tree from this node downwards (incl. current one).
 Node getParent()
          returns parent node
 Node getRoot()
          returns root if the tree
 boolean isLeaf()
          checks whether this node is a leaf
 boolean isRoot()
          checks whether this node is a root
 void prune()
          prune this node from its parent.
 void rebuildHeight()
          rebuilds tree height cache for this node and upwards (is called internally after updates.
 void rebuildLevels()
          rebuilds levels cache for this node and all subnodes (is called internally after updates.
 void remove(Node l)
          remove a node from current children.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ch

protected java.util.Vector ch
List of all child nodes (all of object type Node)


par

protected Node par
Parent node


level

protected int level
Depth (level) of this node. Root has a depth of 0 (cached value)


height

protected int height
Height of the tree downwards (i.e. assuming this node is the root). A single node has the height of 0

Constructor Detail

Node

public Node()
constructs an empty, lonely node


Node

public Node(Node t)
constructs a new root node as the parent of a subtree

Parameters:
t - subtree
Method Detail

isLeaf

public boolean isLeaf()
checks whether this node is a leaf

Returns:
true if this node is a leaf (i.e. has no children)

isRoot

public boolean isRoot()
checks whether this node is a root

Returns:
true if this node is a root (i.e. has no parent)

getChildren

public Node[] getChildren()
returns a list of all children

Returns:
an array of all children (as Node objects or null if this node is a leaf

count

public int count()
returns # of children

Returns:
# of children (or 0 if this node is a leaf)

at

public Node at(int pos)
returns the subnode (child) at specified index

Parameters:
pos - index of the subnode (child). First child is at index 0, last at count()-1. Returns null if range is invalid or node has no children.
Returns:
subnode as Node object

add

public void add(Node n)
adds a subtree as a child

Parameters:
n - node (root of the subtree) to be added

prune

public void prune()
prune this node from its parent. As a result this node becomes a root and the parent loses one child. The method does nothing if this node is already a root.


remove

public void remove(Node l)
remove a node from current children.

Parameters:
l - child to be removed

children

public java.util.Enumeration children()
returns children enumeration

Returns:
Enumeration of children of null if leaf.

getParent

public Node getParent()
returns parent node

Returns:
paret node (or null if this node is a root)

getNodesAtLevel

public void getNodesAtLevel(int dest,
                            java.util.Vector nodes)
fills provided vector with all nodes at specified level (works downwards only, so makes sense basically only if used with roots) *experimental*

Parameters:
dest - level of nodes to be added
nodes - Vector to be used for storage of the nodes (as Node objects). This method does no sanity checks, so ensure your vector is valid

getRoot

public Node getRoot()
returns root if the tree

Returns:
root of the tree (equals to this if the node is already the root)

getLevel

public int getLevel()
returns the level of this node (i.e. the distance from the root)

Returns:
level of this node (0 for root)

getHeight

public int getHeight()
returns the height of the tree from this node downwards

Returns:
tree height (0 for a leaf)

getAllNodes

public void getAllNodes(java.util.Vector dstv)
fills specified vector with all nodes contained in the tree (from this node downward, including) in prefix order (i.e. this node is stored as first)

Parameters:
dstv - destination vector. Objects of type Node are stored. It's not cleared implicitely. Expect a null-exception if you pass null instead of a valid vector.

getNumNodes

public int getNumNodes(boolean leavesOnly)
returns # of nodes in the tree from this node downwards (incl. current one).

Parameters:
leavesOnly - if set to true, only leaves are counted.
Returns:
# of nodes

rebuildLevels

public void rebuildLevels()
rebuilds levels cache for this node and all subnodes (is called internally after updates. There should be no need to call this method externally)


rebuildHeight

public void rebuildHeight()
rebuilds tree height cache for this node and upwards (is called internally after updates. There should be no need to call this method externally)