Work summary - get the sub nodes of the tree and copy the tree

Posted by porrascarlos80 on Fri, 10 Jan 2020 09:04:15 +0100

1: Structure of tree nodes

class KprDimension{
    private String id; //Current node id of the tree
    private String name; //Tree node name
    private String parentId;// Tree node parent id
    private Integer level; //The node level of the tree, as shown in the figure below, A is 1, B,C is 2
    private List<KprDimension> children;//Child nodes of the tree
}

1 get the lowest child node of the tree (that is, the node with empty child node)


That is to get the nodes of the tree without child nodes: G, H, I, J, F.

1.1 way of thinking

The concept of dimension is used in the project, and its relationship is the same as that of tree structure. Now there is a requirement to obtain the lowest level dimension, and convert it to obtain nodes without child nodes in tree structure. Read out the data in the database and convert it into a tree structure. Or use SQL statement to directly convert to tree structure.

1.2 recursive call

    /**
     * Recursive call to get the lowest dimension
     *
     * @param kprDimension
     * @param returnList
     * @return returnList
     */
    private List<KprDimension> getChildrenDimensions(KprDimension kprDimension,List<KprDimension> returnList){

        List<KprDimension> childrenList = kprDimension.getChildren();
        // The conditions for exiting recursion have only one dimension structure
        if(childrenList==null || childrenList.size()<=0){
            returnList.add(kprDimension);
        }else{
            // There are multiple dimension results
            for(KprDimension childrenDimension : childrenList){
                getChildrenDimensions(childrenDimension,returnList);
            }
        }
        return returnList;
    }

The returnList parameter, which is either the in parameter or the out parameter, is used to store the nodes satisfying the conditions in the recursion (nodes without child nodes). The idea is very simple, and the implementation is also very simple. It mainly stores the parameters that meet the conditions, and uses the input parameters to store them.

Replication of tree structure in database

The tree structure is one piece of data in the database. The project has a requirement to copy these data, but the id and parentId cannot be the same, that is to say, only copy the structure and part of the information of the tree, but the id and other information must be replaced to avoid that the information copied by a user will be modified or deleted at the same time.

2.1 way of thinking

According to the level in the database, the original id and the new id after copying are stored in the map set.
When copying the first layer, there is only id and no parentId. Therefore, it is successfully copied. At this time, there is a corresponding relationship between the old id and the new id in the map collection.
When copying the second level of the tree according to the level, you need to replace the parentId. Because the first level structure has the id corresponding relationship of the map, you can directly use the parentId as the key of the map set to obtain the id of the new relationship (the second level structure of the corresponding tree is parentId).
The third layer is the same as the second layer. When the parentId is replaced, the map set already has the first and second level id corresponding relationship, which can be obtained directly. Because replication is based on the tree level, the parentId to be replicated must be stored in the map collection before replication.

2.2 code implementation

There are some deletions, mainly expressing a thought.

    @Override
    public RetEntity<Map<String,String>> copyDimensionByAssessmentId(String copyAssessmentId, String newAssessmentId) throws CommonException {

        // Find all dimensions in the database to be copied
        EntityWrapper<KprDimension> ew =new EntityWrapper<>();
        ew.eq("assessment_id",copyAssessmentId);
        List<KprDimension> dimensionList = this.selectList(ew);
        Map<String,String> old_new_dimensionId_map = new HashMap<>(10);
        // According to the hierarchical classification of dimensions (i.e. the first level and the second level of tree structure)
        Map<Integer, List<KprDimension>> dimensionMap = dimensionList.stream().collect(Collectors.groupingBy(KprDimension::getRank));
        for (int i = 1; i <= dimensionMap.size(); i++) {
            List<KprDimension> list = dimensionMap.get(i);
            Collections.sort(list);
            // First level dimension (rank number of dimension starts from the beginning)
            if (i == 1) {
                for(KprDimension kprDimension : list){
                    String newDimensionId = IdWorker.get32UUID();
                    old_new_dimensionId_map.put(kprDimension.getId(),newDimensionId);
                    kprDimension.setId(newDimensionId);
                    this.insertAllColumn(kprDimension)
                }
            } else{
                for(KprDimension kprDimension : list){
                    // Get parent dimension id
                    String parentDimensionId =old_new_dimensionId_map.get(kprDimension.getParentDimensionId());
                    String newDimensionId = IdWorker.get32UUID();
                    old_new_dimensionId_map.put(kprDimension.getId(),newDimensionId);
                    kprDimension.setId(newDimensionId);
                    kprDimension.setParentDimensionId(parentDimensionId);
                    this.insertAllColumn(kprDimension)
                    }
                }
            }
        }
        return null;
    }

Topics: Programming Database SQL