二叉树的前序遍历、中序遍历和后序遍历

女生的网名这么多〃 提交于 2020-03-02 11:51:58

在这里插入图片描述


public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode root = new HeroNode("宋江",1);
        HeroNode hero2 = new HeroNode("无用",2);
        HeroNode hero3 = new HeroNode("卢俊义",3);
        HeroNode hero4 = new HeroNode("林冲",4);

        //说明,我们先手动创建二叉树,后面我们学习递归的方式创建二叉树
        root.setLeft(hero2);
        root.setRight(hero3);
        hero3.setRight(hero4);
        binaryTree.setRoot(root);

        //前序遍历
        System.out.println("前序遍历");
        binaryTree.preOrder();
        //中序遍历0
        System.out.println("中序遍历");
        binaryTree.infixOrder();
        //后序遍历
        System.out.println("后序遍历");
        binaryTree.postOrder();

    }
}

//定义BinaryTree 二叉树
class BinaryTree{
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    //前序遍历
    public void preOrder(){
        if (this.root != null){
            this.root.preOrder();
        }else{
            System.out.println("二叉树为空 无法遍历");
        }
    }

    //中序遍历
    public void infixOrder(){
        if (this.root != null){
            this.root.infixOrder();
        }else {
            System.out.println("二叉树为空 无法遍历");
        }
    }

    //后序遍历
    public void postOrder(){
        if (this.root != null){
            this.root.postOrder();
        }else {
            System.out.println("二叉树为空 无法遍历");
        }
    }
}


class HeroNode{
    private String name;
    private int no;
    private HeroNode left;      //默认null
    private HeroNode right;     //默认null

    public HeroNode(String name, int no) {
        this.name = name;
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    public HeroNode getLeft() {
        return left;
    }

    public HeroNode getRight() {
        return right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "name='" + name + '\'' +
                ", no=" + no +
                '}';
    }

    //编写前序遍历
    public void preOrder(){
        System.out.println(this);//先输出父节点
        //递归向左子树前序遍历
        if (this.left != null){
            this.left.preOrder();
        }
        //递归向右子树前去遍历
        if (this.right != null){
            this.right.preOrder();
        }
    }

    //编写中序遍历
    public void infixOrder(){
        //递归向左子树中序遍历
        if (this.left != null) {
            this.left.infixOrder();
        }
        //输出父节点
        System.out.println(this);
        //递归向右子树中序遍历
        if (this.right != null){
            this.right.infixOrder();
        }
    }

    //编写后序遍历
    public void postOrder(){
        if (this.left != null){
            this.left.postOrder();
        }
        if (this.right != null){
            this.right.postOrder();
        }
        System.out.println(this);
    }


}



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!