个人笔记-递归构造树

原创 2016年05月31日 16:17:08

/**
     * 根据类型列表构建树结构
     *
     * @param rootId
     * @param typeList
     * @return
     */
    public static Map<String, Object> buildTypeTree(String rootId, List<Map<String, Object>> typeList) {
        Map<String, Object> treeMap = new HashMap<String, Object>();
        treeMap.put("ID", rootId);
        for (Map<String, Object> m : typeList) {
            String idPath = (String) m.get("IDPATH");
            if (idPath == null) {
                treeMap.put("ID", m.get("ID"));
                treeMap.put("NAME", m.get("NAME"));
                continue;
            }
            String[] ids = idPath.split("->");
            m.remove("IDPATH");
            if (ids.length > 0)
                createTree(treeMap, ids, 0, m);
        }
        return treeMap;
    }

    public static void createTree(Map<String, Object> treeMap, String[] ids, int index, Map<String, Object> nodeMap) {
        List<Map<String, Object>> childList = (List<Map<String, Object>>) treeMap.get("CHILD");
        if (childList == null) {
            treeMap.put("CHILD", new ArrayList<Map<String, Object>>());
            childList = (List<Map<String, Object>>) treeMap.get("CHILD");
        }
        if (childList.size() == 0) {
            if (index < ids.length) {
                if (ids.length - index == 1) {
                    childList.add(nodeMap);
                } else {
                    Map<String, Object> childMap = new HashMap<String, Object>();
                    childMap.put("ID", ids[index]);
                    childList.add(childMap);
                    createTree(childMap, ids, ++index, nodeMap);
                }
            }
        } else {
            boolean add = true;
            for (Map<String, Object> childMap : childList) {
                if (childMap.get("ID").equals(ids[index])) {
                    add = false;
                    if (ids.length - index == 1) {
                        childMap.putAll(nodeMap);
                    } else {
                        createTree(childMap, ids, ++index, nodeMap);
                    }
                }
            }
            if (add) {
                if (ids.length - index == 1) {
                    childList.add(nodeMap);
                } else {
                    Map<String, Object> childMap = new HashMap<String, Object>();
                    childMap.put("ID", ids[index]);
                    childList.add(childMap);
                    createTree(childMap, ids, ++index, nodeMap);
                }
            }
        }
    }



/**
	 * 根据code对事件排序
	 * @param child
	 */
	private void sort(List<Map<String, Object>> child) {
		Collections.sort(child, new Comparator<Map<String, Object>>() {
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				String code1 = (String) o1.get(("CODE"));
				String code2 = (String) o2.get(("CODE"));
				if (code1 == null && code2 == null) {
					return 0;
				} else if (code1 != null && code2 == null) {
					return 1;
				} else if (code1 == null && code2 != null) {
					return -1;
				} else if (code1 != null && code2 != null) {
					return code1.compareToIgnoreCase(code2);
				}
				return -1;
			}
		});
		List<Map<String, Object>> $mapList1 = new ArrayList<>();
		List<Map<String, Object>> $mapList2 = new ArrayList<>();
		for (int i = 0, j = child.size(); i < j; i++) {
			String code = (String) child.get(i).get("CODE");
			if (code == null) {
				$mapList2.add($mapList1.size(), child.get(i));
			} else {
				$mapList1.add(child.get(i));
			}
		}
		$mapList1.addAll($mapList2);
	}

public static void sort(Map<String, Object> treeMap) {
        List<Map<String, Object>> child = (List<Map<String, Object>>) treeMap.get("CHILD");
        if (child != null) {
            sort(child);
            for (Map<String, Object> map : child) {
                sort(map);
            }
        }
    }




String sql="select substr(sys_connect_by_path(parent_id,'->'),3) idpath,id,name from sc_t_affair_type start with parent_id='00001' connect by prior id=parent_id";






public static void main(String ar[]) throws Exception{
        String[] ids1 = {"1","2","3","4"};
        String[] ids11 = {"1","2","3","5"};
        String[] ids2 = {"1","22"};
        String[] ids22 = {"1","222"};
        String[][] ids={ids1,ids11,ids2,ids22};
        Map<String,Object> treeMap=new HashMap<String,Object>();
        treeMap.put("CHILD",new ArrayList<Map<String,Object>>());
        treeMap.put("ID","1");
        for(int i=0;i<ids.length;i++){
            Map<String,String> nodeMap=new HashMap<>();
            nodeMap.put("NAME","name"+i);
            nodeMap.put("ID", ids[i][ids[i].length - 1]);
            createTree(treeMap, ids[i], 1, nodeMap);
        }
        System.out.println(treeMap);
    }

    public static void createTree(Map<String,Object> treeMap,String[] ids,int index, Map<String,String> nodeMap){

        List<Map<String,Object>> childList=(List<Map<String,Object>>)treeMap.get("CHILD");
        if(childList==null){
            treeMap.put("CHILD",new ArrayList<Map<String,Object>>());
            childList=(List<Map<String,Object>>)treeMap.get("CHILD");
        }
        if(childList.size()==0){
            Map<String,Object> childMap=new HashMap<String,Object>();
            childMap.put("ID",ids[index++]);
            childList.add(childMap);
            System.out.println(ids.length);
            if(index<ids.length)
                createTree(childMap,ids,index,nodeMap);
        }else{
            boolean add=true;
            for(Map<String,Object> map:childList){
                if(ids[index].equals(map.get("ID"))){
                    add=false;
                    createTree(map,ids,++index,nodeMap);
                }
            }
            if(add){
                Map<String,Object> childMap=new HashMap<String,Object>();
                childMap.put("ID",ids[index++]);
                childList.add(childMap);
                if(index<ids.length)
                    createTree(childMap,ids,index,nodeMap);
            }
        }
    }



                    
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

编译原理学习笔记05——(识别孙悟空72变之魔鬼特训—递归下降分析程序构造)——2014_1_20

感觉学习资料还不全,模仿了一个类似简单程序,还有点不完整,等到后面再完善吧。 不含左递归和每个非终结符的所有候选式的终结首符集都两两不相交条件 暂时先放在一边,也是后面再回头理解。...

递归构造树 treeview

树形结构在开发中的应用 撰文: 李洪根 本文首发于《CSDN开发高手》2003年第十二期 概述TreeView是一个重要的控件,无论是在VB.NET,C# 还是VB、Delphi等各种语言中,都充当了...

用二叉链表存储结构构造一棵二叉树,然后用栈结构进行非递归遍历

二叉树的存储结构包括顺序存储和二叉链表存储结构,其遍历的方式分为递归遍历和非递归遍历,本文章使用二叉链表构建一棵二叉树,然后使用栈这种数据结构实现二叉树的中序非递归遍历。...

PAT 1086. Tree Traversals Again (25)(依据中序遍历的非递归形式用到的堆的状态,来构造树并输出后序遍历)

官网1086. Tree Traversals Again (25)时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ...

C语言构造并递归遍历二叉树

#include #include #define FALSE 1 #define ERROR 0 #define OK 1 #define ON 0 typedef struct BiTNode...

leetCode解题报告之构造二叉树(递归)

此博文主要讲述了构造二叉树的两种方法: 1、通过先序和中序构造出二叉树( 来自leetCode OJ上的 题目:Construct Binary Tree from Preorder and Ino...

php 实现不用递归的树形数组构造函数 无限级分类

本文章转载于:http://bbs.csdn.net/topics/370094009   /** * 创建父节点树形数组 * 参数 * $ar 数组,邻接列表方式组织的数据 * $id ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:个人笔记-递归构造树
举报原因:
原因补充:

(最多只允许输入30个字)