您当前的位置: 首页 > 

wespten

暂无认证

  • 4浏览

    0关注

    899博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

递归查询的使用

wespten 发布时间:2018-10-28 07:32:15 ,浏览量:4

递归优点:

1. 简洁

2.在树的前序,中序,后序遍历算法中,递归的实现明显要比循环简单得多。

递归缺点:

1.递归由于是函数调用自身,而函数调用是有时间和空间的消耗的:每一次函数调用,都需要在内存栈中分配空间以保存参数、返回地址以及临时变量,而往栈中压入数据和弹出数据都需要时间。->效率

2.递归中很多计算都是重复的,由于其本质是把一个问题分解成两个或者多个小问题,多个小问题存在相互重叠的部分,则存在重复计算,如fibonacci斐波那契数列的递归实现。->效率

3.调用栈可能会溢出,其实每一次函数调用会在内存栈中分配空间,而每个进程的栈的容量是有限的,当调用的层次太多时,就会超出栈的容量,从而导致栈溢出。->性能

递归与非递归的对比

用递归首先要找到规律,然后要有一个出口

	/**
	 * 非递归方式求阶乘
	 * @param n
	 * @return
	 */
	static long notDigui(int n){
		long result=1;
		for(int i=1;i0) {
	    		treeNode.put("children", childrens);
	    	}else {
	    		treeNode.put("children", null);
	    	}
	    	treeNodes.add(treeNode);	
	    }
	    rtnNode.put("id", -1);
	    rtnNode.put("text", "街道");
	    rtnNode.put("flag", "1");
	    rtnNode.put("children", treeNodes);
	    rtnNodes.add(rtnNode);
		return rtnNodes;
	}

递归查询

	@GetMapping("/getDicByCodes/{dictCodes}")
	public AssembleJSON getDicByCodes(@PathVariable String dictCodes) {
		SessionData sessionData = getCurrUserData();
		int userId = sessionData.getUserId();
		String orgManageDataCode = sessionData.getOrgManageDataCode();
		String orgDataCode = sessionData.getOrgDataCode();
		Integer orgLevel = sessionData.getOrgLevel();
		return AssembleJSON.SUCCESS(service.getDicByCodes(dictCodes,userId,orgManageDataCode,orgDataCode,orgLevel));
	}

@Override
	public Hashtable getDicByCodes(String dictCodes,Integer userId, String orgManageDataCode,String orgDataCode, Integer orgLevel){
		Hashtable hashtable = new Hashtable();
		String[] codes = dictCodes.split(",");
		List dicItems = new ArrayList();
		for(String code : codes){
			dicItems = getDicByCode(code,userId,orgManageDataCode,orgDataCode,orgLevel);
			if(dicItems != null){
				hashtable.put(code, dicItems);
			}
		}
		return hashtable;
	}
	
	@Override
	public List getDicByCode(String dictCode,Integer userId, String orgManageDataCode,String orgDataCode, Integer orgLevel) {
		Dictionary dictionary1 = new Dictionary();
		dictionary1.setDictCode(dictCode);
		Dictionary dictionary = mapper.selectOne(dictionary1);
		if(dictionary == null){
			return null;
		}
		List listDic = new ArrayList();
		if(dictionary != null){		
			if(dictionary.getDictType() == 1){
				List list = dictionaryItemMapper.selectDictionaryItemAll(dictCode);
				getDicList(list,listDic);
			}else{
				String source1 = dictionary.gettSource();
				if(source1 == null || "".equals(source1)){
					return null;
				}
				String source = "";
				String source2 = "";
				String source3 = "";
				String source4 = "";
				source2 = source1.replace("{@sys_orgDataCode@}", orgDataCode);
				source3 = source2.replace("{@sys_userId@}", String.valueOf(userId));			
				source4 = source3.replace("{@sys_orgManageDataCode@}", orgManageDataCode);
				source = source4.replace("{@sys_orgLevel@}",String.valueOf(orgLevel));
				String code = dictionary.getfCode();
				String name = dictionary.getfName();
				String sort = dictionary.getfSort();
				String pField = dictionary.getpField();
			    List list = new ArrayList();
			    if(sort == null || "".equals(sort)){
			    	if(pField == null || "".equals(pField)){
			    		list = mapper.selectDictionary(code, name, source,"'"+sort+"'");
			    	}else{
			    		list = mapper.selectDictionaryParent(code, name, source,"'"+sort+"'",pField);
			    	}	   
			    }else{
			    	if(pField == null || "".equals(pField)){
			    		list = mapper.selectDictionary(code, name, source,sort);
			    	}else{
			    		list = mapper.selectDictionaryParent(code, name, source,sort,pField);
			    	}	  
			    }			   
			getDicList(list,listDic);				
			}
		}
		return listDic;
	}
	

 

public void getDicList(List list,List listDic){
		if(list.size() == 1){
			for(DictionaryItem dItem : list){				
					DicItem dic = new DicItem();
					dic.setId(dItem.getItemCode());
					dic.setText(dItem.getItemName());
					listDic.add(dic);
			}
		}else{
			boolean flag = true;
			for(DictionaryItem dItem : list){	
				if(dItem.getParentCode() == null || "".equals(dItem.getParentCode())){
					DicItem dic = new DicItem();
					dic.setId(dItem.getItemCode());
					dic.setText(dItem.getItemName());
					listDic.add(dic);
					getChild(list,dic);
					flag = false;
				}
		    }
			if(flag){
				if(list.size() > 0){
					int min = list.get(0).getParentCode().toString().length();
					for(int i=1; i            
关注
打赏
1665965058
查看更多评论
0.0437s