tree-traversal - 如何在java中将树结构转换为节点流

标签 tree-traversal java-stream

我想在 Java8 节点流中转换树。
这是一个存储数据的节点树,可以选择:

public class SelectTree<D> {

  private D data;

  private boolean selected = false;

  private SelectTree<D> parent;

  private final List<SelectTree<D>> children = new ArrayList<>();

  public SelectTree(D data, SelectTree<D> parent) {
    this.data = data;
    if (parent != null) {
      this.parent = parent;
      this.parent.getChildren().add(this);
    }
  }

  public D getData() {
    return data;
  }

  public void setData(D data) {
    this.data = data;
  }

  public boolean isSelected() {
    return selected;
  }

  public void setSelected(boolean selected) {
    this.selected = selected;
  }

  public SelectTree<D> getParent() {
    return parent;
  }

  public void setParent(SelectTree<D> parent) {
    this.parent = parent;
  }

  public List<SelectTree<D>> getChildren() {
    return children;
  }

  public boolean isRoot() {
    return this.getParent() == null;
  }

  public boolean isLeaf() {
    return this.getChildren() == null || this.getChildren().isEmpty();
  }
}
我想获取所选数据的集合
我想做这样的事情:
  public static void main(String[] args) {
    SelectTree<Integer> root = generateTree();
    
    List<Integer> selectedData = root.stream()
            .peek(node -> System.out.println(node.getData()+": "+node.isSelected()))
            .filter(node-> node.isSelected())
            .map(node-> node.getData())
            .collect(Collectors.toList()) ;
    
    System.out.println("\nselectedData="+selectedData);
  }

  private static SelectTree<Integer> generateTree() {
    SelectTree<Integer> n1 = new SelectTree(1, null);
    SelectTree<Integer> n11 = new SelectTree(11, n1);
    SelectTree<Integer> n12 = new SelectTree(12, n1);
    n12.setSelected(true);
    SelectTree<Integer> n111 = new SelectTree(111, n11);
    n111.setSelected(true);
    SelectTree<Integer> n112 = new SelectTree(112, n11);
    SelectTree<Integer> n121 = new SelectTree(121, n12);
    SelectTree<Integer> n122 = new SelectTree(122, n12);
    return n1;
  }
问题是要找到stream()的实现我想我可以帮助一些人分享我的解决方案,我很想知道是否存在一些问题或更好的方法来做到这一点。
起初是为了素面TreeNode但我将问题概括为所有种类的树。

最佳答案

kwisatz 的一小部分补充的回答。

这个实现:

this.getChildren().stream()
        .map(SelectTree::stream)
        .reduce(Stream.of(this), Stream::concat);

会更渴望,我。 e.在流创建期间将遍历整个层次结构。如果您的层次结构很大,并且假设您正在寻找与某个谓词匹配的单个节点,则您可能需要更懒惰的行为:

Stream.concat(Stream.of(this),
              this.getChildren().stream().flatMap(SelectTree::stream));

在这种情况下,在流创建期间只会检索根节点的子节点,并且搜索节点不一定会导致遍历整个层次结构。

两种方法都将展示 DFS迭代顺序。

关于tree-traversal - 如何在java中将树结构转换为节点流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26158082/

相关文章:

jquery - 将多个 <p> 元素包裹到 <h3> 之间的 <div> 中

jquery - 有效地沿着上下 sibling 遍历

java - 使用常数空间和 O(n) 运行时间编写二叉搜索树的非递归遍历

java - 无法推断 hashmap<> 的类型参数

java - 方法 .toArray(IntFunction<A[]> Generator) 如何知道新数组的大小

design-patterns - 并行实现树遍历算法的策略?

algorithm - 给定树的二叉搜索树前、中、后顺序遍历

functional-programming - 默认 Stream<E> stream() vs static<T> Stream<T> of(T t)

Java 8 Streams 取字符串行的总和

java - 正则表达式查找字符串中的变量