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