.
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. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
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.
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 childchild
- 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