《数据结构与算法分析-java语言描述》 - 表达式树 -后缀表达式录入成表达式树

说明

  1. 调用之前写好的Calculator类,可以将中缀表达式转成后缀表达式。(改下方法权限就能调用了)
    后缀表达式求值、中缀转后缀:https://blog.csdn.net/LawssssCat/article/details/102962279
    写好的Calculator类https://blog.csdn.net/LawssssCat/article/details/102989450
  2. 代码中 p a r s e S u f i x E x p r e s s i o n T o B i n a r y T r e e N o d e T r e e ( ) \color{#ff0011}{parseSufixExpressionToBinaryTreeNodeTree()} parseSufixExpressionToBinaryTreeNodeTree()方法为"后缀表达式录入成表达式树"的实现。实现思路跟后缀求值一样。

代码

自定义的内部静态TreeNode类

private static class BinaryTreeNode<T> {

		private T elementDate ; 
		private BinaryTreeNode<T> left ; 
		private BinaryTreeNode<T> right ;
		public BinaryTreeNode(T e , BinaryTreeNode<T> left , BinaryTreeNode<T> right ) {
			this.elementDate = e; 
			this.left = left ; 
			this.right = right;
		}
		
		public void listAll() {
			listAll(0); 
		}
		/*
		 * 方便打印树形结构
		 */
		private void listAll(int ind) {
			//打印本节点值
			for(int i=0 ; i<ind ;i++) {
				System.out.print(" \\ ");
			}
			System.out.println(" "+this.elementDate);
			//先打印左节点值
			//后打印有节点值
			if(this.left!= null && this.right != null) {
				left.listAll(ind+1);
				right.listAll(ind+1);
			}
		}
		/*
		后缀表达式形式打印
		*/
		public void listAsExpression() {
			if(this.left==null && this.right==null) {
				System.out.print(this.elementDate);
			}else {//
				this.left.listAsExpression();
				System.out.print(" ");
				this.right.listAsExpression();
				System.out.print(" "+this.elementDate);
			}
		}
		
		
		
	}

实现类

package cn.edut.tree;

import java.util.List;
import java.util.Stack;

import org.junit.Test;

import cn.edut.clac.Calculator;

public class ExpressionTree {
	
	private static BinaryTreeNode<String> lastNode ; 
	
	public static  void parseSufixExpressionToBinaryTreeNodeTree(List<String> expression) {
		Stack<BinaryTreeNode<String>> stackNumberNode = new Stack<>(); 
		//遍历后缀表达式的每个字符
		for(String s : expression) {
			//如果是数字,进栈
			if(!s.matches("\\D")) {
				stackNumberNode.push(new BinaryTreeNode<String>(s, null, null));
			}else {//如果不是数字
				BinaryTreeNode<String> nodeRight = stackNumberNode.pop() ;
				BinaryTreeNode<String> nodeLeft = stackNumberNode.pop() ;
				BinaryTreeNode<String> operation = 
						new BinaryTreeNode<String>(s, nodeLeft, nodeRight);
				stackNumberNode.push(operation);
				
			}
		}
		lastNode = stackNumberNode.pop();
	}
	
	public BinaryTreeNode<String> getRoot(){
		return lastNode;
	}
}

测试方法

@Test
	public void test01() {
		/*
		 * 准备 
		 */
		//中缀表达式
		String expression = "1  + 7 * ( 8 + ( 16 + 12 ) / ( 13 - ( 20 / 15 ) ) )" ;
		//后缀表达式
		List<String> sufixExpression = Calculator.parseSufixExpression(
				Calculator.parseExpression(expression));
		//计算结果
		int result = Calculator.calcExpression(expression) ;
		//打印
		System.out.println("中缀表达式:"+expression);
		System.out.println("后缀表达式:"+ sufixExpression.toString());
		System.out.println("计算结果:"+result);
		/*
		 * 准备完毕	
		 */
		
		//分析表达式
		parseSufixExpressionToBinaryTreeNodeTree(sufixExpression);
		System.out.print("树以后缀摆列:");getRoot().listAsExpression();
		System.out.println("\n树形式展示:(左上右下)");
		getRoot().listAll();
		
		
	}

测试结果

在这里插入图片描述
在这里插入图片描述

完整代码

package cn.edut.tree;

import java.util.List;
import java.util.Stack;

import org.junit.Test;

import cn.edut.clac.Calculator;

public class ExpressionTree {
	@Test
	public void test01() {
		/*
		 * 准备 
		 */
		//中缀表达式
		String expression = "1  + 7 * ( 8 + ( 16 + 12 ) / ( 13 - ( 20 / 15 ) ) )" ;
		//后缀表达式
		List<String> sufixExpression = Calculator.parseSufixExpression(
				Calculator.parseExpression(expression));
		//计算结果
		int result = Calculator.calcExpression(expression) ;
		//打印
		System.out.println("中缀表达式:"+expression);
		System.out.println("后缀表达式:"+ sufixExpression.toString());
		System.out.println("计算结果:"+result);
		/*
		 * 准备完毕	
		 */
		
		//分析表达式
		parseSufixExpressionToBinaryTreeNodeTree(sufixExpression);
		System.out.print("树以后缀摆列:");getRoot().listAsExpression();
		System.out.println("\n树形式展示:(左上右下)");
		getRoot().listAll();
		
		
	}
	
	private static BinaryTreeNode<String> lastNode ; 
	
	public static  void parseSufixExpressionToBinaryTreeNodeTree(List<String> expression) {
		Stack<BinaryTreeNode<String>> stackNumberNode = new Stack<>(); 
		//遍历后缀表达式的每个字符
		for(String s : expression) {
			//如果是数字,进栈
			if(!s.matches("\\D")) {
				stackNumberNode.push(new BinaryTreeNode<String>(s, null, null));
			}else {//如果不是数字
				BinaryTreeNode<String> nodeRight = stackNumberNode.pop() ;
				BinaryTreeNode<String> nodeLeft = stackNumberNode.pop() ;
				BinaryTreeNode<String> operation = 
						new BinaryTreeNode<String>(s, nodeLeft, nodeRight);
				stackNumberNode.push(operation);
				
			}
		}
		lastNode = stackNumberNode.pop();
	}
	
	public BinaryTreeNode<String> getRoot(){
		return lastNode;
	}
	
	private static class BinaryTreeNode<T> {

		private T elementDate ; 
		private BinaryTreeNode<T> left ; 
		private BinaryTreeNode<T> right ;
		public BinaryTreeNode(T e , BinaryTreeNode<T> left , BinaryTreeNode<T> right ) {
			this.elementDate = e; 
			this.left = left ; 
			this.right = right;
		}
		
		public void listAll() {
			listAll(0); 
		}
		
		private void listAll(int ind) {
			//打印本节点值
			for(int i=0 ; i<ind ;i++) {
				System.out.print(" \\ ");
			}
			System.out.println(" "+this.elementDate);
			//先打印左节点值
			//后打印有节点值
			if(this.left!= null && this.right != null) {
				left.listAll(ind+1);
				right.listAll(ind+1);
			}
		}

		public void listAsExpression() {
			if(this.left==null && this.right==null) {
				System.out.print(this.elementDate);
			}else {//
				this.left.listAsExpression();
				System.out.print(" ");
				this.right.listAsExpression();
				System.out.print(" "+this.elementDate);
			}
		}
		
		
		
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骆言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值