This commit is contained in:
Looly 2020-05-07 16:00:30 +08:00
parent 2ea7bfc54b
commit 987d9e35f5
2 changed files with 40 additions and 3 deletions

View File

@ -134,14 +134,14 @@ public class TreeUtil {
return node;
}
//fix NPE
if(null == node.getChildren()) {
final List<Tree<T>> children = node.getChildren();
if(null == children) {
return null;
}
// 查找子节点
Tree<T> childNode;
for (Tree<T> child : node.getChildren()) {
for (Tree<T> child : children) {
childNode = child.getNode(id);
if (null != childNode) {
return childNode;

View File

@ -0,0 +1,37 @@
package cn.hutool.core.lang.tree;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class TreeSearchTest {
static List<TreeNode<Long>> all_menu=new ArrayList<>();
static {
/*
* root
* /module-A
* /module-A-menu-1
* /module-B
* /module-B-menu-1
* /module-B-menu-2
*/
all_menu.add(new TreeNode<>(1L, 0L, "root", 0L));
all_menu.add(new TreeNode<>(2L,1L,"module-A",0L));
all_menu.add(new TreeNode<>(3L,1L,"module-B",0L));
all_menu.add(new TreeNode<>(4L,2L,"module-A-menu-1",0L));
all_menu.add(new TreeNode<>(5L,3L,"module-B-menu-1",0L));
all_menu.add(new TreeNode<>(6L,3L,"module-B-menu-2",0L));
}
@Test
public void searchNode() {
List<Tree<Long>> treeItems=TreeUtil.build(all_menu, 0L);
Tree<Long> tree=treeItems.get(0);
Tree<Long> searchResult=tree.getNode(3L);
Assert.assertEquals("module-B", searchResult.getName());
}
}