美文网首页
Java List转树形状结构,过滤某些节点

Java List转树形状结构,过滤某些节点

作者: NoException | 来源:发表于2024-07-29 09:06 被阅读0次

一段AI生成的代码,供参考+思考

有一个java对象列表,里面有code和pcode两个字段,pcode代表上级code,需要将这个列表转换成树形结构,并且去除没有选中的分支


我的需求

有一个java对象列表,里面有code和pcode两个字段,pcode代表上级code,需要将这个列表转换成树形结构

AI回答

public class TreeNode {
    private String code;
    private String pcode;
    private List<TreeNode> children;

    public TreeNode(String code, String pcode) {
        this.code = code;
        this.pcode = pcode;
        this.children = new ArrayList<>();
    }

    // getters and setters
    public String getCode() { return code; }
    public void setCode(String code) { this.code = code; }
    public String getPcode() { return pcode; }
    public void setPcode(String pcode) { this.pcode = pcode; }
    public List<TreeNode> getChildren() { return children; }
    public void setChildren(List<TreeNode> children) { this.children = children; }
    public void addChild(TreeNode child) { children.add(child); }
}

接下来,你需要一个方法来构建树。这个方法会遍历整个列表,并根据 pcode 将每个节点添加到它的父节点下面。以下是实现该功能的方法:

public class TreeBuilder {

    public static TreeNode buildTree(List<TreeNode> nodes) {
        Map<String, TreeNode> nodeMap = new HashMap<>();
        TreeNode root = null;

        for (TreeNode node : nodes) {
            nodeMap.put(node.getCode(), node);
            if (node.getPcode().equals("root")) { // 假设根节点的 pcode 为 "root"
                root = node;
            } else {
                TreeNode parentNode = nodeMap.get(node.getPcode());
                if (parentNode != null) {
                    parentNode.addChild(node);
                } else {
                    System.out.println("Warning: Parent not found for node with code " + node.getCode());
                }
            }
        }

        return root;
    }
}

在这个例子中,我们首先创建了一个 HashMap 来存储所有的节点,键是节点的 code,值是节点本身。然后,对于列表中的每一个节点,我们检查它的 pcode 并在 nodeMap 中查找其父节点。如果找到父节点,则将当前节点添加到父节点的 children 列表中。如果找不到父节点(这通常意味着父节点还没有被处理),则打印警告信息。

请注意,这个例子假设根节点的 pcode 是 "root"。你需要根据实际情况调整这个条件。

如果你需要一个完整的示例代码,请告诉我,我可以为你提供一个从列表构建树的完整示例。


继续追问

如果这个列表中还有一个字段check,标记是否被选中,这个树中叶子结点没有被选中的话,此分支需要去掉

AI回答

在这种情况下,我们需要对构建好的树进行修剪,移除所有未被选中的叶子节点及其未被选中的父节点分支。为了实现这一点,我们可以添加一个新的方法来遍历树并移除未选中的叶子节点以及它们的未选中父节点。这里假设 TreeNode 类已经扩展以包含一个布尔类型的 check 字段,表示该节点是否被选中。

首先,让我们更新 TreeNode 类的定义:

public class TreeNode {
    private String code;
    private String pcode;
    private boolean check;
    private List<TreeNode> children;

    public TreeNode(String code, String pcode, boolean check) {
        this.code = code;
        this.pcode = pcode;
        this.check = check;
        this.children = new ArrayList<>();
    }

    // Getters and setters
    public String getCode() { return code; }
    public void setCode(String code) { this.code = code; }
    public String getPcode() { return pcode; }
    public void setPcode(String pcode) { this.pcode = pcode; }
    public boolean isCheck() { return check; }
    public void setCheck(boolean check) { this.check = check; }
    public List<TreeNode> getChildren() { return children; }
    public void setChildren(List<TreeNode> children) { this.children = children; }
    public void addChild(TreeNode child) { children.add(child); }
}

接下来,我们需要添加一个方法来修剪树。这个方法应该递归地检查每个节点及其子节点,并移除所有未选中的叶子节点以及它们未选中的父节点。这里是一个实现该功能的方法:

public class TreeBuilder {

    public static TreeNode buildTree(List<TreeNode> nodes) {
        Map<String, TreeNode> nodeMap = new HashMap<>();
        TreeNode root = null;

        for (TreeNode node : nodes) {
            nodeMap.put(node.getCode(), node);
            if (node.getPcode().equals("root")) {
                root = node;
            } else {
                TreeNode parentNode = nodeMap.get(node.getPcode());
                if (parentNode != null) {
                    parentNode.addChild(node);
                } else {
                    System.out.println("Warning: Parent not found for node with code " + node.getCode());
                }
            }
        }

        pruneUnselectedNodes(root);

        return root;
    }

    private static void pruneUnselectedNodes(TreeNode node) {
        if (node == null) {
            return;
        }

        // Prune the subtree
        Iterator<TreeNode> iterator = node.getChildren().iterator();
        while (iterator.hasNext()) {
            TreeNode child = iterator.next();
            pruneUnselectedNodes(child);

            // If the child is unselected and has no selected children, remove it.
            if (!child.isCheck() && child.getChildren().isEmpty()) {
                iterator.remove();
            }
        }

        // If the node has no selected children and is unselected, mark it as removable.
        if (!node.isCheck() && node.getChildren().isEmpty()) {
            node.setCheck(false);
        }
    }
}

