[Java]手动构建表达式二叉树,求值,求后序表达式

Inlet类,这颗二叉树是”人力运维“的:

package com.hy;

public class Inlet {
    public static void main(String[] args) throws Exception{

        // 手动构造表达式二叉树
        Node n4=new Node(NodeType.Digit,4,null,null);
        Node n5=new Node(NodeType.Digit,5,null,null);
        Node nPlus=new Node(NodeType.OP_Plus,n4,n5);
        
        Node n6=new Node(NodeType.Digit,6,null,null);
        Node n2=new Node(NodeType.Digit,2,null,null);
        Node nDivide=new Node(NodeType.OP_Divide,n6,n2);
        
        Node n8=new Node(NodeType.Digit,8,null,null);
        Node nMinus=new Node(NodeType.OP_Minus,n8,nDivide);
        
        Node root=new Node(NodeType.OP_Multi,nPlus,nMinus);
        
        // 求值
        System.out.println("表达式(4+5)*(8-6/2)求值="+root.getValue());
        
        // 后序遍历
        System.out.print("表达式(4+5)*(8-6/2)转化为后序表达式为");
        postOrder(root);
    }
    
    // 后序遍历
    private static void postOrder(Node n){
        if(n!=null){
            postOrder(n.getLeftNode());
            
            postOrder(n.getRightNode());
            System.out.print(n);
        }
    }
}

运行结果如下:

表达式(4+5)*(8-6/2)求值=45.0
表达式(4+5)*(8-6/2)转化为后序表达式为4.0 5.0 + 8.0 6.0 2.0 / - * 

Node类 这个类用来表示二叉树节点:

package com.hy;

// 二叉树节点类
public class Node {
    private NodeType type;
    private float value;
    private Node leftNode;
    private Node rightNode;
    
    public Node(){
        type=NodeType.Undifined;
        value=0.0f;
        leftNode=null;
        rightNode=null;
    }
    
    public Node(NodeType type,float value,Node leftNode,Node rightNode){
        this.type=type;
        this.value=value;
        this.leftNode=leftNode;
        this.rightNode=rightNode;
    }
    
    public Node(NodeType type,Node leftNode,Node rightNode){
        this.type=type;
        this.value=0;
        this.leftNode=leftNode;
        this.rightNode=rightNode;
    }
    
    public float getValue() throws Exception{
        if(this.type==NodeType.Digit){
            return value;
        }else if(this.type==NodeType.OP_Divide){
            return leftNode.getValue()/rightNode.getValue();
        }else if(this.type==NodeType.OP_Minus){
            return leftNode.getValue()-rightNode.getValue();
        }else if(this.type==NodeType.OP_Multi){
            return leftNode.getValue()*rightNode.getValue();
        }else if(this.type==NodeType.OP_Plus){
            return leftNode.getValue()+rightNode.getValue();
        }else{
            throw new Exception("Not initialize");
        }
    }
    
    public void setLeftNode(Node leftNode) {
        this.leftNode = leftNode;
    }

    public void setRightNode(Node rightNode) {
        this.rightNode = rightNode;
    }
    
    public Node getLeftNode() {
        return leftNode;
    }

    public Node getRightNode() {
        return rightNode;
    }
    
    public String toString(){
        if(this.type==NodeType.Digit){
            return String.valueOf(value)+" ";
        }else if(this.type==NodeType.OP_Divide){
            return "/ ";
        }else if(this.type==NodeType.OP_Minus){
            return "- ";
        }else if(this.type==NodeType.OP_Multi){
            return "* ";
        }else if(this.type==NodeType.OP_Plus){
            return "+ ";
        }else{
            return "? ";
        }
    }
}

NodeType枚举 用来定义二叉树类型:

package com.hy;

// 节点类型
public enum NodeType {
    Undifined,
    OP_Plus,
    OP_Minus,
    OP_Multi,
    OP_Divide,
    Digit,
}

好了,到此,又把Long long ago学的数据结构又复习了一遍。

--END--2019年9月3日18点42分

posted @ 2019-09-03 18:45  逆火狂飙  阅读(423)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东