public class Node<T>
extends java.lang.Object
Tree<T> class. The Node<T> is also
a container, and can be thought of as instrumentation to determine the
location of the type T in the Tree<T>.
Ideas from:
http://sujitpal.blogspot.ca/2006/05/java-data-structure-generic-tree.html| Constructor and Description |
|---|
Node()
Default constructor.
|
Node(T data)
Convenience ctor to create a
Node<T> with an instance of T. |
| Modifier and Type | Method and Description |
|---|---|
void |
addChild(Node<T> child)
Adds a child to the list of children for this
Node<T>. |
void |
addChildren(java.util.List<Node<T>> newChildren)
Adds a list of children to this
Node<T>. |
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
|
boolean |
equalsNode(Node<T> node)
Value-equality of nodes and all children (i.e.
|
Node<T> |
getChild(int idx)
Gets a child at a particular index
|
java.util.List<Node<T>> |
getChildren()
Return the children of
Node<T>. |
T |
getData() |
int |
getNumberOfChildren()
Returns the number of immediate children of this
Node<T>. |
int |
getNumberOfParents()
Returns the number of immediate parents of this
Node<T>. |
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<T> at a particular
index |
void |
removeAllChildren()
Clears all children, ensuring to remove the parent entry
|
void |
removeChild(int index)
Remove the
Node<T> element at index index of the List<Node<T>>. |
void |
removeChild(Node<T> child)
Remove the
Node<T> element |
void |
setChildren(java.util.List<Node<T>> children)
Sets the children of a
Node<T> object. |
void |
setData(T data) |
java.lang.String |
toString() |
public Node()
public Node(T data)
Node<T> with an instance of T.data - an instance of T.public java.util.List<Node<T>> getChildren()
Node<T>. The Tree<T> is represented by a
single root Node<T> whose children are represented by a List<Node<T>>. Each of these Node<T> elements in the List can
have children. The getChildren() method will return the children of a
Node<T>.Node<T>public Node<T> getChild(int idx) throws java.lang.IndexOutOfBoundsException
idx - the index of the child to getjava.lang.IndexOutOfBoundsExceptionpublic void setChildren(java.util.List<Node<T>> children)
Node<T> object. See docs for getChildren() for more
information.children - the List<Node<T>> to set.public int getNumberOfChildren()
Node<T>.public int getNumberOfParents()
Node<T>.public void addChild(Node<T> child)
Node<T>. The addition of the
first child will create a new List<Node<T>>.child - a Node<T> object to set.public void insertChildAt(int index,
Node<T> child)
Node<T> at a particular
indexindex - the index at which to insert the childchild - a Node<T> object to set.public void addChildren(java.util.List<Node<T>> newChildren)
Node<T>.newChildren - list of Node<T> objects to addpublic void removeChild(int index)
throws java.lang.IndexOutOfBoundsException
Node<T> element at index index of the List<Node<T>>.index - the index of the element to delete.java.lang.IndexOutOfBoundsException - if thrown.public void removeChild(Node<T> child)
Node<T> elementchild - the node to removepublic void removeAllChildren()
public void detachFromParents()
public void detachFromParent(Node<T> parent)
parent - parent from which this node is to be removedpublic T getData()
public void setData(T data)
public java.lang.String toString()
toString in class java.lang.Objectpublic void clear()
public boolean equalsNode(Node<T> node)
node - node to compare withpublic void consolidate()