标签导航:

深度剖析树结构递归优化策略

祖传代码中使用递归生成树结构数据,效率低下,本文将针对以下java代码片段进行优化,提升树结构生成效率。

public list<map> creategrouptreenode() {
    list<map> childrenlist = new arraylist<map>();
    getchildlist(0l,childrenlist);
    list<map> treelist = new arraylist<map>();
    map map = new hashmap();
    map.put("id", 0);
    map.put("text","根节点");
    map.put("isleaf", "0");
    map.put("icon", "fa fa-folder");
    map submap = new hashmap();
    submap.put("opened", true);
    submap.put("selected", true);
    map.put("state", submap);
    map.put("children", childrenlist);
    treelist.add(map);
    return treelist;  
}

public list<map> getchildlist(long id,list<map> childrenlist){
    list<basegroup> childlist = basemapper.childlistbyparentid(id);
    if(childlist != null && childlist.size() > 0){
        list<map> tempmap = new arraylist<map>();
        for(int i = 0; i < childlist.size(); i++){
            list<map> mylist = getchildlist(childlist.get(i).getid(),childrenlist);
            map map = new hashmap();
            if(mylist == null){
                map.put("id", childlist.get(i).getid());
                map.put("text", childlist.get(i).getnumber() + " - " + childlist.get(i).getname());
                map.put("isleaf", "1");
                map.put("icon", "fa fa-folder");
                map submap = new hashmap();
                submap.put("opened", false);
                map.put("state", submap);
                tempmap.add(map);
            }else{
                map.put("id", childlist.get(i).getid());
                map.put("text", childlist.get(i).getnumber() + " - " + childlist.get(i).getname());
                map.put("isleaf", "0");
                map.put("icon", "fa fa-folder");
                map.put("children", mylist);
                map submap = new hashmap();
                submap.put("opened", false);
                map.put("state", submap);
                tempmap.add(map);
            }
            if(id == 0){
                childrenlist.add(map);
            }
        }
        return tempmap;
    }
    return null;
}

这段代码中getchildlist方法存在效率问题。首先,childrenlist参数作为输出参数,仅在id == 0l时使用,这导致了不必要的参数传递。我们可以直接利用该方法的返回值,移除该参数。

其次,在getchildlist方法的循环中,反复调用childlist.get(i)降低了效率。我们可以引入一个中间变量缓存循环中的元素,或者使用增强型for循环来优化。

最后,在for循环内的if分支中,大部分代码是相同的,只有isleaf和children字段有所不同,我们可以将重复的代码提取出来,只处理差异部分。

经过优化后的代码如下:

public List<Map> createGroupTreeNode() {
    List<Map> childrenList = getChildList(0L);
    // ....
}

public List<Map> getChildList(Long id) {
    List<BaseGroup> childList = baseMapper.childListByParentId(id);
    if(childList != null && childList.size() > 0){
        List<Map> tempMap = new ArrayList<Map>();
        for (BaseGroup it : childList) {
            Map map = new HashMap();
            map.put("id", it.getId());
            map.put("text", it.getNumber() + " - " + it.getName());
            map.put("icon", "fa fa-folder");
            Map subMap = new HashMap();
            subMap.put("opened", false);
            map.put("state", subMap);

            List<Map> mylist = getChildList(it.getId());
            if (mylist == null) {
                map.put("isleaf", "1");
            } else {
                map.put("isleaf", "0");
                map.put("children", mylist);
            }
            tempMap.add(map);
        }
        return tempMap;
    }
    return null;
}

通过这些修改,我们简化了代码,并提高了代码的可读性和效率。