Java教程

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

本文主要是介绍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;

}



}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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>

1234567891011121314151617181920212223242526
https://blog.csdn.net/weixin_43298522/article/details/118057675
这篇关于Java递归树(构建和收集子孙节点二种方式)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!