在这个实现中,我们添加了一个名为 pruneUnselectedNodes 的私有方法,它递归地遍历树,并在适当的情况下移除未选中的叶子节点。如果一个节点没有被选中,并且它也没有任何被选中的子节点,那么该节点会被标记为未选中或直接从树中移除。

请注意,如果一个节点没有被选中并且它的所有子节点也都没有被选中,那么这个节点也会被标记为未选中。这意味着如果根节点没有被选中并且它所有的子节点都没有被选中,那么整棵树将被标记为未选中。

如果你需要一个完整的示例代码,包括如何初始化数据列表,请告诉我,我可以为你提供一个完整的示例。


这里可以看到AI提示我可以产生完整的例子,这当然是个大福利,直接CV就能搞定需求,接下来是AI生成的完整代码


AI回答

当然可以。以下是一个完整的示例代码,包括如何初始化数据列表、构建树以及修剪未选中的节点:

import java.util.*;

public class TreeBuilderExample {

    public static void main(String[] args) {
        List<TreeNode> nodes = new ArrayList<>();
        nodes.add(new TreeNode("1", "root", true));
        nodes.add(new TreeNode("2", "1", true));
        nodes.add(new TreeNode("3", "1", false));
        nodes.add(new TreeNode("4", "2", false));
        nodes.add(new TreeNode("5", "2", true));
        nodes.add(new TreeNode("6", "5", false));
        nodes.add(new TreeNode("7", "5", true));
        nodes.add(new TreeNode("8", "7", false));

        TreeNode root = TreeBuilder.buildTree(nodes);
        
        // 打印结果
        printTree(root, "");
    }

    private static void printTree(TreeNode node, String indent) {
        if (node == null) {
            return;
        }

        System.out.println(indent + node.getCode() + " (Selected: " + node.isCheck() + ")");
        for (TreeNode child : node.getChildren()) {
            printTree(child, indent + "  ");
        }
    }
}

class TreeNode {
    private String code;
    private String pcode;
    private boolean check;
    private List<TreeNode> children;

    public TreeNode(String code, String pcode, boolean check) {
        this.code = code;
        this.pcode = pcode;
        this.check = check;
        this.children = new ArrayList<>();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getPcode() {
        return pcode;
    }

    public void setPcode(String pcode) {
        this.pcode = pcode;
    }

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }

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

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    public void addChild(TreeNode child) {
        children.add(child);
    }
}

class TreeBuilder {

    public static TreeNode buildTree(List<TreeNode> nodes) {
        Map<String, TreeNode> nodeMap = new HashMap<>();
        TreeNode root = null;

        for (TreeNode node : nodes) {
            nodeMap.put(node.getCode(), node);
            if (node.getPcode().equals("root")) {
                root = node;
            } else {
                TreeNode parentNode = nodeMap.get(node.getPcode());
                if (parentNode != null) {
                    parentNode.addChild(node);
                } else {
                    System.out.println("Warning: Parent not found for node with code " + node.getCode());
                }
            }
        }

        pruneUnselectedNodes(root);

        return root;
    }

    private static void pruneUnselectedNodes(TreeNode node) {
        if (node == null) {
            return;
        }

        // Prune the subtree
        Iterator<TreeNode> iterator = node.getChildren().iterator();
        while (iterator.hasNext()) {
            TreeNode child = iterator.next();
            pruneUnselectedNodes(child);

            // If the child is unselected and has no selected children, remove it.
            if (!child.isCheck() && child.getChildren().isEmpty()) {
                iterator.remove();
            }
        }

        // If the node has no selected children and is unselected, mark it as removable.
        if (!node.isCheck() && node.getChildren().isEmpty()) {
            node.setCheck(false);
        }
    }
}

还贴心的来了一个提示,必须给赞。

1.初始化:

我们创建了一个 nodes 列表,并向其中添加了一些 TreeNode 实例。
每个节点都有一个 code(标识符)、一个 pcode(父节点的标识符)以及一个 check 标志(表示是否被选中)。

2.构建树:

使用 buildTree 方法构建树结构。这个方法创建了一个 nodeMap 用于存储所有节点,并通过 pcode 将子节点与父节点关联起来。
在构建完成后,调用 pruneUnselectedNodes 方法来移除未选中的节点。

3.修剪未选中的节点:

pruneUnselectedNodes 方法递归地遍历树,并移除那些未选中的叶子节点。
如果一个节点未被选中并且没有选中的子节点,它会被从树中移除。

4.打印结果:

最后,我们定义了一个 printTree 方法来打印出树的结构。这可以帮助我们验证树是否正确构建。

你可以运行这段代码来看看最终构建出来的树是什么样的。如果需要进一步的帮助或修改,请随时告知。

相关文章

网友评论

      本文标题:Java List转树形状结构,过滤某些节点

      本文链接:https://www.haomeiwen.com/subject/xztihjtx.html