Class TreeNode<T>

java.lang.Object
cloud.opencode.base.tree.TreeNode<T>
Type Parameters:
T - the data type | 数据类型

public class TreeNode<T> extends Object
Tree Node 树节点

A generic tree node implementation.

通用树节点实现。

Features | 主要功能:

  • Parent-child relationships with back-references - 带反向引用的父子关系
  • Pre-order, post-order, breadth-first traversal - 前序、后序、广度优先遍历
  • Search, filter, and map operations - 搜索、过滤和映射操作
  • Ancestor, descendant, and sibling queries - 祖先、后代和兄弟查询
  • Depth, height, and size calculations - 深度、高度和大小计算

Usage Examples | 使用示例:

// Create tree - 创建树
TreeNode<String> root = new TreeNode<>("root");
TreeNode<String> child = root.addChild("child");

// Search - 搜索
Optional<TreeNode<String>> found = root.find(s -> s.equals("child"));

// Traverse - 遍历
root.forEachPreOrder(node -> System.out.println(node.getData()));

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: No (null data is allowed but not checked) - 空值安全: 否(允许null数据但不做检查)
Since:
JDK 25, opencode-base-tree V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • TreeNode

      public TreeNode(T data)
  • Method Details

    • getData

      public T getData()
    • setData

      public void setData(T data)
    • getParent

      public TreeNode<T> getParent()
    • getChildren

      public List<TreeNode<T>> getChildren()
    • addChild

      public TreeNode<T> addChild(T data)
    • addChild

      public TreeNode<T> addChild(TreeNode<T> child)
    • removeChild

      public boolean removeChild(TreeNode<T> child)
    • clearChildren

      public void clearChildren()
    • isRoot

      public boolean isRoot()
    • isLeaf

      public boolean isLeaf()
    • getChildCount

      public int getChildCount()
    • hasChildren

      public boolean hasChildren()
    • getDepth

      public int getDepth()
    • getHeight

      public int getHeight()
    • getRoot

      public TreeNode<T> getRoot()
    • getSiblings

      public List<TreeNode<T>> getSiblings()
    • getAncestors

      public List<TreeNode<T>> getAncestors()
    • getDescendants

      public List<TreeNode<T>> getDescendants()
    • getLeaves

      public List<TreeNode<T>> getLeaves()
    • find

      public Optional<TreeNode<T>> find(Predicate<T> predicate)
    • findAll

      public List<TreeNode<T>> findAll(Predicate<T> predicate)
    • forEachPreOrder

      public void forEachPreOrder(Consumer<TreeNode<T>> action)
    • forEachPostOrder

      public void forEachPostOrder(Consumer<TreeNode<T>> action)
    • forEachBreadthFirst

      public void forEachBreadthFirst(Consumer<TreeNode<T>> action)
    • map

      public <R> TreeNode<R> map(Function<T,R> mapper)
    • filter

      public TreeNode<T> filter(Predicate<T> predicate)
    • size

      public int size()
    • toString

      public String toString()
      Overrides:
      toString in class Object