- package org.example.tree;
-
- import java.util.List;
-
- public class TreeNode {
- private List
childs; - private String op;
- private boolean isLeaf;
-
- private String val;
-
- private TreeNode parent;
-
- public List
getChilds() { - return childs;
- }
-
- public void setChilds(List
childs) { - this.childs = childs;
- }
-
- public String getOp() {
- return op;
- }
-
- public void setOp(String op) {
- this.op = op;
- }
-
- public boolean isLeaf() {
- return isLeaf;
- }
-
- public void setLeaf(boolean leaf) {
- isLeaf = leaf;
- }
-
- public String getVal() {
- return val;
- }
-
- public void setVal(String val) {
- this.val = val;
- }
-
- public TreeNode(List
childs, String op) { - this.childs = childs;
- this.op = op;
- this.isLeaf = false;
- for (TreeNode child : childs) {
- child.setParent(this);
- }
- }
-
- public TreeNode( String val) {
- this.isLeaf = true;
- this.val = val;
- }
-
- public TreeNode getParent() {
- return parent;
- }
-
- public void setParent(TreeNode parent) {
- this.parent = parent;
- }
- }
- package org.example.tree;
-
- import cn.hutool.core.collection.CollectionUtil;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
-
- public class TreeMain {
-
- public static void main(String[] args) {
- TreeNode leaf1 = new TreeNode("A");
- TreeNode leaf2 = new TreeNode("B");
- TreeNode leaf3 = new TreeNode("C");
-
- TreeNode leaf7 = new TreeNode("G");
- TreeNode leaf8 = new TreeNode("H");
-
- TreeNode leaf4 = new TreeNode("E");
- TreeNode leaf6 = new TreeNode("F");
-
- List
leaf5Child = new ArrayList<>(); - leaf5Child.add(leaf7);
- leaf5Child.add(leaf8);
- TreeNode leaf5 = new TreeNode(leaf5Child,"&&");
-
- List
node1Child = new ArrayList<>(); - node1Child.add(leaf1);
- node1Child.add(leaf2);
-
- List
node2Child = new ArrayList<>(); - node2Child.add(leaf3);
- node2Child.add(leaf5);
- node2Child.add(leaf4);
- node2Child.add(leaf6);
-
- TreeNode node1 = new TreeNode(node1Child,"||");
- TreeNode node2 = new TreeNode(node2Child,"||");
- List
rootChilds = new ArrayList<>(); - rootChilds.add(node1);
- rootChilds.add(node2);
-
- TreeNode nodeRoot = new TreeNode(rootChilds,"&&");
- System.out.println(nodeRoot);
- traversal(nodeRoot);
- }
-
-
- private static void traversal(TreeNode treeNode){
- if(treeNode.isLeaf()){
- System.out.println(treeNode.getVal());
- }else {
- List
childs = treeNode.getChilds(); - boolean canMergeAnd = canMergeAnd(treeNode);
- if(canMergeAnd){
- String newVal = treeNode.getChilds().stream().map(item -> item.getVal()).collect(Collectors.joining(""));
- treeNode.setVal(newVal);
- treeNode.setChilds(null);
- treeNode.setOp(null);
- treeNode.setLeaf(true);
- System.out.println("合并:"+newVal);
- traversal(treeNode.getParent());
- }else if(canMergeAndOrOr(treeNode)){
-
- }else {
- System.out.println(treeNode.hashCode()+" ->mergeAnd:"+ canMergeAnd);
- for (TreeNode child : childs) {
- traversal(child);
- }
- }
- }
- }
-
- /**
- * 是否可以进行 && 合并
- * @param treeNode
- * @return
- */
- private static boolean canMergeAnd(TreeNode treeNode){
- if(!treeNode.isLeaf() && "&&".equals(treeNode.getOp())){
- long count = treeNode.getChilds().stream().filter(item -> !item.isLeaf()).count();
- return count == 0;
- }
- return false;
- }
- private static boolean canMergeAndOrOr(TreeNode treeNode){
- if(!treeNode.isLeaf() && "&&".equals(treeNode.getOp())){
- List
childs = treeNode.getChilds(); - long count = childs.stream().filter(item -> !item.isLeaf() && "||".equals(item.getOp())).count();
- if(count == childs.size()){
- for (TreeNode child : childs) {
- // 判断是否有非叶子
- boolean isHasNode = child.getChilds().stream().filter(item -> !item.isLeaf()).findAny().isPresent();
- return !isHasNode;
- }
- }
- }
- return false;
- }
- }