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

package org.example.tree;

import java.util.List;

public class TreeNode {
    private List<TreeNode> childs;
    private String op;
    private boolean isLeaf;

    private String val;

    private TreeNode parent;

    public List<TreeNode> getChilds() {
        return childs;
    }

    public void setChilds(List<TreeNode> 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<TreeNode> 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<TreeNode> leaf5Child = new ArrayList<>();
        leaf5Child.add(leaf7);
        leaf5Child.add(leaf8);
        TreeNode leaf5 = new TreeNode(leaf5Child,"&&");

        List<TreeNode> node1Child = new ArrayList<>();
        node1Child.add(leaf1);
        node1Child.add(leaf2);

        List<TreeNode> 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<TreeNode> 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<TreeNode> 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<TreeNode> 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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,可以使用BigDecimal类进行精确的数值计算,包括组合运算。BigDecimal提供了各种方法来执行加法、减法、乘法和除法等运算。 下面是一个示例,展示了如何使用BigDecimal执行组合运算: ```java import java.math.BigDecimal; public class BigDecimalExample { public static void main(String[] args) { BigDecimal num1 = new BigDecimal("10.5"); BigDecimal num2 = new BigDecimal("5.2"); // 加法 BigDecimal sum = num1.add(num2); System.out.println("Sum: " + sum); // 减法 BigDecimal difference = num1.subtract(num2); System.out.println("Difference: " + difference); // 乘法 BigDecimal product = num1.multiply(num2); System.out.println("Product: " + product); // 除法 BigDecimal quotient = num1.divide(num2, 2, BigDecimal.ROUND_HALF_UP); System.out.println("Quotient: " + quotient); } } ``` 在上面的示例中,我们首先创建了两个BigDecimal对象num1和num2,然后使用add()、subtract()、multiply()和divide()方法执行加法、法、乘法和除法运算。最后,我们将结果打印到控制台上。 需要注意的是,在使用divide()方法进行除法运算时,我们传递了两个参数。第一个参数是除数,第二个参数是保留的小数位数,第三个参数是舍入模式。在示例中,我们使用了BigDecimal.ROUND_HALF_UP舍入模式,表示四舍五入。 使用BigDecimal进行组合运算可以避免浮点数计算中的精度问题,提供了更准确的计算结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值