public class TreeNode
extends java.lang.Object
null
parent.
Modifier and Type | Field and Description |
---|---|
private java.util.LinkedList<TreeNode> |
children
List of children, in order of insertion.
|
private TreeNode |
parent
Parent node to this node
|
Constructor and Description |
---|
TreeNode()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addChild(TreeNode child)
Adds a node to the end of the list of children.
|
private void |
addChildrenToList(java.util.LinkedList<TreeNode> list)
Recursively walk the list of children and add each child to the list
in a depth-first traversal.
|
java.util.Iterator<? extends TreeNode> |
childIterator()
Return an iterator to walk ONLY the immediate children of this node.
|
java.util.Iterator<? extends TreeNode> |
descendantsIterator()
Return an iterator to walk this node's children recursively in a
depth-first traversal.
|
int |
getChildDepth()
Get the level of nesting of the farmost child node from this node, where
0 indicates this node, and a positive number indicates
generations from this node. |
private int |
getChildDepth(TreeNode node)
Get the level of nesting of the farmost child node from this node, where
0 indicates this node, and a positive number indicates
generations from this node. |
int |
getDepth()
Get the level of nesting of this node from the root, where
0 indicates the root, and a positive number indicates
generations from the root. |
TreeNode |
getFirstChild()
Access the first child in the list of children.
|
TreeNode |
getNextSibling()
Access the next sibling (the child to the right of this node in the
parent's list of children).
|
int |
getNumChildren()
Get the number of immediate child nodes.
|
TreeNode |
getParent()
Get this nodes's parent, if any.
|
TreeNode[] |
getPath()
Get the array of tree nodes which represents the path to this node from
the root of the tree.
|
TreeNode |
getPreviousSibling()
Access the previous sibling (the child to the left of this node in the
parent's list of children).
|
private TreeNode |
getRelativeSibling(int relativeIndex)
Return the sibling at the index relative (positive or negative) to the
this node.
|
void |
setParent(TreeNode parent)
Sets this nodes's parent.
|
private TreeNode parent
private java.util.LinkedList<TreeNode> children
public void addChild(TreeNode child)
child
- The node to add.public int getDepth()
0
indicates the root, and a positive number indicates
generations from the root.public int getChildDepth()
0
indicates this node, and a positive number indicates
generations from this node.public TreeNode[] getPath()
The array always contains the root of the tree at index 0 and this node as the last item.
public int getNumChildren()
public TreeNode getFirstChild()
null
if there are no children in the list.public TreeNode getNextSibling()
null
if
there is no child to the right.public TreeNode getPreviousSibling()
null
if
there is no child to the left.public TreeNode getParent()
null
if this node represents
the root of a hierarchy.public void setParent(TreeNode parent)
parent
- The parent node of this node.public java.util.Iterator<? extends TreeNode> descendantsIterator()
public java.util.Iterator<? extends TreeNode> childIterator()
private void addChildrenToList(java.util.LinkedList<TreeNode> list)
list
- The list to which children need to be added.private TreeNode getRelativeSibling(int relativeIndex)
relativeIndex
- The positive or negative relative index position.null
if no such sibling exists.private int getChildDepth(TreeNode node)
0
indicates this node, and a positive number indicates
generations from this node.node
- node to start with