读书人

用递归算法查找父节点上的所有叶子节点

发布时间: 2012-10-20 14:12:48 作者: rapoo

用递归算法查找父节点下的所有叶子节点

父节点

----叶子节点

----子节点一

---------叶子节点

---------叶子节点

----子节点二

---------子节点三

----------------叶子节点

----------------叶子节点

如何得到父节点

采用递归算法,碰到叶子节点就加到列表里,不是叶子节点就对其进行循环再递归遍历

/** * 向旗添加 * 实现将得到的目录的所有子目录和目录本身变成ID的LIST * @param 目录ID * @return  目录与其所有子目录组成的ID的LIST * @throws ServiceException */public List<Long> getAllPisTCategoryStr(PisTCategory obj)throws ServiceException{List<PisTCategory> listAll = new ArrayList<PisTCategory>();List<Long> list = new ArrayList<Long>();listAll = getAllPisTCategoryByParentId(obj);    if(listAll.size()!=0){         for(int i =0;i<listAll.size();i++){         list.add(listAll.get(i).getId());         }    }    return list;}/** * 向旗添加 * 实现根据目录ID得到其所有子目录list * @param 目录ID * @return 所有子目录list * @throws ServiceException */public List<PisTCategory> getAllPisTCategoryByParentId(PisTCategory obj)throws ServiceException{List<PisTCategory> listAll = new ArrayList<PisTCategory>();List<PisTCategory> list = new ArrayList<PisTCategory>();List<PisTCategory> listtest = new ArrayList<PisTCategory>();try{if(obj.getLeaf() != null){if(obj.getLeaf()==1){listAll.add(obj);return listAll;}}obj.setLeaf(null);list =  getPisTCategoryByParentId_Leaf(obj);if(list.size()!=0){for(int i=0;i<list.size();i++){PisTCategory instance = new PisTCategory();instance = list.get(i);instance.setParentId(instance.getId());listtest = getAllPisTCategoryByParentId(instance);listAll.addAll(listtest);}}}catch(Exception e){e.printStackTrace();throw new ServiceException(this.getClass().getName()+e.getMessage());}return listAll;}

?

/** * 向旗添加 * 实现根据父目录查询其子目录及其的所有商品信息 * @param categoryId * @return * @throws ServiceException */public List<PisTProduct> getListPisTProductByCategoryId(Long categoryId)throws ServiceException{PisTCategory obj = new PisTCategory();obj = pisTCategoryService.getPisTCategoryById(categoryId);obj.setParentId(obj.getId());List<Long> list = pisTCategoryService.getAllPisTCategoryStr(obj);System.out.println(list.size());GetProductBySiteCategoryQueryObject qo = new GetProductBySiteCategoryQueryObject();qo.setSiteId(null);qo.setCategorys(list);try {return pisTProductDao.getListPisTProductBySiteId_CategoryIDs(qo);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}
?

读书人网 >编程

热点推荐