maspack.graph
Class Node<T>

java.lang.Object
  extended by maspack.graph.Node<T>
Direct Known Subclasses:
BiNode

public class Node<T>
extends java.lang.Object

Represents a node of the Tree class. The Node is also a container, and can be thought of as instrumentation to determine the location of the type T in the Tree. Ideas from: http://sujitpal.blogspot.ca/2006/05/java-data-structure-generic-tree.html


Constructor Summary
Node()
          Default constructor.
Node(T data)
          Convenience ctor to create a Node with an instance of T.
 
Method Summary
 void addChild(Node<T> child)
          Adds a child to the list of children for this Node.
 void addChildren(java.util.List<Node<T>> newChildren)
          Adds a list of children to this Node.
 void clear()
          Removes the branch below this item, clearing children if this is the only parent
 void consolidate()
          merges children with equal content, reducing branches
 void detachFromParent(Node<T> parent)
          Removes child from a particular parent
 void detachFromParents()
          Removes node from all parents
 Node<T> getChild(int idx)
          Gets a child at a particular index
 java.util.List<Node<T>> getChildren()
          Return the children of Node.
 T getData()
           
 int getNumberOfChildren()
          Returns the number of immediate children of this Node.
 int getNumberOfParents()
          Returns the number of immediate parents of this Node.
 Node<T> getParent(int idx)
           
 java.util.List<Node<T>> getParents()
           
 void insertChildAt(int index, Node<T> child)
          Adds a child to the list of children for this Node at a particular index
 void removeAllChildren()
          Clears all children, ensuring to remove the parent entry
 void removeChild(int index)
          Remove the Node element at index index of the List>.
 void removeChild(Node<T> child)
          Remove the Node element
 void setChildren(java.util.List<Node<T>> children)
          Sets the children of a Node object.
 void setData(T data)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Node

public Node()
Default constructor.


Node

public Node(T data)
Convenience ctor to create a Node with an instance of T.

Parameters:
data - an instance of T.
Method Detail

getChildren

public java.util.List<Node<T>> getChildren()
Return the children of Node. The Tree is represented by a single root Node whose children are represented by a List>. Each of these Node elements in the List can have children. The getChildren() method will return the children of a Node.

Returns:
the children of Node

getChild

public Node<T> getChild(int idx)
                 throws java.lang.IndexOutOfBoundsException
Gets a child at a particular index

Parameters:
idx - the index of the child to get
Returns:
the child
Throws:
java.lang.IndexOutOfBoundsException

setChildren

public void setChildren(java.util.List<Node<T>> children)
Sets the children of a Node object. See docs for getChildren() for more information.

Parameters:
children - the List> to set.

getNumberOfChildren

public int getNumberOfChildren()
Returns the number of immediate children of this Node.

Returns:
the number of immediate children.

getNumberOfParents

public int getNumberOfParents()
Returns the number of immediate parents of this Node.

Returns:
the number of immediate parents.

addChild

public void addChild(Node<T> child)
Adds a child to the list of children for this Node. The addition of the first child will create a new List>.

Parameters:
child - a Node object to set.

insertChildAt

public void insertChildAt(int index,
                          Node<T> child)
Adds a child to the list of children for this Node at a particular index

Parameters:
index - the index at which to insert the child
child - a Node object to set.

getParents

public java.util.List<Node<T>> getParents()

getParent

public Node<T> getParent(int idx)

addChildren

public void addChildren(java.util.List<Node<T>> newChildren)
Adds a list of children to this Node.

Parameters:
newChildren - list of Node objects to add

removeChild

public void removeChild(int index)
                 throws java.lang.IndexOutOfBoundsException
Remove the Node element at index index of the List>.

Parameters:
index - the index of the element to delete.
Throws:
java.lang.IndexOutOfBoundsException - if thrown.

removeChild

public void removeChild(Node<T> child)
Remove the Node element

Parameters:
child - the node to remove

removeAllChildren

public void removeAllChildren()
Clears all children, ensuring to remove the parent entry


detachFromParents

public void detachFromParents()
Removes node from all parents


detachFromParent

public void detachFromParent(Node<T> parent)
Removes child from a particular parent

Parameters:
parent -

getData

public T getData()

setData

public void setData(T data)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

clear

public void clear()
Removes the branch below this item, clearing children if this is the only parent


consolidate

public void consolidate()
merges children with equal content, reducing branches