java实现二叉树的遍历

java实现二叉树的遍历

节点类

/**
 * 节点类
 */
class Node {
    private int id;
    private Node left;
    private Node right;

    /**
     * 前序遍历的方法
     */
    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);
    }

    @Override
    public String toString() {
        return "Node{" +
                "id=" + id +
                '}';
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }
}

二叉树类

/**
 * 二叉树
 */
class BinaryTree {
    private Node root;

    /**
     * 前序遍历
     */
    public void preOrder() {
        // 判断是否为空
        if (this.root == null) {
            System.out.println("二叉树为空");
            return;
        }
        this.root.preOrder();
    }

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

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

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

测试类

public class Demo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        // 创建需要的节点
        Node root = new Node(000);
        Node node01 = new Node(001);
        Node node02 = new Node(002);
        Node node03 = new Node(003);
        Node node04 = new Node(004);

        // 手动创建二叉树
        binaryTree.setRoot(root);
        root.setLeft(node01);
        root.setRight(node02);
        node02.setLeft(node03);
        node02.setRight(node04);

        // 前序
        binaryTree.preOrder();
        System.out.println("=============");
        // 中序
        binaryTree.infixOrder();
        System.out.println("=============");
        // 后序
        binaryTree.postOrder();
        System.out.println("=============");
    }
}
posted @ 2022-04-16 22:29  CoderCatIce  阅读(74)  评论(0编辑  收藏  举报