用遞歸算法查找父節點下的所有葉子節點


父節點

----葉子節點

----子節點一

---------葉子節點

---------葉子節點

----子節點二

---------子節點三

----------------葉子節點

----------------葉子節點

如何得到父節點

采用遞歸算法,碰到葉子節點就加到列表里,不是葉子節點就對其進行循環再遞歸遍歷

Java代碼   收藏代碼
  1. /** 
  2.      * 向旗添加 
  3.      * 實現將得到的目錄的所有子目錄和目錄本身變成ID的LIST 
  4.      * @param 目錄ID 
  5.      * @return  目錄與其所有子目錄組成的ID的LIST 
  6.      * @throws ServiceException 
  7.      */  
  8.     public List<Long> getAllPisTCategoryStr(PisTCategory obj)throws ServiceException{  
  9.         List<PisTCategory> listAll = new ArrayList<PisTCategory>();  
  10.         List<Long> list = new ArrayList<Long>();  
  11.         listAll = getAllPisTCategoryByParentId(obj);  
  12.         if(listAll.size()!=0){  
  13.              for(int i =0;i<listAll.size();i++){  
  14.                  list.add(listAll.get(i).getId());  
  15.              }  
  16.         }  
  17.         return list;  
  18.     }  
  19.       
  20.     /** 
  21.      * 向旗添加 
  22.      * 實現根據目錄ID得到其所有子目錄list 
  23.      * @param 目錄ID 
  24.      * @return 所有子目錄list 
  25.      * @throws ServiceException 
  26.      */  
  27.     public List<PisTCategory> getAllPisTCategoryByParentId(PisTCategory obj)throws ServiceException{  
  28.         List<PisTCategory> listAll = new ArrayList<PisTCategory>();  
  29.         List<PisTCategory> list = new ArrayList<PisTCategory>();  
  30.         List<PisTCategory> listtest = new ArrayList<PisTCategory>();  
  31.         try{  
  32.             if(obj.getLeaf() != null){  
  33.             if(obj.getLeaf()==1){  
  34.                 listAll.add(obj);  
  35.                 return listAll;  
  36.             }}  
  37.             obj.setLeaf(null);  
  38.             list =  getPisTCategoryByParentId_Leaf(obj);  
  39.             if(list.size()!=0){  
  40.             for(int i=0;i<list.size();i++){  
  41.                     PisTCategory instance = new PisTCategory();  
  42.                     instance = list.get(i);  
  43.                     instance.setParentId(instance.getId());  
  44.                     listtest = getAllPisTCategoryByParentId(instance);  
  45.                     listAll.addAll(listtest);  
  46.             }  
  47.               
  48.             }  
  49.         }catch(Exception e){  
  50.             e.printStackTrace();  
  51.             throw new ServiceException(this.getClass().getName()+e.getMessage());  
  52.         }  
  53.         return listAll;  
  54.     }  

 

Java代碼   收藏代碼
  1. /** 
  2.      * 向旗添加 
  3.      * 實現根據父目錄查詢其子目錄及其的所有商品信息 
  4.      * @param categoryId 
  5.      * @return 
  6.      * @throws ServiceException 
  7.      */  
  8.     public List<PisTProduct> getListPisTProductByCategoryId(Long categoryId)throws ServiceException{  
  9.         PisTCategory obj = new PisTCategory();  
  10.         obj = pisTCategoryService.getPisTCategoryById(categoryId);  
  11.         obj.setParentId(obj.getId());  
  12.         List<Long> list = pisTCategoryService.getAllPisTCategoryStr(obj);  
  13.         System.out.println(list.size());  
  14.         GetProductBySiteCategoryQueryObject qo = new GetProductBySiteCategoryQueryObject();  
  15.         qo.setSiteId(null);  
  16.         qo.setCategorys(list);  
  17.         try {  
  18.             return pisTProductDao.getListPisTProductBySiteId_CategoryIDs(qo);  
  19.         } catch (Exception e) {  
  20.             // TODO Auto-generated catch block  
  21.             e.printStackTrace();  
  22.         }  
  23.         return null;  
  24.     }  
  25.       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM