【java】表达式树

本文介绍了如何通过递归方法从给定的数学表达式构建表达式树,并提供了从前缀和后缀表达式构建表达式树的方法及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据数学表达式建立表达式树

对于数学表达式a+b*(c-d)-e/f,可以表示成如下图所示的二叉树,


要建立二叉表达式树,方法有很多,下面介绍一种:找到最后计算的运算符,然后递归处理。

对于最后计算的运算符,+ -号在* /号之后计算,而对于相同优先级的运算符(+和-,*和/),我们规定最右出现的后计算(也可按其他顺序设计程序)。

输入数学表达式,然后输出后缀表达式。

样例输入:

a+b*(c-d)-e/f

样例输出:

abcd-*+ef/-

import java.util.Scanner;

class Node{
	char val;
	Node left;
	Node right;
	public Node(char val){
		this.val=val;
	}
}
public class Main {
	public static Node buildTree(String str){
		Node node=null;
		if(str.length()==1){
			node=new Node(str.charAt(0));
			return node;
		}
		int p=0,c1=-1,c2=-1;			//p,c1,c2分别存放未匹配的括号数量,最右出现的+-号下标,最右出现的*/号下标
		for(int i=0;i<str.length();i++){
			switch(str.charAt(i)){
			case '(': 
				p++;
				break;
			case ')': 
				p--;
				break;
			case '+': case'-': 
				if(p==0) 
					c1=i;
				break;
			case '*': case'/':
				if(p==0)
					c2=i;
				break;
			}
		}
		if(c1<0&&c2<0)			//整个表达式是被一对括号括起来的
			node=buildTree(str.substring(1, str.length()-1));
		else if(c1>0){
			node=new Node(str.charAt(c1));
			node.left=buildTree(str.substring(0,c1));
			node.right=buildTree(str.substring(c1+1,str.length()));
		}else{
			node=new Node(str.charAt(c2));
			node.left=buildTree(str.substring(0,c2));
			node.right=buildTree(str.substring(c2+1,str.length()));
		}
		return node;
	}
	public static void postOrder(Node node){
		if(node==null)
			return;
		postOrder(node.left);
		postOrder(node.right);
		System.out.print(node.val);
	}
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNext()){
			String str=scanner.nextLine();
			Node root=buildTree(str);
			postOrder(root);
			System.out.println();
		}
		scanner.close();
	}
}


根据后缀表达式建立表达式树

输入后缀表达式,然后输出前缀表达式和中缀表达式。

样例输入:

abcd-*+ef/-

样例输出:

-+a*b-cd/ef
a+b*c-d-e/f

import java.util.Scanner;
import java.util.Stack;

class Node{
	char val;
	Node left;
	Node right;
	public Node(char val){
		this.val=val;
	}
}
public class Main {
	public static Node buildTree(String str){
		Stack<Node> stack=new Stack<Node>();
		for(char c:str.toCharArray()){
			if(c=='+'||c=='-'||c=='*'||c=='/'){
				Node node=new Node(c);
				Node right=stack.pop();
				Node left=stack.pop();
				node.left=left;
				node.right=right;
				stack.push(node);
			}else{
				Node node=new Node(c);
				stack.push(node);
			}
		}
		Node root=stack.pop();
		return root;
	}
	public static void preOrder(Node node){
		if(node==null)
			return;
		System.out.print(node.val);
		preOrder(node.left);
		preOrder(node.right);
	}
	public static void inOrder(Node node){
		if(node==null)
			return;
		inOrder(node.left);
		System.out.print(node.val);
		inOrder(node.right);
	}
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNext()){
			String str=scanner.nextLine();
			Node root=buildTree(str);
			preOrder(root);
			System.out.println();
			inOrder(root);
			System.out.println();
		}
		scanner.close();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值