二叉树的先序、中序与后序遍历java语言实现

package Demo8;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree b = new BinaryTree(); //先创建一颗二叉树
        //需要创建结点
        HeroNode root = new HeroNode(1,"张三"); //将第一个结点当成树
        HeroNode h2 = new HeroNode(2,"李四");
        HeroNode h3 = new HeroNode(3,"王五");
        HeroNode h4 = new HeroNode(4,"七八");
        HeroNode h5 = new HeroNode(5,"五六");
        //说明,我们先手动创建二叉树,后面我们学习递归的方式创建二叉树
        root.setLeft(h2);           //根结点的左结点进行添加
        root.setRight(h3);
        h2.setLeft(h4);
        h4.setLeft(h5);
        b.setRoot(root);
        //测一下
        System.out.println("前序遍历!");
        b.preOrder();
        System.out.println("中序遍历!");
        b.infixOder();
        System.out.println("后序遍历!");
        b.postOrder();

    }
}

package Demo8;

public class BinaryTree {
    private  HeroNode root;
    public void BinaryTree(HeroNode root){
        this.root = root;
    }

    public HeroNode getRoot() {
        return 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 infixOder(){
        if(this.root!=null){
            this.root.infixorder();
        }else{
            System.out.println("当前二叉树为空!");
        }
    }
    //后序遍历
    public void postOrder(){
        if (this.root!=null){
            this.root.postoder();
        }else{
            System.out.println("当前二叉树为空!");
        }
    }
}

package Demo8;

public class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(){}         //构造方法
    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }
    //get、set方法


    public int getNo() {
        return no;
    }

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

    public String getName() {
        return name;
    }

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

    public HeroNode getLeft() {
        return left;
    }

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

    public HeroNode getRight() {
        return right;
    }

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
    //前序遍历
    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 postoder(){
        if(this.left!=null){
            this.left.postoder();
        }
        if (this.right!=null){
            this.right.postoder();
        }
        System.out.println(this);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值