表达式二叉树的构建

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

如何构建树 ?

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

遍历二叉树的3种方法

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

  1. 先序排列
    遍历顺序为:根节点—>左节点—->右节点
    遍历结果:*-+1234
  2. 中序排列
    遍历顺序为:左节点—–>根节点—–>右节点
    遍历结果:1+2-3*4
  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/

猜你喜欢

转载自blog.csdn.net/A_D_I_D_A_S/article/details/82632464