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分

最新文章

  1. KM模板
  2. ThinkPHP 3.2.3 自动加载公共函数文件的方法
  3. MVC Actionlink 参数说明
  4. 译:C#面向对象的基本概念 (Basic C# OOP Concept) 第三部分(多态,抽象类,虚方法,密封类,静态类,接口)
  5. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
  6. UVaLive4043 UVa1411 Ants 巨人与鬼
  7. Fastreport怎么样在同一页上下部分打印相同内容
  8. SCGHR 系统设计
  9. javascript高级知识分析——作为对象的函数
  10. android 中webview调用js
  11. 如何使用mysqldump备份数据库
  12. mpvue小程序开发之 wx.getUserInfo获取用户信息授权
  13. bat实现固定时间循环抓取设备log
  14. (转)Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践
  15. Session, Token and SSO 有什么区别
  16. python字符串前带u,r,b的含义
  17. XMPP 基础
  18. Android低功耗蓝牙(BLE)使用详解
  19. BZOJ - 2618 凸多边形 (半平面交)
  20. java-logic====吃货联盟

热门文章

  1. Ceph自动化部署----Ceph-ansible
  2. sourceforge.net
  3. mybatis-03
  4. mysql数据库: 用户管理、pymysql使用、navicat插件使用
  5. 余数之和BZOJ1257
  6. Java类的反射
  7. React全家桶构建一款Web音乐App实战(六):排行榜及歌曲本地持久化
  8. STM32F103C8T6最小板搞定CMSIS-DAP和SWO功能
  9. bat批处理文件
  10. Codeforces 884E E. Binary Matrix