• Java递归树(构建和收集子孙节点二种方式)


    Java递归树(构建和收集子孙节点二种方式)

    TreeUtil

    1.递归工具类:(构建递归树 且支持 通过 buildByRecursive方法 depth参数控制递归的层级深度 )

    /**
     * @Description: 树节点
     * @Author: 徐刘
     * @Date: 2020/12/5 12:18
     */
    @Data
    public class TreeNode {
    
    <span class="token keyword">protected</span> String id<span class="token punctuation">;</span>
    
    <span class="token keyword">protected</span> String parentId<span class="token punctuation">;</span>
    
    <span class="token keyword">private</span>  <span class="token keyword">int</span> level<span class="token punctuation">;</span>
    <span class="token comment">// 是否有子节点</span>
    <span class="token keyword">private</span>  <span class="token keyword">boolean</span> isLeaf<span class="token punctuation">;</span>
    
    List<span class="token generics function"><span class="token punctuation">&lt;</span>TreeNode<span class="token punctuation">&gt;</span></span> children <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span>TreeNode node<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        children<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>node<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    

    }

    /**

    • @Description: tree结构工具类

    • @Author: 徐刘

    • @Date: 2020/12/5 12:17
      /
      public class TreeUtil {
      /
      *

      • 两层循环实现建树

      • @param treeNodes 传入的树节点列表

      • @return
        */
        public static <T extends TreeNode> List<T> bulid(List<T> treeNodes, Object root) {

        List<T> trees = new ArrayList<T>();

        for (T treeNode : treeNodes) {

         <span class="token keyword">if</span> <span class="token punctuation">(</span>root<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>treeNode<span class="token punctuation">.</span><span class="token function">getParentId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
             trees<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>treeNode<span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token punctuation">}</span>
        
         <span class="token keyword">for</span> <span class="token punctuation">(</span>T it <span class="token operator">:</span> treeNodes<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
             <span class="token keyword">if</span> <span class="token punctuation">(</span>StringUtils<span class="token punctuation">.</span><span class="token function">equalsIgnoreCase</span><span class="token punctuation">(</span>it<span class="token punctuation">.</span><span class="token function">getParentId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> treeNode<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
                 <span class="token keyword">if</span> <span class="token punctuation">(</span>CollectionUtils<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>treeNode<span class="token punctuation">.</span><span class="token function">getChildren</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
                     treeNode<span class="token punctuation">.</span><span class="token function">setChildren</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                 <span class="token punctuation">}</span>
                 treeNode<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>it<span class="token punctuation">)</span><span class="token punctuation">;</span>
             <span class="token punctuation">}</span>
         <span class="token punctuation">}</span>
        

        }
        return trees;
        }

      /**

      • 使用递归方法建树

      • @param treeNodes

      • @param depth 需要递归的层级 深度

      • @return
        */
        public static <T extends TreeNode> List<T> buildByRecursive(List<T> treeNodes, Object root, Integer depth) {

        List<T> trees = new ArrayList<T>();
        int level = 1;
        for (T treeNode : treeNodes) {
        // 获取根节点(可以有多个根节点)
        if (root.equals(treeNode.getParentId())) {
        // 每个根节点递归添加子节点
        trees.add(findChildren(treeNode, treeNodes,level,depth));
        treeNode.setLevel(level);
        }
        }
        return trees;
        }

      /**

      • @Description: 递归查找子节点
      • @Author: 徐刘
      • @Date: 2020/12/7 9:57
      • @param treeNode
      • @param treeNodes
      • @param level 层级
      • @param depth
      • @Return: T
        */
        public static <T extends TreeNode> T findChildren(T treeNode, List<T> treeNodes, int level, Integer depth) {
        if(depthnull){
        depth = Integer.MAX_VALUE;
        }
        for (T it : treeNodes) {
        if (StringUtils.equalsIgnoreCase(treeNode.getId(), it.getParentId())) {
        if (treeNode.getChildren() null) {
        treeNode.setChildren(new ArrayList<>());
        }
        // 控制递归的 层级深度
        if(level>=depth){
        return treeNode;
        }
        treeNode.add(findChildren(it, treeNodes, level +1, depth));
        }
        treeNode.setLevel(level);
        treeNode.setLeaf(treeNode.getChildren().size() <= 0);
        }
        return treeNode;
        }

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    1. 递归构建 非树形结构 ,返回指定节点 下子孙节点 收集成一个list 集合

      商品分类 示例

       /**
    	 * @desc 递归查找出该节点下面的子孙节点
    	 * @return
    	 * @author xuliu
    	 * @date 2020/7/4 15:32
    	 */
    
    <span class="token keyword">public</span> List<span class="token generics function"><span class="token punctuation">&lt;</span>CPProductClass<span class="token punctuation">&gt;</span></span> <span class="token function">buildByRecursive</span><span class="token punctuation">(</span>List<span class="token generics function"><span class="token punctuation">&lt;</span>CPProductClass<span class="token punctuation">&gt;</span></span> targetProductClasslist<span class="token punctuation">,</span> String classid<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
    	List<span class="token generics function"><span class="token punctuation">&lt;</span>CPProductClass<span class="token punctuation">&gt;</span></span> trees <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	Integer level <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
    	<span class="token function">findChildren</span><span class="token punctuation">(</span>trees<span class="token punctuation">,</span>targetProductClasslist<span class="token punctuation">,</span>classid<span class="token punctuation">,</span>level<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token keyword">return</span> trees<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">private</span> List<span class="token generics function"><span class="token punctuation">&lt;</span>CPProductClass<span class="token punctuation">&gt;</span></span> <span class="token function">findChildren</span><span class="token punctuation">(</span>List<span class="token generics function"><span class="token punctuation">&lt;</span>CPProductClass<span class="token punctuation">&gt;</span></span> trees<span class="token punctuation">,</span> List<span class="token generics function"><span class="token punctuation">&lt;</span>CPProductClass<span class="token punctuation">&gt;</span></span> targetProductClasslist<span class="token punctuation">,</span> String classid<span class="token punctuation">,</span>Integer level<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
    	<span class="token keyword">if</span><span class="token punctuation">(</span>targetProductClasslist <span class="token operator">!=</span>null <span class="token operator">&amp;&amp;</span> targetProductClasslist<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
    		<span class="token keyword">for</span> <span class="token punctuation">(</span>CPProductClass cpProductClass <span class="token operator">:</span> targetProductClasslist<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
    			<span class="token keyword">if</span> <span class="token punctuation">(</span>cpProductClass<span class="token punctuation">.</span><span class="token function">getParentclassid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>classid<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
    				<span class="token function">findChildren</span><span class="token punctuation">(</span>trees<span class="token punctuation">,</span>targetProductClasslist<span class="token punctuation">,</span>cpProductClass<span class="token punctuation">.</span><span class="token function">getClassid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>level<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    				cpProductClass<span class="token punctuation">.</span><span class="token function">setClasslevel</span><span class="token punctuation">(</span>level<span class="token punctuation">)</span><span class="token punctuation">;</span>
    				trees<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>cpProductClass<span class="token punctuation">)</span><span class="token punctuation">;</span>
    			<span class="token punctuation">}</span>
    		<span class="token punctuation">}</span>
    	<span class="token punctuation">}</span>
    	<span class="token keyword">return</span> trees<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    https://blog.csdn.net/weixin_43298522/article/details/118057675
  • 相关阅读:
    Codeforces Round #384 (Div. 2)
    Codeforces Round #383 (Div. 2)
    bzoj-4514(网络流)
    bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)
    bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)
    hdu-5988 Coding Contest(费用流)
    hdu-5992 Finding Hotels(kd-tree)
    用链表实现杭电1276士兵队列训练问题
    循环链表
    图书管理系统
  • 原文地址:https://www.cnblogs.com/sunny3158/p/16692600.html
Copyright © 2020-2023  润新知