T - the node id typeA - the attachment biz object typepublic final class TreeNode<T extends Serializable & Comparable<T>,A> extends PlainNode<T,A>
Tree node structure ┌───────────────────────────┐ │ 0 │ │ ┌─────┴─────┐ │ │ 1 2 │ │ ┌───┴───┐ ┌───┴───┐ │ │ 3 4 5 6 │ │ ┌─┴─┐ ┌─┘ │ │ 7 8 9 │ └───────────────────────────┘ 上面这棵二叉树中的遍历方式: DFS前序遍历:0137849256 DFS中序遍历:7381940526 DFS后序遍历:7839415620 BFS广度优先:0123456789 CFS孩子优先:0123478956 (备注:教科书上没有CFS一说,是我为方便说明描述而取名的)
| 限定符和类型 | 字段和说明 |
|---|---|
protected int |
leftLeafCount |
protected int |
level |
protected int |
nodeDegree |
protected NodePath<T> |
path |
protected int |
siblingOrdinal |
protected int |
treeDegree |
protected int |
treeHeight |
protected int |
treeLeafCount |
protected int |
treeNodeCount |
| 构造器和说明 |
|---|
TreeNode(T id,
T parentId) |
TreeNode(T id,
T parentId,
boolean enabled,
boolean available,
A attach) |
| 限定符和类型 | 方法和说明 |
|---|---|
static <T extends Serializable & Comparable<T>,A,E extends PlainNode<T,A>> |
build(List<E> list) |
static <T extends Serializable & Comparable<T>,A,E extends PlainNode<T,A>> |
build(List<E> list,
boolean buildPath,
Comparator<? super TreeNode<T,A>> siblingNodesComparator)
Builds root tree node from node list
|
<E extends TreeTrait<T,A,E>> |
convert(Function<TreeNode<T,A>,E> converter,
boolean includeUnavailable) |
List<FlatNode<T,A>> |
flatBFS()
广度优先遍历BFS(Breath-First Search)
|
List<FlatNode<T,A>> |
flatCFS()
按层级方式展开节点:兄弟节点相邻
子节点优先搜索CFS(Children-First Search)
Should be invoking after
mount(List, boolean, boolean, Comparator)
Note:为了构建复杂表头,保证左侧的叶子节点必须排在右侧叶子节点前面,此处不能用广度优先搜索策略
|
List<FlatNode<T,A>> |
flatDFS()
深度优先搜索DFS(Depth-First Search):使用前序遍历
Should be invoking after
mount(List, boolean, boolean, Comparator) |
void |
forEachChild(Consumer<TreeNode<T,A>> childProcessor) |
TreeNode<T,A> |
getNode(T id)
Gets node by node id
|
<E extends PlainNode<T,A>> |
mount(List<E> list) |
<E extends PlainNode<T,A>> |
mount(List<E> list,
boolean ignoreIsolated,
boolean buildPath,
Comparator<? super TreeNode<T,A>> siblingNodesComparator)
Mount these nodes append to this tree
|
void |
print(Appendable output,
Function<TreeNode<T,A>,CharSequence> nodeLabel) |
TreeNode<T,A> |
removeNode(T id)
Remove node form tree
|
static <T extends Serializable & Comparable<T>,A> |
root(T id)
Creates single root tree node with id and parentId is null.
|
String |
toString() |
void |
traverse(Consumer<TreeNode<T,A>> action)
Traverses the tree, use BFS
{@code
Traverser.
|
protected NodePath<T extends Serializable & Comparable<T>> path
protected int level
protected int siblingOrdinal
protected int leftLeafCount
protected int nodeDegree
protected int treeDegree
protected int treeHeight
protected int treeNodeCount
protected int treeLeafCount
public static <T extends Serializable & Comparable<T>,A> TreeNode<T,A> root(T id)
T - the node id typeA - the attachment biz object typeid - the tree node idpublic static <T extends Serializable & Comparable<T>,A,E extends PlainNode<T,A>> TreeNode<T,A> build(List<E> list)
public static <T extends Serializable & Comparable<T>,A,E extends PlainNode<T,A>> TreeNode<T,A> build(List<E> list, boolean buildPath, Comparator<? super TreeNode<T,A>> siblingNodesComparator)
T - the node id typeA - the attachment biz object typeE - the type of list nodelist - the node listbuildPath - is whether build pathsiblingNodesComparator - the sibling nodes comparatorpublic <E extends PlainNode<T,A>> void mount(List<E> list, boolean ignoreIsolated, boolean buildPath, Comparator<? super TreeNode<T,A>> siblingNodesComparator)
list - 节点列表ignoreIsolated - 是否忽略孤立的节点buildPath - 是否构建节点路径(比较耗资源)siblingNodesComparator - 兄弟节点间的排序比较器public TreeNode<T,A> removeNode(T id)
id - the node idpublic List<FlatNode<T,A>> flatDFS()
Should be invoking after mount(List, boolean, boolean, Comparator)
public List<FlatNode<T,A>> flatCFS()
按层级方式展开节点:兄弟节点相邻
子节点优先搜索CFS(Children-First Search)
Should be invoking after mount(List, boolean, boolean, Comparator)
Note:为了构建复杂表头,保证左侧的叶子节点必须排在右侧叶子节点前面,此处不能用广度优先搜索策略
public void traverse(Consumer<TreeNode<T,A>> action)
Traverser.<File>forTree(f -> f.isDirectory() ? Arrays.asList(f.listFiles()) : Collections.emptyList())
.breadthFirst(new File("/xxx/path"))
.forEach(f -> System.out.println(f.getName()));
action - the action functionTraverserpublic <E extends TreeTrait<T,A,E>> E convert(Function<TreeNode<T,A>,E> converter, boolean includeUnavailable)
public void print(Appendable output, Function<TreeNode<T,A>,CharSequence> nodeLabel) throws IOException
IOExceptionCopyright © 2025. All rights reserved.