The following is a sample code for tree construction based on Java: import java.util.ArrayList;

The following is a sample code for tree construction based on Java: import java.util.ArrayList;
import java.util.List;

public class TreeNode {
private LightNodeData data;
private boolean isLeaf;
private boolean isChecked;

public TreeNode(LightNodeData data) {  
    this.data = data;  
    this.isLeaf = false;  
    this.isChecked = false;  
}  

public LightNodeData getData() {  
    return data;  
}  

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

public boolean isLeaf() {  
    return isLeaf;  
}  

public void setLeaf(boolean isLeaf) {  
    this.isLeaf = isLeaf;  
}  

public boolean isChecked() {  
    return isChecked;  
}  

public void setChecked(boolean isChecked) {  
    this.isChecked = isChecked;  
}  

@Override  
public String toString() {  
    return data.toString();  
}  

}

public class TreeNodeData {
private LightNodeData data;

public TreeNodeData(LightNodeData data) {  
    this.data = data;  
}  

public LightNodeData getData() {  
    return data;  
}  

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

}

public class TreeNodeTree {
private TreeNode root;

public TreeNodeTree(TreeNode root) {  
    this.root = root;  
}  

public void buildTree(List<TreeNodeData> dataList) {  
    if (dataList.isEmpty()) {  
        return;  
    }  

    TreeNodeData data = dataList.get(0);  
    TreeNode node = new TreeNode(data);  

    for (TreeNodeData data2 : dataList) {  
        TreeNode node2 = new TreeNode(data2);  

        if (node2.isLeaf()) {  
            node.setLeaf(true);  
            node2.setLeaf(true);  
        } else {  
            node.setChecked(true);  
            node2.setChecked(true);  
        }  

        node.setData(data2.getData());  
        node2.setData(data.getData());  
    }  

    root.setData(data.getData());  
}  

}
In this example, we define a TreeNode class to represent tree nodes, including node data, whether it is a leaf node and whether it is selected. We also define a TreeNodeData class to represent the data of the tree node, including the data of the node and whether it is a leaf node and whether it is selected.

Next, we define a TreeNodeTree class to represent the entire tree structure, including the root node. In the constructor, we get the first data from the data list and create a new node object. Then, we traverse all the data in the data list, for each data, create a new node object, and set the data and status of the node according to whether the node is a leaf node and whether it is selected. Finally, we set the data of the root node to the data of the newly created node object.

Finally, we can use the following code to test the functionality of tree construction: public static void main(String[] args) { TreeNodeData data1 = new TreeNodeData(new LightNodeData(“A”)); TreeNodeData data2 = new TreeNodeData(new LightNodeData( "A1")); TreeNodeData data3 = new TreeNodeData(new LightNodeData("A11")); TreeNodeData data4 = new TreeNodeData(new LightNodeData("A12")); TreeNodeData data5 = new TreeNodeData(new LightNodeData("B1")) ; TreeNodeData data6 = new TreeNodeData(new LightNodeData("B11"));





TreeNodeTree tree = new TreeNodeTree(tree);  

tree.buildTree(dataList);  

}

Guess you like

Origin blog.csdn.net/weixin_72686492/article/details/129988373