一段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); }
}
接下來,你需要一個方法來構建樹。這個方法會遍歷整個列表,并根據(jù) pcode 將每個節(jié)點添加到它的父節(jié)點下面。以下是實現(xiàn)該功能的方法:
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")) { // 假設根節(jié)點的 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;
}
}
在這個例子中,我們首先創(chuàng)建了一個 HashMap 來存儲所有的節(jié)點,鍵是節(jié)點的 code,值是節(jié)點本身。然后,對于列表中的每一個節(jié)點,我們檢查它的 pcode 并在 nodeMap 中查找其父節(jié)點。如果找到父節(jié)點,則將當前節(jié)點添加到父節(jié)點的 children 列表中。如果找不到父節(jié)點(這通常意味著父節(jié)點還沒有被處理),則打印警告信息。
請注意,這個例子假設根節(jié)點的 pcode 是 "root"。你需要根據(jù)實際情況調(diào)整這個條件。
如果你需要一個完整的示例代碼,請告訴我,我可以為你提供一個從列表構建樹的完整示例。
繼續(xù)追問
如果這個列表中還有一個字段check,標記是否被選中,這個樹中葉子結點沒有被選中的話,此分支需要去掉
AI回答
在這種情況下,我們需要對構建好的樹進行修剪,移除所有未被選中的葉子節(jié)點及其未被選中的父節(jié)點分支。為了實現(xiàn)這一點,我們可以添加一個新的方法來遍歷樹并移除未選中的葉子節(jié)點以及它們的未選中父節(jié)點。這里假設 TreeNode 類已經(jīng)擴展以包含一個布爾類型的 check 字段,表示該節(jié)點是否被選中。
首先,讓我們更新 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); }
}
接下來,我們需要添加一個方法來修剪樹。這個方法應該遞歸地檢查每個節(jié)點及其子節(jié)點,并移除所有未選中的葉子節(jié)點以及它們未選中的父節(jié)點。這里是一個實現(xiàn)該功能的方法:
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);
}
}
}
在這個實現(xiàn)中,我們添加了一個名為 pruneUnselectedNodes 的私有方法,它遞歸地遍歷樹,并在適當?shù)那闆r下移除未選中的葉子節(jié)點。如果一個節(jié)點沒有被選中,并且它也沒有任何被選中的子節(jié)點,那么該節(jié)點會被標記為未選中或直接從樹中移除。
請注意,如果一個節(jié)點沒有被選中并且它的所有子節(jié)點也都沒有被選中,那么這個節(jié)點也會被標記為未選中。這意味著如果根節(jié)點沒有被選中并且它所有的子節(jié)點都沒有被選中,那么整棵樹將被標記為未選中。
如果你需要一個完整的示例代碼,包括如何初始化數(shù)據(jù)列表,請告訴我,我可以為你提供一個完整的示例。
這里可以看到AI提示我可以產(chǎn)生完整的例子,這當然是個大福利,直接CV就能搞定需求,接下來是AI生成的完整代碼
AI回答
當然可以。以下是一個完整的示例代碼,包括如何初始化數(shù)據(jù)列表、構建樹以及修剪未選中的節(jié)點:
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.初始化:
我們創(chuàng)建了一個 nodes 列表,并向其中添加了一些 TreeNode 實例。
每個節(jié)點都有一個 code(標識符)、一個 pcode(父節(jié)點的標識符)以及一個 check 標志(表示是否被選中)。
2.構建樹:
使用 buildTree 方法構建樹結構。這個方法創(chuàng)建了一個 nodeMap 用于存儲所有節(jié)點,并通過 pcode 將子節(jié)點與父節(jié)點關聯(lián)起來。
在構建完成后,調(diào)用 pruneUnselectedNodes 方法來移除未選中的節(jié)點。
3.修剪未選中的節(jié)點:
pruneUnselectedNodes 方法遞歸地遍歷樹,并移除那些未選中的葉子節(jié)點。
如果一個節(jié)點未被選中并且沒有選中的子節(jié)點,它會被從樹中移除。
4.打印結果:
最后,我們定義了一個 printTree 方法來打印出樹的結構。這可以幫助我們驗證樹是否正確構建。
你可以運行這段代碼來看看最終構建出來的樹是什么樣的。如果需要進一步的幫助或修改,請隨時告知。