Module org.jhotdraw8.fxbase
Package org.jhotdraw8.fxbase.tree
Interface TreeNode<T extends TreeNode<T>>
- Type Parameters:
T- the type of the tree node
- All Known Implementing Classes:
SimpleTreeNode
public interface TreeNode<T extends TreeNode<T>>
Represents a node of a tree structure.
A node has zero or one parents, and zero or more children.
All nodes in the same tree structure are of the same type <T>.
A node may support only a restricted set of parent types <P extends T>.
A node may only support a restricted set of child types <C extends T>.
Usage:
public class MyTree implements TreeNode<MyTree> {
private @Nullable MyTree parent;
private ChildList<MyTree> children=new ChildList<>(this);
@Override
public @Nullable MyTree getParent() { return parent; }
@Override
public void setParent(@Nullable MyTree p) { this.parent = p; }
@Override
public ObservableList<MyTree> getChildren() { return children; }
}
- Author:
- Werner Randelshofer
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classTreeNode.AncestorIterator<T extends TreeNode<T>>Ancestor iterator. -
Method Summary
Modifier and TypeMethodDescriptionReturns an iterable which can iterate through this figure and all its ancesters up to the root.Returns an iterable which can iterate through this figure and all its descendants in breadth first sequence.Returns an iterable which can iterate through this figure and all its descendants in depth first sequence.default voiddumpTree()Dumps the figure and its descendants to system.out.default voiddumpTree(Appendable out, int depth) Dumps the figure and its descendants.default <TT> @Nullable TTgetAncestor(Class<TT> ancestorType) Returns the nearest ancestor of the specified type.default TgetChild(int index) Gets the child with the specified index from the node.Returns the children of the tree node.default intgetDepth()Returns the depth of this node.default @Nullable TGets the first child.default @Nullable TGets the last child.default intGets the maximal depth of the sub-tree starting at this tree node.@Nullable TReturns the parent of the tree node.getPath()Returns the path to this node.default booleanisSuitableChild(T newChild) This method returns whether the provided node is a suitable child for this node.default booleanisSuitableParent(T newParent) This method returns whether the provided node is a suitable parent for this node.Returns an iterable which can iterate through this figure and all its descendants in postorder sequence.Returns an iterable which can iterate through this figure and all its descendants in preorder sequence.default Spliterator<T> Returns a spliterator which can iterate through this figure and all its descendants in preorder sequence.voidSets the parent of the tree.
-
Method Details
-
ancestorIterable
Returns an iterable which can iterate through this figure and all its ancesters up to the root.- Returns:
- the iterable
-
breadthFirstIterable
Returns an iterable which can iterate through this figure and all its descendants in breadth first sequence.- Returns:
- the iterable
-
dumpTree
default void dumpTree()Dumps the figure and its descendants to system.out. -
dumpTree
Dumps the figure and its descendants.- Parameters:
out- an output streamdepth- the indentation depth- Throws:
IOException- from appendable
-
getAncestor
Returns the nearest ancestor of the specified type.- Type Parameters:
TT- The ancestor type- Parameters:
ancestorType- The ancestor type- Returns:
- Nearest ancestor of type <T> or null if no ancestor of
this type is present. Returns
thisif this object is of type <T>.
-
getChild
Gets the child with the specified index from the node.- Parameters:
index- the index- Returns:
- the child
-
getChildren
Returns the children of the tree node.In order to keep the tree structure consistent, implementations of this interface must implement the following behavior:
- A child can only be added if the child is a suitable child for
this node, and this node is a suitable parent for the child.
See
isSuitableChild(TreeNode),isSuitableParent(TreeNode). - If a child is added to this list, then this tree node removes it from its former parent, and this tree node then sets itself as the parent of the* child.
- If a child is removed from this tree node, then this tree node sets the parent of the child to null.
- Returns:
- the children
- A child can only be added if the child is a suitable child for
this node, and this node is a suitable parent for the child.
See
-
getFirstChild
Gets the first child.- Returns:
- The first child. Returns null if the figure has no getChildren.
-
getLastChild
Gets the last child.- Returns:
- The last child. Returns null if the figure has no getChildren.
-
getParent
@Nullable T getParent()Returns the parent of the tree node.Note that - by convention - the parent property is changed only by a parent tree node.
- Returns:
- the parent. Returns null if the tree node has no parent.
-
setParent
Sets the parent of the tree.Note that - by convention - the parent property is changed only by a parent tree node.
- Parameters:
newValue- the new parent
-
getPath
Returns the path to this node.- Returns:
- path including this node
-
getDepth
default int getDepth()Returns the depth of this node.- Returns:
- depth (0 if the node is the root)
-
postorderIterable
Returns an iterable which can iterate through this figure and all its descendants in postorder sequence.- Returns:
- the iterable
-
depthFirstIterable
Returns an iterable which can iterate through this figure and all its descendants in depth first sequence.- Returns:
- the iterable
-
preorderIterable
Returns an iterable which can iterate through this figure and all its descendants in preorder sequence.- Returns:
- the iterable
-
preorderSpliterator
Returns a spliterator which can iterate through this figure and all its descendants in preorder sequence.- Returns:
- the iterable
-
getMaxDepth
default int getMaxDepth()Gets the maximal depth of the sub-tree starting at this tree node.- Returns:
- the maximal depth
-
isSuitableParent
This method returns whether the provided node is a suitable parent for this node.The default implementation returns always true.
- Parameters:
newParent- The new parent node.- Returns:
- true if
newParentis an acceptable parent
-
isSuitableChild
This method returns whether the provided node is a suitable child for this node.The default implementation returns always true.
- Parameters:
newChild- The new child node.- Returns:
- true if
newChildis an acceptable child
-