Java实现二叉树的遍历详细代码

本文通过Node、BinaryTree和UseBinaryTree类详细讲解了如何使用Java实现二叉树的遍历,提供了具体的代码实现,并欢迎读者进行讨论。
摘要由CSDN通过智能技术生成

测试时,二叉树是采用逐个节点搭建的方法构建的

Node类:树的节点类

public class Node {
    private int no;
    private Node left;
    private Node right;

    public Node() {
    }

    public Node(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

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

    public Node getLeft() {
        return left;
    }

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

    public Node getRight() {
        return right;
    }

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

    @Override
    public String toString() {
        return "Node{" +
                "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(left != null){
            left.postOrder();
        }
        if(right != null){
            right.postOrder();
        }
        System.out.println(this);
    }
}

BinaryTree类:二叉树类

public class BinaryTree {
    private Node root;

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

    public Node getRoot() {
        return root;
    }

    public void preOrder(){
        if(root == null){
            System.out.println("二叉树为空");
            return;
        }
        root.preOrder();
    }

    public void infixOrder(){
        if(root == null){
            System.out.println("二叉树为空");
            return;
        }
        root.infixOrder();
    }

    public void postOrder(){
        if(root == null){
            System.out.println("二叉树为空");
            return;
        }
        root.postOrder();
    }
}

UseBinaryTree类:二叉树遍历测试类

public class UseBinaryTree {
    public static void main(String[] args) {
        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        Node n4 = new Node(4);
        Node n5 = new Node(5);
        Node n6 = new Node(6);
        BinaryTree bt = new BinaryTree();
        bt.setRoot(n1);
        bt.getRoot().setLeft(n2);
        bt.getRoot().setRight(n3);
        n2.setLeft(n4);
        n2.setRight(n5);
        n3.setLeft(n6);
        bt.preOrder();
        bt.infixOrder();
        bt.postOrder();
    }
}

欢迎讨论

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值