表达式二叉树的构建

什么是表达式二叉树 ?
表达式二叉树由数字和运算符构成,将运算符作为父节点,数字作为左、右节点。如下图所示:
这里写图片描述

如何构建树 ?

  1. 创建数组队列number_list 和 operation_list,将数字和运算符分别储存在数组队列中。
  2. 从number_list中取出两个数字,从operation_list中取出一个运算符,组成一个节点。并且删除相应的数字和节点。
  3. 重复第二步,直到数字列表只剩一个节点,该节点就是根节点。

遍历二叉树的3种方法

(下面的遍历结果均以上图表达式二叉树为例)

  1. 先序排列
    遍历顺序为:根节点—>左节点---->右节点
    遍历结果:**-+1234*
  2. 中序排列
    遍历顺序为:左节点----->根节点----->右节点
    遍历结果:1+2-34*
  3. 后序排列
    遍历顺序为:左节点----->右节点----->根节点
    遍历结果:12+3-4*

代码示例

节点类:

属性:左节点、右节点、数据


public class Node{
	
	Node left,right;
	char data;
	
	public Node(){}
	
	public Node(char data){
		this.data = data;
	}
	
	
	public char getData(){
		return data;
	}
	
	public void setLeft(Node left){
		this.left = left;
	}
	
	public Node getLeft(){
		return left;
	}
	
	public void setRight(Node right){
		this.right = right;
	}
	
	public Node getRight(){
		return right;
	}

}

二叉树类:


public class Tree {
	
	static Node root;	//根节点
	String str;
	
	public void build_tree(String str){
		//创建链表,用来存储运算符
		ArrayList<Node> operation = new ArrayList<>();
		//创建链表,用来存储数字
		ArrayList<Node> number = new ArrayList<>();
		//将运算符和数字分别存放到相应的数组队列中
		for(int i=0;i<str.length();i++){
			char ch = str.charAt(i);
			Node temp = new Node(ch);
			if(ch>='0' && ch<='9'){
				number.add(temp);
			}else {
				operation.add(temp);
			}
		}
		//构建二叉树
		while(operation.size()>0){
			//取出数组队列(number)中的第一位元素作为左节点
			Node left = number.get(0);
			//移除(number)中的第一位元素
			number.remove(0);
			//取出数组队列(number)中的第一位元素作为右节点
			Node right = number.get(0);
			//移除(number)中的第一位元素
			number.remove(0);
			//取出数组队列(operation)中的第一位元素
			Node root = operation.get(0);
			//移除(operation)中的第一位元素
			operation.remove(0);
			//将运算符作为根节点,left 和 right 分别作为左右节点
			root.setLeft(left);
			root.setRight(right);
			//将新生成的节点作为第一个节点
			number.add(0, root);
		}
		root = number.get(0);
	}
	
	//先序遍历(根左右)
	public void before_output(Node node){
		if(node==null){
			return ;
		}else {
			System.out.print(node.data);
			before_output(node.getLeft());
			before_output(node.getRight());
		}
	}
	
	//中序遍历(左根右)
	public void middle_output(Node node){
		if(node.getLeft()!=null){
			middle_output(node.getLeft());
		}
		System.out.print(node.data);
		if(node.getRight()!=null){
			middle_output(node.getRight());
		}
	}
	
	//后序遍历(左右根)
	public void behind_output(Node node){
		if(node.getLeft()!=null){
			behind_output(node.getLeft());
		}
		if(node.getRight()!=null){
			behind_output(node.getRight());
		}
		System.out.print(node.data);
	}
	
	
	public static void main(String[] args){
		Tree tree = new Tree();
		tree.build_tree("1+2-3*4/5");
		System.out.println("先序排列:根->左->右:");
		tree.before_output(root);
		System.out.println();
		System.out.println("中序排列:左->右->根:");
		tree.middle_output(root);
		System.out.println();
		System.out.println("后序排列:左->右->根:");
		tree.behind_output(root);
	}
	
}

样例输出

先序排列:根->->右:
/*-+12345
中序排列:左->根->右:
1+2-3*4/5
后序排列:左->右->根:
12+3-4*5/

可承接各种项目,有意者加QQ:1217898975

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

steven_moyu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值