• 多叉树组合运算(未完待续)


    1. package org.example.tree;
    2. import java.util.List;
    3. public class TreeNode {
    4. private List childs;
    5. private String op;
    6. private boolean isLeaf;
    7. private String val;
    8. private TreeNode parent;
    9. public List getChilds() {
    10. return childs;
    11. }
    12. public void setChilds(List childs) {
    13. this.childs = childs;
    14. }
    15. public String getOp() {
    16. return op;
    17. }
    18. public void setOp(String op) {
    19. this.op = op;
    20. }
    21. public boolean isLeaf() {
    22. return isLeaf;
    23. }
    24. public void setLeaf(boolean leaf) {
    25. isLeaf = leaf;
    26. }
    27. public String getVal() {
    28. return val;
    29. }
    30. public void setVal(String val) {
    31. this.val = val;
    32. }
    33. public TreeNode(List childs, String op) {
    34. this.childs = childs;
    35. this.op = op;
    36. this.isLeaf = false;
    37. for (TreeNode child : childs) {
    38. child.setParent(this);
    39. }
    40. }
    41. public TreeNode( String val) {
    42. this.isLeaf = true;
    43. this.val = val;
    44. }
    45. public TreeNode getParent() {
    46. return parent;
    47. }
    48. public void setParent(TreeNode parent) {
    49. this.parent = parent;
    50. }
    51. }
    1. package org.example.tree;
    2. import cn.hutool.core.collection.CollectionUtil;
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.stream.Collectors;
    6. public class TreeMain {
    7. public static void main(String[] args) {
    8. TreeNode leaf1 = new TreeNode("A");
    9. TreeNode leaf2 = new TreeNode("B");
    10. TreeNode leaf3 = new TreeNode("C");
    11. TreeNode leaf7 = new TreeNode("G");
    12. TreeNode leaf8 = new TreeNode("H");
    13. TreeNode leaf4 = new TreeNode("E");
    14. TreeNode leaf6 = new TreeNode("F");
    15. List leaf5Child = new ArrayList<>();
    16. leaf5Child.add(leaf7);
    17. leaf5Child.add(leaf8);
    18. TreeNode leaf5 = new TreeNode(leaf5Child,"&&");
    19. List node1Child = new ArrayList<>();
    20. node1Child.add(leaf1);
    21. node1Child.add(leaf2);
    22. List node2Child = new ArrayList<>();
    23. node2Child.add(leaf3);
    24. node2Child.add(leaf5);
    25. node2Child.add(leaf4);
    26. node2Child.add(leaf6);
    27. TreeNode node1 = new TreeNode(node1Child,"||");
    28. TreeNode node2 = new TreeNode(node2Child,"||");
    29. List rootChilds = new ArrayList<>();
    30. rootChilds.add(node1);
    31. rootChilds.add(node2);
    32. TreeNode nodeRoot = new TreeNode(rootChilds,"&&");
    33. System.out.println(nodeRoot);
    34. traversal(nodeRoot);
    35. }
    36. private static void traversal(TreeNode treeNode){
    37. if(treeNode.isLeaf()){
    38. System.out.println(treeNode.getVal());
    39. }else {
    40. List childs = treeNode.getChilds();
    41. boolean canMergeAnd = canMergeAnd(treeNode);
    42. if(canMergeAnd){
    43. String newVal = treeNode.getChilds().stream().map(item -> item.getVal()).collect(Collectors.joining(""));
    44. treeNode.setVal(newVal);
    45. treeNode.setChilds(null);
    46. treeNode.setOp(null);
    47. treeNode.setLeaf(true);
    48. System.out.println("合并:"+newVal);
    49. traversal(treeNode.getParent());
    50. }else if(canMergeAndOrOr(treeNode)){
    51. }else {
    52. System.out.println(treeNode.hashCode()+" ->mergeAnd:"+ canMergeAnd);
    53. for (TreeNode child : childs) {
    54. traversal(child);
    55. }
    56. }
    57. }
    58. }
    59. /**
    60. * 是否可以进行 && 合并
    61. * @param treeNode
    62. * @return
    63. */
    64. private static boolean canMergeAnd(TreeNode treeNode){
    65. if(!treeNode.isLeaf() && "&&".equals(treeNode.getOp())){
    66. long count = treeNode.getChilds().stream().filter(item -> !item.isLeaf()).count();
    67. return count == 0;
    68. }
    69. return false;
    70. }
    71. private static boolean canMergeAndOrOr(TreeNode treeNode){
    72. if(!treeNode.isLeaf() && "&&".equals(treeNode.getOp())){
    73. List childs = treeNode.getChilds();
    74. long count = childs.stream().filter(item -> !item.isLeaf() && "||".equals(item.getOp())).count();
    75. if(count == childs.size()){
    76. for (TreeNode child : childs) {
    77. // 判断是否有非叶子
    78. boolean isHasNode = child.getChilds().stream().filter(item -> !item.isLeaf()).findAny().isPresent();
    79. return !isHasNode;
    80. }
    81. }
    82. }
    83. return false;
    84. }
    85. }

  • 相关阅读:
    Universal adversarial perturbations(2017 CVPR)
    依汇心理系统搭建平台模式
    国庆征文获奖名单公布
    【FPGA教程案例77】通信案例3——数据组帧,帧同步、拆帧
    巧妙利用unbuffer实时写入
    Spring Boot Security配置用户认证和资源授权
    es6 Promise, all, race 理解及应用
    算法分析与设计CH15:动态规划算法详解合集——装配线问题、矩阵链乘、LCS(最长公共子序列问题)、最大子数组和问题
    Langchain Chain - RouterChain 根据输入相关性进行路由的路由链
    ARM cache 分析
  • 原文地址:https://blog.csdn.net/liuhenghui5201/article/details/134522141