以递归方式在节点点击时将树更新为从根节点到子节点的方式。

huangapple go评论82阅读模式
英文:

Update tree in root to child manner on node click with recursion

问题

我正在使用Angular + Spring Boot项目,其中我必须以分层方式显示数据的树状结构。

这是我的TreeNode类用于创建节点。
我们使用mat-tree创建树,目前整个树一次性创建,因此加载树需要很长时间。
所以我必须更改生成树的方式如下:

树示例:

  - 根
   - 子节点1
     - 子子节点1
       - a
         - a1
         - a2
       - b
     - 子子节点2
       - c
       - d
   - 子节点2
     - 子子节点3
       - f
       - j
     - 子子节点4

首先需要加载树的第一层:

  - 根
   - 子节点1
   - 子节点2

然后在点击子节点1后,我想在树中加载其子节点。节点的数据已被正确获取,但我需要维护一个全局变量来修改它,以便在执行节点点击时更新树。从Angular到Java发出加载树的请求,因此我需要维护一个树以进行更新。

我的问题是,随着树的深入,我需要编写许多循环。因此,我编写了一个递归函数,但它不能正常工作:

private TreeNode addChildren(TreeNode treeNodeOld, Long id) {
    for (TreeNode treeNode : treeNodeOld.getChildren()) {
        addChildren(treeNode, id);
        if (treeNode.getNodeId() == id) {
            return treeNode;
        }
        addChildren(treeNode, id);
    }
    return null;
}

这个方法不能正常工作。我想知道在哪里精确地放置子节点。该方法只适用于一级,但随着树的深入,它不起作用。我可以通过为每个级别编写循环来解决,但这会增加代码行数,并且许多时候级别也是未知的。

如何在根到子节点的方式中更新树,而不使用大量的代码,而是使用递归?

英文:

I am working with Angular+ spring boot project, in which I have to show tree structure of my data as the data in in hierarchical manner.

public class TreeNode {

    private long nodeId;
    private String nodePath;
    private String hierarchyLevel;
    private String nodeFullPath;
    private Integer pageNumber;
    private String label;
    private Long parentNodeId;
    private List<OutlineNode> children = new LinkedList<>();

}

This is my TreeNode class for creating a node.
We have used mat-tree to create tree, for now the whole tree is created at once, so it takes long time to load the tree.
So I have to change the manner to generate tree as :

Tree example :

  • root
    • child1
      • subchild1
        • a
          • a1
          • a2
        • b
      • subchild2
        • c
        • d
    • Child2
      • subchild3
        • f
        • j
      • subchild4

First need to load first level of tree :

  • root
    • child1
    • child2

Then on click of the child1, I want to load its children nodes in the tree. The data of the nodes is fetched correctly, but I need to maintain a global variable for the tree to modify it as click on the node is performed. The request to load the tree come from Angular to Java so I need to maintain a tree to update it.

My issue is that I need to write many for loops as the tree get deeper. So I have written a recursive function, but it not working properly :

private TreeNode addChildren(TreeNode treeNodeOld, Long id) {
            for (TreeNode treeNode : treeNodeOld.getChildren()) {
                addChildren(treeNode, id);
                if (treeNode.getNodeId() == id) {
                    return treeNode;
                }
                addChildren(treeNode, id);
            }
            return null;
        } 

This method not working properly. I want to know the exact position to put child on it. The method wok fine for one level only, but as the tree grow deeper this is not working. I can work by writing for loop for each level, but this will increase lines of code and many time the level also will be unknown.

How can I update the tree in root to child manner without number of code by using recursion?

答案1

得分: 0

将代码修改为类似以下内容

    private TreeNode addChildren(TreeNode treeNodeOld, Long id) {
        for (TreeNode treeNode : treeNodeOld.getChildren()) {
            if (treeNode.getNodeId() == id) {
                return treeNode;
            }
            TreeNode found = addChildren(treeNode, id);
            if (found != null) {
                return found;
            }
        }
        return null;
    }
英文:

Change to something like:

private TreeNode addChildren(TreeNode treeNodeOld, Long id) {
            for (TreeNode treeNode : treeNodeOld.getChildren()) {
                if (treeNode.getNodeId() == id) {
                    return treeNode;
                }
                TreeNode found = addChildren(treeNode, id);
                if (found != null) {
                    return found;
                }
            }
            return null;
        } 

huangapple
  • 本文由 发表于 2020年9月16日 20:47:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63920403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定