public final class FlatTreeNode<T> extends Object implements FlatTree<T,FlatTreeNode<T>>, Serializable
FlatTree
interface. Beside the
flattened and dense layout it is also an immutable implementation of
the Tree
interface. It can only be created from an existing tree.
final Tree<String, ?> immutable = FlatTreeNode.of(TreeNode.parse(...));
Modifier and Type | Method and Description |
---|---|
FlatTreeNode<T> |
childAt(int index)
Return the child node with the given index.
|
int |
childCount()
Return the number of children this tree node consists of.
|
int |
childOffset()
Return the index of the first child node in the underlying node array.
|
boolean |
equals(Object obj) |
ISeq<FlatTreeNode<T>> |
flattenedNodes()
Return the whole flattened tree values in breadth-first order.
|
Optional<FlatTreeNode<T>> |
getParent()
Return the parent node of this tree node.
|
FlatTreeNode<T> |
getRoot()
Returns the root of the tree that contains this node.
|
T |
getValue()
Return the value of the current
Tree node. |
int |
hashCode() |
boolean |
identical(Tree<?,?> other)
Tests whether
this node is the same as the other node. |
boolean |
isRoot()
Returns
true if this node is the root of the tree. |
<B> ISeq<B> |
map(Function<FlatTreeNode<T>,? extends B> mapper)
Return a sequence of all mapped nodes of the whole underlying
tree.
|
static <V> FlatTreeNode<V> |
of(Tree<? extends V,?> tree)
Create a new, immutable
FlatTreeNode from the given tree . |
static FlatTreeNode<String> |
parse(String tree)
Parses a (parentheses) tree string, created with
Tree.toParenthesesString() . |
static <B> FlatTreeNode<B> |
parse(String tree,
Function<? super String,? extends B> mapper)
Parses a (parentheses) tree string, created with
Tree.toParenthesesString() . |
int |
size()
Return the number of nodes of
this node (sub-tree). |
Stream<FlatTreeNode<T>> |
stream()
Return a stream of all nodes of the whole underlying tree.
|
String |
toString() |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
breadthFirstIterator, breadthFirstStream, childAfter, childAtPath, childAtPath, childBefore, childIterator, childPath, childStream, depth, depthFirstIterator, depthFirstStream, equals, firstChild, firstLeaf, getPath, hashCode, indexOf, isAncestor, isChild, isDescendant, isLeaf, isRelated, isSibling, iterator, lastChild, lastLeaf, leafCount, level, nextLeaf, nextNode, nextSibling, path, pathElements, pathFromAncestorIterator, postorderIterator, postorderStream, preorderIterator, preorderStream, previousLeaf, previousNode, previousSibling, sharedAncestor, siblingCount, toParenthesesString, toParenthesesString, toString
forEach, spliterator
public FlatTreeNode<T> getRoot()
getRoot
in interface Tree<T,FlatTreeNode<T>>
public boolean isRoot()
Tree
true
if this node is the root of the tree.isRoot
in interface Tree<T,FlatTreeNode<T>>
true
if this node is the root of its tree, false
otherwisepublic T getValue()
Tree
Tree
node. The value may be
null
.getValue
in interface Tree<T,FlatTreeNode<T>>
Tree
nodepublic Optional<FlatTreeNode<T>> getParent()
Tree
getParent
in interface Tree<T,FlatTreeNode<T>>
Optional.empty()
if this node is the
root of the treepublic FlatTreeNode<T> childAt(int index)
Tree
childAt
in interface Tree<T,FlatTreeNode<T>>
index
- the child indexpublic int childCount()
Tree
childCount
in interface Tree<T,FlatTreeNode<T>>
public int childOffset()
-1
is returned if this
node is a leaf.childOffset
in interface FlatTree<T,FlatTreeNode<T>>
-1
if this
node is a leafpublic ISeq<FlatTreeNode<T>> flattenedNodes()
FlatTree
final ISeq<T> seq = getRoot().breadthFirstStream()
.collect(ISeq.toISeq());
flattenedNodes
in interface FlatTree<T,FlatTreeNode<T>>
public Stream<FlatTreeNode<T>> stream()
final Stream<FlatTreeNode<T>> nodes = getRoot().breadthFirstStream();
stream
in interface Tree<T,FlatTreeNode<T>>
Tree.breadthFirstStream()
public <B> ISeq<B> map(Function<FlatTreeNode<T>,? extends B> mapper)
final ISeq<B> seq = stream()
.map(mapper)
.collect(ISeq.toISeq())
B
- the mapped typemapper
- the mapper functionpublic boolean identical(Tree<?,?> other)
Tree
this
node is the same as the other
node.
The default implementation returns the object identity,
this == other
, of the two objects, but other implementations may
use different criteria for checking the identity.identical
in interface Tree<T,FlatTreeNode<T>>
other
- the other
nodetrue
if the other
node is the same as this
node.public int size()
Tree
this
node (sub-tree).size
in interface Tree<T,FlatTreeNode<T>>
this
node (sub-tree)public static <V> FlatTreeNode<V> of(Tree<? extends V,?> tree)
FlatTreeNode
from the given tree
.V
- the tree value typestree
- the source treeFlatTreeNode
from the given tree
NullPointerException
- if the given tree
is null
public static FlatTreeNode<String> parse(String tree)
Tree.toParenthesesString()
. The tree string might look like this:
mul(div(cos(1.0),cos(π)),sin(mul(1.0,z)))
tree
- the parentheses tree stringNullPointerException
- if the given tree
string is
null
IllegalArgumentException
- if the given tree string could not be
parsedTree.toParenthesesString(Function)
,
Tree.toParenthesesString()
,
TreeNode.parse(String)
public static <B> FlatTreeNode<B> parse(String tree, Function<? super String,? extends B> mapper)
Tree.toParenthesesString()
. The tree string might look like this
0(1(4,5),2(6),3(7(10,11),8,9))and can be parsed to an integer tree with the following code:
final Tree<Integer, ?> tree = FlatTreeNode.parse(
"0(1(4,5),2(6),3(7(10,11),8,9))",
Integer::parseInt
);
B
- the tree node value typetree
- the parentheses tree stringmapper
- the mapper which converts the serialized string value to
the desired typeNullPointerException
- if one of the arguments is null
IllegalArgumentException
- if the given parentheses tree string
doesn't represent a valid treeTree.toParenthesesString(Function)
,
Tree.toParenthesesString()
,
TreeNode.parse(String, Function)
© 2007-2019 Franz Wilhelmstötter (2019-11-18 20:30)