public static void getSearchNode(String msg, OrgDto root, OrgDto out) {
boolean isSetFatherNodeValue = false;
String v = root.getName();
if (!v.isEmpty() && v.toLowerCase().indexOf(msg.toLowerCase()) > -1) {
out.setName(v);
out.setStatus(root.getStatus());
out.setCode(root.getCode());
out.setCreateTime(root.getCreateTime());
isSetFatherNodeValue = true;
}
List<OrgDto> nodes = root.getChilds();
if (nodes != null && nodes.size() > 0) {
List<OrgDto> ns = new ArrayList<>();
for (int i = 0; i < nodes.size(); i++) {
OrgDto node = OrgDto.builder().build();
getSearchNode(msg, nodes.get(i), node);
if (!Objects.isNull(node.getName()) && !node.getName().isEmpty()) {
ns.add(node);
if (!isSetFatherNodeValue) {
out.setName(root.getName());
out.setCode(root.getCode());
out.setCreateTime(root.getCreateTime());
out.setStatus(root.getStatus());
}
}
}
if (ns.size() > 0) {
out.setChilds(ns);
}
}
}
全部代码
package com.xiuye.util.tree;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xiuye.util.log.LogUtil;
import com.xiuye.util.time.TimeUtil;
public class TravelTreeNode {
public static void main(String[] args) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
TreeNode<String> root = new TreeNode<>();
root.setValue("A");
root.setStatus("open");
List<TreeNode<String>> nodes = new ArrayList<>();
for (int i = 0; i < 10; i++) {
TreeNode<String> node = new TreeNode<>();
node.setValue("B" + i);
node.setStatus("close");
List<TreeNode<String>> l = new ArrayList<>();
for (int j = 0; j < 3; j++) {
TreeNode<String> nodej = new TreeNode<>();
nodej.setValue("C" + j);
nodej.setStatus("close");
l.add(nodej);
}
node.setNodes(l);
nodes.add(node);
}
root.setNodes(nodes);
LogUtil.log(root);
LogUtil.log(gson.toJson(root));
LogUtil.log("遍历(应该是先序):");
TimeUtil.start();
travel(root);
TimeUtil.outCostOnConsoleMs();
LogUtil.log();
LogUtil.log("仅仅查出含有msg的节点,如果有上层节点就包含上层节点,但不包含其节点的子节点:");
TreeNode<String> out = new TreeNode<>();
getSearchNode("9", root, out);
TimeUtil.outCostOnConsoleMs();
LogUtil.log(gson.toJson(out));
LogUtil.log();
LogUtil.log("保留下一级所有子节点,但子节点不open:");
out = new TreeNode<>();
getSearchNodeIncludeChildren("9", root, out);
TimeUtil.outCostOnConsoleMs();
LogUtil.log(gson.toJson(out));
}
public static void travel(TreeNode<String> root) {
LogUtil.print(root.getValue());
List<TreeNode<String>> nodes = root.getNodes();
if (nodes != null)
for (int i = 0; i < nodes.size(); i++) {
travel(nodes.get(i));
}
}
public static void getSearchNode(String msg, TreeNode<String> root, TreeNode<String> out) {
if (Objects.isNull(msg)) {
throw new NullPointerException("Searching msg is not blank!");
}
boolean isSetFatherNodeValue = false;
String v = root.getValue();
if (!v.isEmpty() && v.toLowerCase().indexOf(msg.toLowerCase()) > -1) {
out.setValue(v);
out.setStatus(root.getStatus());
isSetFatherNodeValue = true;
}
List<TreeNode<String>> nodes = root.getNodes();
if (nodes != null && nodes.size() > 0) {
List<TreeNode<String>> ns = new ArrayList<>();
for (int i = 0; i < nodes.size(); i++) {
TreeNode<String> node = new TreeNode<>();
getSearchNode(msg, nodes.get(i), node);
if (!Objects.isNull(node.getValue()) && !node.getValue().isEmpty()) {
ns.add(node);
if (!isSetFatherNodeValue) {
out.setValue(root.getValue());
out.setStatus(root.getStatus());
}
}
}
if (ns.size() > 0) {
out.setNodes(ns);
}
}
}
public static void getSearchNodeIncludeChildren(String msg, TreeNode<String> root, TreeNode<String> out) {
if (Objects.isNull(msg)) {
throw new NullPointerException("Searching msg is not blank!");
}
boolean isSetFatherNodeValue = false;
String v = root.getValue();
if (!v.isEmpty() && v.toLowerCase().indexOf(msg.toLowerCase()) > -1) {
out.setValue(v);
out.setStatus("open");
isSetFatherNodeValue = true;
}
List<TreeNode<String>> nodes = root.getNodes();
if (nodes != null && nodes.size() > 0) {
List<TreeNode<String>> ns = new ArrayList<>();
for (int i = 0; i < nodes.size(); i++) {
TreeNode<String> node = new TreeNode<>();
getSearchNodeIncludeChildren(msg, nodes.get(i), node);
if (!Objects.isNull(node.getValue()) && !node.getValue().isEmpty()) {
ns.add(node);
if (!isSetFatherNodeValue) {
out.setValue(root.getValue());
out.setStatus("open");
}
if ((node.getNodes() == null || node.getNodes().isEmpty())
&& !Objects.isNull(nodes.get(i).getNodes())
&& !nodes.get(i).getNodes().isEmpty()
) {
copyNodeAndSetClose(nodes.get(i), node);
}
}
}
if (ns.size() > 0) {
out.setNodes(ns);
}
}
}
private static void copyNodeAndSetClose(TreeNode<String> source, TreeNode<String> target) {
List<TreeNode<String>> children = source.getNodes();
List<TreeNode<String>> targetChildren = new ArrayList<>();
for (TreeNode<String> child : children) {
TreeNode<String> node = new TreeNode<>();
node.setValue(child.getValue());
node.setStatus("close");
targetChildren.add(node);
if (!Objects.isNull(child.getNodes()) && !child.getNodes().isEmpty()) {
copyNodeAndSetClose(child, node);
}
}
target.setNodes(targetChildren);
}
}