public void copyElementTree(List<DictTree> dictTrees, String newTempletId, String parentId) throws Exception {String parent = "";//父节点idif (CollectionUtils.isNotEmpty(dictTrees)) {//遍历树结构for (DictTree treeDto : dictTrees) {//查询评估指标层级数据InstitutionElement institutionElement = institutionElementMapper.selectById(treeDto.getId().toString());InstitutionElement element = new InstitutionElement();BeanUtils.copyProperties(institutionElement, element);String templetId = institutionElement.getElementKey();//模板idString oldElementId = institutionElement.getTempletKey();//风险维度id//如果是叶子节点if (treeDto.getIsLast().equals(RiskConstant.IS_LEAF_1)) {//复制叶子节点insertElement(element, newTempletId, parentId);} else {//复制普通节点if (treeDto.getParentId().equals("-1")) {parent = insertElement(element, newTempletId, "-1");} else {parent = insertElement(element, newTempletId, parentId);}//如果不是叶子节点,递归copyElementTree(treeDto.getChildren(), newTempletId, parent);}}}}
以上是递归的主要方法。该方法可以实现普通树结构的复制,不局限于二叉树。
复制的步骤:
1.首先需要获取树结构
2.调用该方法进行遍历
获取树结构,目前用的是treeNode插件实现,也可以使用递归。
