Tree Construction Interview Questions

Tree structure construction
Topic:
Convert the tree shape, filter the tree shape of IEntityTable through InputEntityValidList, and rebuild the tree shape:
1. If the parent node is not in the filter list, judge upward until the upper node exists, and hang to the node Down. Or the parent node cannot be found, and it is hung under the root node.
2. According to the passed parameter curEntityKey, set the node’s isSelected to true
3. According to the passed parameter InputEntityValidList, set the node’s isChecked to true
4. According to the new tree shape, set the node’s ITree<> isLeaf attribute
For example:
[Original EntityQuery Tree]:
A
A1
A11
A111
A12
A2
A21
A22
B
B1
B11
InputEntityValidList: A, A111, A12, B11
curEntityKey: A
The new tree is:
A (isChecked: true, isSelected: true, isLeaf: false)
A111 (isChecked :true, isSelected:false, isLeaf:true)
A12 (isChecked:true, isSelected:false, isLeaf:true)
B11 (isChecked:true, isSelected:false, isLeaf:false)
Input parameters
IEntityTable entityTable, List inputEntityValidList, String curEntityKey
Output:
List<ITree>
Node conversion example:
ITree target = new ITree(LightNodeData.buildEntityData(row));
target. setLeaf(true);
target.setSelected(true);
target.setChecked(true);
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 Then, we define a class to represent the data of the tree node, including the data of the node. Next, we define a class to represent a tree, including a root node and a method to build the tree. In the method, we first judge whether the data list is empty, if not, take the first data as the root node, and then traverse all the data in the data list, for each data, build a new node, and according to The node's selected state sets the node's data. Finally, we set the root node's data to the newly constructed node's data.
TreeNodeDataTreeNodeTreebuildTreebuildTree

It should be noted that in practical applications, we need to build a tree according to specific business requirements, and set the data and status of nodes as needed. At the same time, we also need to consider the complexity and performance of the tree, 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 tree-building functionality:
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);  

}
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/129987937